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

20,143

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_ ;
Share:
20,143
AAV
Author by

AAV

Updated on December 22, 2020

Comments

  • AAV
    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
      Admin almost 11 years
      -Wno-unused-variable
    • Lee Meador
      Lee Meador almost 11 years
      Delete or comment out the unused parts.
    • ouah
      ouah almost 11 years
      The first one, casting to void is the most portable and more idiomatic way.
    • jscs
      jscs almost 11 years
      There's also #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wunused-variable" <#unused variables#> #pragma clang diagnostic pop
    • ott--
      ott-- almost 11 years
    • AAV
      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
      ouah almost 11 years
      @AmitVyawahare yes, but if you have more than like 4 parameters maybe you should redesign your API
    • gnasher729
      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
      MarcusJ over 9 years
      -Wno-unused-variable does not wrk from the command line, nor does -Wno-unused
    • johannes_lalala
      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
    AAV almost 11 years
    I like the idea. I hate left indent #pragma by doing this we can solve that problem as well.
  • Ozgur Vatansever
    Ozgur Vatansever over 10 years
    You shouldn't allocate more memory than you need just to suppress a warning.
  • bdesham
    bdesham about 10 years
    This is likely to be confusing to anyone who reads your code later.
  • OLL
    OLL over 8 years
    GCC would also warn that it is set, but unused.
  • Sapphire_Brick
    Sapphire_Brick over 2 years
    Identifiers starting with "_" followed by a capital letter are reserved.