Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 221
Mark Lilback Avatar asked Jan 21 '12 12:01

Mark Lilback


2 Answers

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-value"
 pageId = [NSNumber numberWithInt:val];
#pragma clang diagnostic pop
like image 108
Matt Hudson Avatar answered Oct 13 '22 22:10

Matt Hudson


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

like image 35
csga5000 Avatar answered Oct 13 '22 23:10

csga5000