Reputation: 171
I want to do a function to get a pointer on a struct. I done this :
void *getTokenList() {
static t_token *list;
return &list;
}
At compilation, I have this warning :
warning: variable ‘list’ set but not used [-Wunused-but-set-variable]
Is it possible to disable this warning for this function (only this one), or put an GCC attribute on this variable to hide this warning?
I had put #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
in top of my file but I want to hide this warning ONLY for this variable in this function.
Thanks, Jean
Upvotes: 15
Views: 16456
Reputation: 4679
The upcoming C23 standard will provide attributes as a standardized way to consider such cases (very similar to attributes in C++).
With this you can write
[[maybe_unused]] static t_token *list;
Although not yet standardized, the functionality is already available from gcc 10.1, even with pre C23 code, see this example on godbolt.
Further resources:
Upvotes: 0
Reputation: 144520
This warning was a bug in gcc. The warning, introduced in version 4.6, can easy be disabled as explained in other answers, but it should be noted that current versions of gcc do not produce the warning.
Upvotes: 1
Reputation: 182609
You can use this to shut it off:
(void)list;
Alternatively and less portably you can use __attribute__((unused))
.
Upvotes: 21