Bug 334754 - Potential Null pointer access and the assert keyword
Summary: Potential Null pointer access and the assert keyword
Status: VERIFIED WORKSFORME
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.7   Edit
Hardware: PC Windows 7
: P3 normal (vote)
Target Milestone: 3.7 M5   Edit
Assignee: Ayushman Jain CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-01-19 05:33 EST by Laurent Goubet CLA
Modified: 2011-01-25 01:37 EST (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Laurent Goubet CLA 2011-01-19 05:33:32 EST
Hi,

This might be intentional, but I couldn't find a bug report for this (bug #173725 is close, but that's not it).

I have enabled the "potential null access" check in the compiler options. Previously (3.4, 3.5, 3.6, I believe it was also the case in 3.3), the following code :

[...]
Object test = test();
if (test != null) {
	doSomething();
}
assert test != null;
test.toString();
[...]

compiled with no problem. i.e : the "assert x != null" prevented the apparition of the compilation error/warning "potential null pointer access".

I believe that this warning only serves at compilation time in order to help the developper. The assert keyword is at the same level : it might not be there at all at runtime, so whatever we do in an assert is only compilation time information. For the code above, the "assert x != null" is only there to tell the developper/compiler that "Yes, the compiler doesn't, but I know that this variable can no longer be null at that location".

In 3.7, the code above triggers the apparition of this "potential NPE" warning. Is that a regression, or was it intended?
Comment 1 Ayushman Jain CLA 2011-01-19 06:15:13 EST
By default, we assume that asserts are not enabled at runtime, and we do our null analysis based on that assumption. So, in this piece of code, the presence of assert statement makes no difference to the analysis. And indeed, without the assert, the variable test can potentially be null. So this is correct.

Check out the new compiler error/warning preference "include assert in null analysis" in the preferences>java>compiler>errors/warnings. If you enable it, the warning goes away, as you wanted!
Comment 2 Ayushman Jain CLA 2011-01-19 06:18:56 EST
Marking as WORKSFORME, since there's no warning when the option is turned on.
Comment 3 Laurent Goubet CLA 2011-01-19 07:15:45 EST
Thanks, this kind of option is indeed what I sought :).
Comment 4 Satyam Kandula CLA 2011-01-25 01:37:04 EST
Verified for 3.7M5 using build I20110124-1800