Clang: -Wnullable-to-nonnull-conversion is over–eager in Objective-C++

Originator:daniel
Number:rdar://26902722 Date Originated:20-Jun-2016 03:12 PM
Status:Open Resolved:
Product:Developer Tools Product Version:Xcode-beta (8S128d)
Classification:Other Bug Reproducible:Always
 
Summary:
With the first developer seed of Xcode 8, clang has gotten much stricter regarding nullability — which is good! Unfortunately, it also reports false positives — which is sad.

Steps to Reproduce:
Compile the following function with `-Wnullable-to-nonnull-conversion` in the specified Xcode version as Objective–C and as Objective-C++:
```
static BOOL IsValidJSONData(NSData *const _Nullable data) {
    if (data == nil) return NO;

    return nil != [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:NULL];
}
```

Expected Results:
While the implementation is absurdly wasteful, the code above should compile without warnings (let alone errors). This happens regardless if compiled as Objective–C or Objective–C++.

Actual Results:
When compiled as Objective–C, the code compiles without errors or warnings. When compiled as Objective–C++, however, there’s a warning that the parameter passed to `+[NSJSONSerialization JSONObjectWithData:options:error:]` could be nil — which is obviously wrong, since there is no way that the constant could have been changed.

Notes:
This does not happen on Xcode 7. In fact, marking a nullable parameter as const, and then performing a nil check is how I silenced these warnings without casts or additional local variables.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!