@Contract("false -> !null") not honored
Hello!
I'd expected that code to compile:
String bar() {
return foo(false);
}
@Contract("false -> !null")
@Nullable String foo(boolean returnNull) {
if (returnNull) {
return null;
}
return "foo";
}
Esssentially foo has a contract that if returnNull is false, the method never returns null.
Hoewever, when compiling this, NullAway errors:
error: [NullAway] returning @Nullable expression from method with @NonNull return type
return foo(false);
^
(Wrongfully) changing the contract to
@Contract("_ -> !null")
and compilation works.
Maybe a case of partial support of @Contract annotations, as mentioned here?
Reproducer: 1232.zip
Thanks for the report. We have partial support for contracts and don't yet support detecting when true or false is passed as a literal argument. @mhalbritter is this showing up often in your code base?
Can't say for sure, but I don't think so. I'm currently working on adding nullness annotations to our codebase, but it's far from done. So far, i've encountered the pattern two times.
Thanks for the report. We have partial support for contracts and don't yet support detecting when
trueorfalseis passed as a literal argument. @mhalbritter is this showing up often in your code base?
In the Assert class in Spring Framework, we have several @Contract declarations that make use of boolean false in the contract clause.
Similarly, in the Assertions class in JUnit Jupiter, we have several @Contract declarations that make use of boolean true or false in the contract clause.
@sbrannen do you have any examples with true or false in the antecedents and then null or !null in the consequent?
In any case, we can look into this one but it'll take a bit of work.