Skip to content

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

An answer to this question on Stack Overflow.

Question

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)
}

Answer

In C23 you can write

[[maybe_unused]] int somevar;

(details)