llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
26.77k stars 10.97k forks source link

Add strict checker for nullability #32627

Open llvmbot opened 7 years ago

llvmbot commented 7 years ago
Bugzilla Link 33280
Version 4.0
OS MacOS X
Reporter LLVM Bugzilla Contributor

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.

llvmbot commented 7 years ago

assigned to @tkremenek

yshui commented 4 days ago

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
}