Re: False positive unused variable

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Dec 05, 2023 at 03:10:05AM +0100, Henrik Holst via Gcc-help wrote:
> int64_t seq = seq_next (queue); /* get next free place */
> struct item *item = queue_get (queue, seq); /* get the item at place 'seq'
> */
> 
> item->a = 1; /* we just fill in dummy data for the example */
> item->b = 2;
> 
> queue_publish (queue, seq); /* publish place 'seq' as containing data */
> 
> Now the issue here is that since 'item' is never referenced by any
> function, GCC, falsely sees item as being an unused variable. So far AFAIK
> only a warning is issued and that can be ignored, but at one time in the
> future I fear that the optimizer will kick in here and simply remove where
> we fill item with data.

GCC will never do things like this.  The queue_get function can have
side effects (=~ it may make changes to state), so the function needs to
be called, even if the result isn't used.  The warning is because this
is likely a mistake (maybe you have an "item2" variable as well and you
confused them somewhere, silly typoes or thinkoes like that).

> So does there exist a proper way to tell GCC that this is a false positive?
> The only attribute I could find is (unused) which silences the warning but
> ofc will only further tell GCC that the variable really is unused. Honestly
> this feels like a need for there being a (used) attribute to inform the
> compiler that yes this variable is indeed used even though it doesn't
> detect it.

Just don't write

    struct item *item = queue_get (queue, seq);

if you won't use the function result, but instead write the much clearer
(clearer to the compiler, as well as to human readers, which is the more
important part!)

    queue_get (queue, seq);


Segher



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux