Created
January 30, 2013 18:31
-
-
Save numist/4675493 to your computer and use it in GitHub Desktop.
After a lifetime of being humiliated by the static analyzer, it has a false positive and I am vindicated.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
void doSomething(id obj) __attribute__((nonnull(1))); | |
void doSomething(id obj) | |
{ | |
NSLog(@"%@", obj); | |
} | |
int main(int argc, const char * argv[]) | |
{ | |
@autoreleasepool { | |
NSArray *list = @[@"a", @"b", @"c", @"d", [NSNull null]]; | |
__block id match = nil; | |
BOOL (^listContainsNull)() = ^{ | |
for (id item in list) { | |
if (item == [NSNull null]) { | |
return YES; | |
} | |
} | |
return NO; | |
}; | |
while (listContainsNull()) { | |
for (id item in list) { | |
if (item == [NSNull null]) { | |
match = item; | |
} | |
} | |
doSomething(match); // Analyzer: Null pointer passed as an argument to a 'nonnull' parameter | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That said, putting an
NSAssert
above the call todoSomething
resolves the issue reported by the analyzer and adds value to the code by making its assumptions more explicit. More robust code == better code!