Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is the best way to suppress "unused variable" warning

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)
}
like image 211
AAV Avatar asked Jul 12 '13 19:07

AAV


People also ask

How do I get rid of the unused variable warning?

Solution: If variable <variable_name> or function <function_name> is not used, it can be removed. If it is only used sometimes, you can use __attribute__((unused)) . This attribute suppresses these warnings.

How do you handle unused variables in Python?

To suppress the warning, one can simply name the variable with an underscore ('_') alone. Python treats it as an unused variable and ignores it without giving the warning message.

What are unused variables?

Unused variables are a waste of space in the source; a decent compiler won't create them in the object file. Unused parameters when the functions have to meet an externally imposed interface are a different problem; they can't be avoided as easily because to remove them would be to change the interface.

What does unused variable mean in C?

No nothing is wrong the compiler just warns you that you declared a variable and you are not using it. It is just a warning not an error. While nothing is wrong, You must avoid declaring variables that you do not need because they just occupy memory and add to the overhead when they are not needed in the first place.


1 Answers

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.

like image 142
justin Avatar answered Oct 15 '22 17:10

justin