Open ghost opened 5 months ago
Thanks for the report! Yes, that looks like a false positive that we should look into removing.
Note that if the field value
were @Nullable
, this would not be a false positive: the Runnable
could be run at some later point and the field might have been re-set to null by that time.
But for a @MonotonicNonNull
, once it is non-null it cannot become null again, so we know that the pre-condition will be fulfilled for any future call.
Interestingly, test case in here https://github.com/bazelbuild/bazel/issues/10326 also got a false positive
import org.checkerframework.checker.nullness.qual.Nullable;
public class T {
private final @Nullable String f;
T(@Nullable String f) {
this.f = f;
}
void test() {
if (f == null) {
return;
}
Runnable r =
() -> {
System.err.println(f.toString());
};
}
}
checker/bin/javac -processor nullness T.java
T.java:17: error: [dereference.of.nullable] dereference of possibly-null reference f
System.err.println(f.toString());
^
1 error
Commands
checker-framework-3.42.0-eisop3/checker/bin/javac -processor nullness TestNullable.java
Inputs
Outputs
Expectation
Similar code with
print();
instead ofRunnable r = () -> print();
is allowed in current version of checker framework. Checker framework should allow both version of the code, regardless ifthis
is captured by a lambda or not.