Which is the best way to suppress "unused variable" warning
Solution 1
This is the approach I use: cross platform macro for silencing unused variables warning
It allows you to use one macro for any platform (although the definitions may differ, depending on the compiler), so it's a very portable approach to express your intention to popular compilers for C based languages. On GCC and Clang, it is equivalent of wrapping your third example (#pragma unused(testString)
) into a macro.
Using the example from the linked answer:
- (void)testString:(NSString *)testString
{
MONUnusedParameter(testString);
}
I've found this approach best for portability and clarity, in use with some pretty large C, C++, ObjC, and ObjC++ codebases.
Solution 2
If you are compiling with GCC, you can take advantage of attribute extensions to set the 'unused' attribute. Like this:
int somevar __attribute__((unused));
It also works for unused parameter warnings (-Wunused-parameter)
To make it shorter to write I am using this macro:
#define _U_ __attribute__((unused))
And declare like this:
int somevar _U_ ;
AAV
Updated on December 22, 2020Comments
-
AAV over 3 years
There are 3 (which I know) ways to suppress the "unused variable" warning. Any particular way is better than other ?
First
- (void)testString:(NSString *)testString { (void)testString; }
Second
- (void)testString:(NSString *)__unused testString { }
Third
- (void)testString:(NSString *)testString { #pragma unused(testString) }
-
Admin almost 11 years
-Wno-unused-variable
-
Lee Meador almost 11 yearsDelete or comment out the unused parts.
-
ouah almost 11 yearsThe first one, casting to
void
is the most portable and more idiomatic way. -
jscs almost 11 yearsThere's also
#pragma clang diagnostic push #pragma clang diagnostic ignored "-Wunused-variable" <#unused variables#> #pragma clang diagnostic pop
-
ott-- almost 11 years
-
AAV almost 11 years@ouah only problem with that is if you have more than one unused parameter then you have to put one more void on next line and it will go on...
-
ouah almost 11 years@AmitVyawahare yes, but if you have more than like 4 parameters maybe you should redesign your API
-
gnasher729 about 10 years-Wno-unused-variable is deeply unprofessional. It's like smashing the little speaker that warns you when you are driving without a seatbelt, instead of putting on the seatbelt. Cast to void is idiomatic, and it is valid code on every compiler.
-
MarcusJ over 9 years-Wno-unused-variable does not wrk from the command line, nor does -Wno-unused
-
johannes_lalala about 2 years@gnasher729 not all code is professional, not all code needs to be professional. Apart from that, there are many situations where it's desirable and totally ok to mute almost any particular warning including unused-variable.
-
-
AAV almost 11 yearsI like the idea. I hate left indent #pragma by doing this we can solve that problem as well.
-
Ozgur Vatansever over 10 yearsYou shouldn't allocate more memory than you need just to suppress a warning.
-
bdesham about 10 yearsThis is likely to be confusing to anyone who reads your code later.
-
OLL over 8 yearsGCC would also warn that it is set, but unused.
-
Sapphire_Brick over 2 yearsIdentifiers starting with "_" followed by a capital letter are reserved.