False-positive with CLANG_ANALYZER_NONNULL
| Bugzilla Link | 26143 |
| Version | unspecified |
| OS | MacOS X |
| Attachments | warning with code |
| Reporter | LLVM Bugzilla Contributor |
| CC | @devincoughlin |
Extended Description
Summary: CLANG_ANALYZER_NONNULL with Xcode 7.3 Beta 1 is giving false-positive warnings.
Steps to Reproduce: static NSString *stringArray[] = { @"http://example.net" };
- (void)load { [[[self alloc] init] useIndex:0]; }
- (void)useIndex:(int)index { if (stringArray[index]) { } NSURL *url = [NSURL URLWithString:stringArray[index]]; NSLog(@"%@", url); }
Expected Results: No warning
Actual Results: Warning "Null passed to a callee that requires a non-null argument"
Version: Xcode Version 7.3 beta (7D111g) OS X beta 10.11.4 (15E27e)
When we have static NSString * const stringArray[] = { @"" };, could analyzer reason that stringArray[0] is never nil?
Reproduced. Thanks for the report!
This happens because the analyzer does not reason that stringArray[0] is not nil and so when it sees the if statement, it explores two paths: a spurious path one when stringArray[0] is definitely nil and a feasible when it is definitely not nil.
It is along the spurious path that the analyzer warns about nullability.
One potential workaround is to move the call to +URLWithString: inside the 'if'. Another workaround is to add assert(stringArray[index] != nil) before the call.
Warning reproductible with those few lines:
static NSString * const stringArray[] = { @"http://example.net" };
- (void)load { if (stringArray[0]) { } [NSURL URLWithString:stringArray[0]]; }
assigned to @tkremenek