Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable one unused variable warning

How can I disable one warning in one place only?

I have one variable which I temporarily don't use. Xcode shows me a warning about the "unused variable". I want to disable the warning, but for this variable only, not all warnings of this type.

Is it possible without setting/getting this variable's value?

like image 636
user2159978 Avatar asked Feb 19 '14 07:02

user2159978


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 remove unused variables in Python?

To remove all unused imports (whether or not they are from the standard library), use the --remove-all-unused-imports option. To remove unused variables, use the --remove-unused-variables option.

How do I disable GCC?

To answer your question about disabling specific warnings in GCC, you can enable specific warnings in GCC with -Wxxxx and disable them with -Wno-xxxx. From the GCC Warning Options: You can request many specific warnings with options beginning -W , for example -Wimplicit to request warnings on implicit declarations.

What does unused mean in C?

In GCC, you can label the parameter with the unused attribute. This attribute, attached to a variable, means that the variable is meant to be possibly unused. GCC will not produce a warning for this variable.


2 Answers

It's pretty simple:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-variable"
    NSUInteger abc; /// Your unused variable
#pragma clang diagnostic pop

But keeping unused variable is redundant and generally bad idea. Unused code should be removed. If you use git, there all changes are still in your repo and you can revert your code if you found that this variable is necessary.

like image 107
Tomasz Szulc Avatar answered Sep 22 '22 06:09

Tomasz Szulc


From GCC / Specifying Attributes of Variables (understood by Clang as well):

int x __attribute__ ((unused));

or

int y __attribute__((unused)) = initialValue ;
like image 21
Martin R Avatar answered Sep 22 '22 06:09

Martin R