Mark Lilback
Mark Lilback

Reputation: 1164

avoiding "expression result unused" warning in a block

The following code is returning an expression unused warning on the assignment operation in the block. The code isn't very practical, but there is a lot more code in the excluded section and that code has to run on a particular queue.

__block NSNumber *pageId=nil;
dispatch_sync(_myDispatchQueue, ^{
    int val;
    //... code generates an int and puts it in val
    pageId = [NSNumber numberWithInt:val];
}];
//pageId used below

How do I get rid of this error?

Upvotes: 23

Views: 7248

Answers (2)

csga5000
csga5000

Reputation: 4141

My Experimental Findings

Note I got this from Intrubidus, but I wanted additional information so after experimenting I recorded my findings here for the next guy.

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-value"
pageId = [NSNumber numberWithInt:val];
#pragma clang diagnostic pop

Only applies to the area between the ignore and the pop. "-Wunused-value" does not suppress unused variables.



This is how you would suppress unused variables:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-variable"
int i = 0;
#pragma clang diagnostic pop



Also, without the push and pop, as shown:

#pragma clang diagnostic ignored "-Wunused-value"
pageId = [NSNumber numberWithInt:val];

The type of warning was ignored anywhere in that file after the #pragma. This seems to only apply to the file in question.

Hope you found this useful,
    - Chase

Upvotes: 3

Matt Hudson
Matt Hudson

Reputation: 7348

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-value"
 pageId = [NSNumber numberWithInt:val];
#pragma clang diagnostic pop

Upvotes: 47

Related Questions