What is the best way to suppress A "Unused variable x" warning?
CGccC Problem Overview
What is the best/neatest way to suppress a compiler (in this case GCC) like "Unused variable x" warning?
I don't want to give any certain flags to GCC to remove all these warnings, just for special cases.
C Solutions
Solution 1 - C
(void) variable
might work for some compilers.
For C++ code, also see Mailbag: Shutting up compiler warnings where Herb Sutter recommends using:
template<class T> void ignore( const T& ) { }
...
ignore(variable);
Solution 2 - C
Do not give the variable a name (C++)
void foo(int /*bar*/) {
...
}
Tell your compiler using a compiler specific nonstandard mechanism
See individual answers for __attribute__((unused))
, various #pragma
s and so on. Optionally, wrap a preprocesor macro around it for portability.
Switch the warning off
IDEs can signal unused variables visually (different color, or underline). Having that, compiler warning may be rather useless.
In GCC and Clang, add -Wno-unused-parameter
option at the end of the command line (after all options that switch unused parameter warning on, like -Wall
, -Wextra
).
Add a cast to void
void foo(int bar) {
(void)bar;
}
As per jamesdlin's answer and Mailbag: Shutting up compiler warnings.
Solution 3 - C
I found an article, http://sourcefrog.net/weblog/software/languages/C/unused.html, that explains UNUSED. It is interesting that the author also mangles the unused variable name, so you can't inadvertently use it in the future.
Excerpt:
#ifdef UNUSED
#elif defined(__GNUC__)
# define UNUSED(x) UNUSED_ ## x __attribute__((unused))
#elif defined(__LCLINT__)
# define UNUSED(x) /*@unused@*/ x
#else
# define UNUSED(x) x
#endif
void dcc_mon_siginfo_handler(int UNUSED(whatsig))
Solution 4 - C
If this is really what you want, you could use the unused attribute (GCC only), something like:
void foo(int __attribute__((__unused__)) bar) {
...
}
Not just for function parameters, of course, but that's the most common use case, since it might be a callback function for an API where you don't actually need all the input.
Additionally, GLib has a G_GNUC_UNUSED macro which I believe expands to that attribute.
Solution 5 - C
You can silence the warning using #pragma
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused"
int unususedVariable = 1;
#pragma clang diagnostic pop
If you are using GCC, use #pragma gcc ...
Solution 6 - C
#pragma unused <variable>
Solution 7 - C
It's a very hackish solution, but try simply assigning the variable to itself.
I think that should fool most compilers into thinking that the variable is used. It should be quite portable too.
Solution 8 - C
The cast to a void is the best approach because it shows that you didn't "accidentally" keep the variable in your code - ie: this function might be an instance where you have a table of function pointers that need the same parameter types and return types, but in this particular table entry you are not using the parameter.
That said, if you don't need it, get rid of it. ;)
Solution 9 - C
Assign it to itself:
void f(int unused) {
unused = unused;
}
It works in GCC, but Clang needs -Wno-self-assign.
I think casting to void is the most portable solution: Both GCC and Clang understand this, even with full warnings -W{all,extra,pedantic}
:
(void)unused;
Solution 10 - C
Delete the unused variable declaration from the code (pun intended).
(What??? It's what I do: point that the obvious is most likely the best solution.)
Now, from comments on other answers, apparently it's garbage generated from macros. Well, that's a pleonasm.
Solutions:
- refactor that macro to #if declare the variable only if it's really used;
- create another version of the macro that skips the unused variable generation.
- Better still, avoid using macros that bring issues to the code.
Solution 11 - C
If it’s used and you are shipping the project, delete it. Worst, comment it.