7

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?

jscs
  • 63,694
  • 13
  • 151
  • 195
user2159978
  • 2,629
  • 2
  • 16
  • 14
  • Well, you could simply NSLog that variable. Or you could just ignore the warning. Any special reason it bothers you so much ? – user1349663 Feb 19 '14 at 07:42
  • 1
    possible duplicate of [How can I get rid of an "unused variable" warning in Xcode](http://stackoverflow.com/questions/5451123/how-can-i-get-rid-of-an-unused-variable-warning-in-xcode) – jscs Feb 19 '14 at 07:43
  • @user1349663 Not really a good suggestion to just `NSLog` it. This is just going to cause a load junk in the console that isn't needed. – Popeye Feb 19 '14 at 08:19
  • @Popeye True. I just suggested it since s/he said that the variable is "temporarily unused" (i.e s/he will use it later and be done with the warning as well as the log. But I get your point. – user1349663 Feb 19 '14 at 08:23
  • I asked about it because: 1)I imported a library as code which has some unused variables but I don't want to edit it; 2)I replaced global #define ... with a static variable - the code is workable, but this string is highlighted as unused (but it is really used in other files) – user2159978 Feb 19 '14 at 13:39

4 Answers4

10

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.

Tomasz Szulc
  • 4,217
  • 4
  • 43
  • 79
7

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

int x __attribute__ ((unused));

or

int y __attribute__((unused)) = initialValue ;
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • almost, you should mention that if it was written `static int x = ...;` then additional code should be added in the middle: `static int x __attribute__ ((unused)) = ...;" – user2159978 Feb 20 '14 at 08:32
  • 1
    @user2159978: That is generally so (not only for static variables). I have updated the answer accordingly. Thanks for the feedback. – Martin R Feb 20 '14 at 08:37
3
__unused int theInt = 0;
// there will be no warning, but you are still able to use `theInt` in the future
OlDor
  • 1,460
  • 12
  • 18
0

Shortest keystroke answer, change:

int x; // this variable temporarily unused

to:

// int x; // this variable temporarily unused

and the warning will go. Also you cannot forget to remove it when you need the variable again, which you can do with any approach which leaves the variable declared.

If you want the removal to be a bit more visible, but wish to keep the property that you can't forget to remove it, try:

#if 0
int x; // this variable temporarily unused
#endif

HTH

CRD
  • 52,522
  • 5
  • 70
  • 86
  • wrong answer. I know about comments `//` and `#if 0`. I need to disable the warning only, but not to delete/comment the variable – user2159978 Feb 20 '14 at 08:30
  • @user2159978 - I did wonder why you'd want to keep an unused variable around. One reason might be to keep the object *size* the same, but that is unlikely for an Objective-C object so I discounted it. Can you say why you need storage allocated for a variable you're not using? Just curious. – CRD Feb 20 '14 at 09:00
  • I answered in a comment to my question – user2159978 Feb 20 '14 at 10:23
  • @user2159978 - I see that now. For (2) you should look at `extern` so you have just one variable rather than 1 per file, some unused. For (1) I fail to see why adding an attribute is not an edit, but all methods work and you should pick which one you like! – CRD Feb 20 '14 at 18:09