-
Notifications
You must be signed in to change notification settings - Fork 355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nullness checker incorrectly marking possible null on compound if/else condition #5016
Comments
/assign |
Thanks for reporting this problem. You are right that the Nullness Checker does not track arbitrary boolean conditions (not even ones as simple as a conjunction). Thus, the code that you wrote is beyond the capabilities of its simple brain. Here is a variant that the Nullness Checker does verify: import java.time.LocalTime;
import org.checkerframework.checker.nullness.qual.Nullable;
public class IfElseNullable {
private int compareLastEdited(
final @Nullable LocalTime thisLastEdited, final @Nullable LocalTime otherLastEdited) {
if (null == thisLastEdited) {
if (null != otherLastEdited) {
return 1;
} else {
// null == otherLastEdited
return 0;
}
} else {
// null != thisLastEdited
if (null == otherLastEdited) {
return -1;
} else {
return thisLastEdited.compareTo(otherLastEdited);
}
}
}
} It differs in that each There are pros and cons of the two different ways of writing the code, and I can see why you would prefer your version. It would be possible to change the Checker Framework to track more complex boolean conditions, which would enable it to handle your version. This would take extra code and would have a run-time cost, so unfortunately it is not high on our priority list (though we would be happy to review such a code contribution). |
According to typetools/checker-framework#5016 compound conditions are not handled by checker.
In this code, it's not possible for thisLastEdited or otherLastEdited to be null in the else block. This is a pretty common way to check for nulls in comparisons.
I didn't put this on #4872 because that one is accessing local class state, so another thread may made the class variable null between statements. Here I have stored a local reference to ensure that this can't happen.
These are the outputs of the checker framework.
This output was created using checker framework version 3.21.1.
The text was updated successfully, but these errors were encountered: