Open llvmbot opened 7 years ago
assigned to @tkremenek
I was just about to open another issue for this, good thing I checked :sweat_smile:
Can we also have this for C/C++ too, not just obj-c? This will be useful there too.
Example:
int f(int * _Nonnull a) {
return *a;
}
int g1(int * _Nullable a) {
return f(a); // <-- warning: passing nullable to nonnull
}
int g2(int * _Nullable a) {
if (!a) return;
return f(a); // <-- OK
}
Extended Description
The _Nonnull and _Nullable annotations seem mainly focused on preventing the literal "nil" being passed into a method or set for a nonnull property. However, this is much more relaxed than Swift's null checking. It would be great if we could statically analyze if what was being passed in as a nonnull parameter was nonnull itself. It could recognize if a nullable variable was being used in a way that made it nonnull.
For example: if (!x) { return; } [self runWithNonnullArg:x];
or
[self runWithNonnullArg:(x ?: @"")];
Both of these would be pass the static analysis.