Null analysis seems to ignore short-circuiting [message #1833427] |
Mon, 12 October 2020 22:18 |
David M. Karr Messages: 800 Registered: July 2009 |
Senior Member |
|
|
I've been experimenting with null annotations.
I'm looking at the following block of code, which I did not write, and I've simplified it:
if (null != response.getCart().getStatus()
&& response.getCart().getStatus() .equalsIgnoreCase("SUCCESS")) {
Without any null annotations, no warnings appear here.
If I go to the "Cart" class and add "@NonNullByDefault" to the class, that results in the following warning on the first line of that block:
Quote:Redundant null check: comparing '@NonNull String' against null
This is expected, as it now believes that all the properties of that are not null, so I shouldn't compare it against null.
However, that particular property actually can be null, so I added @Nullable to the return type of the getter. This now results in the following warning on the SECOND line:
Quote:Potential null pointer access: The method getStatus() may return null
This is certainly true, but in this block of code, it's not possible to reach this line, as the first condition in the ANDed expression will be false, so it will never execute the second subexpression.
Is it reasonable for it to check for short-circuiting here and NOT emit this warning?
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03076 seconds