Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 290263

Summary: [compiler][null][correlation] Wrong "potential null pointer access"
Product: [Eclipse Project] JDT Reporter: Mauro Molinari <mauromol>
Component: CoreAssignee: JDT Core Triaged <jdt-core-triaged>
Status: CLOSED DUPLICATE QA Contact: Ayushman Jain <amj87.iitr>
Severity: enhancement    
Priority: P5 CC: axel.gross, christian.dietrich.opensource, david, kstenerud, manoj.palat, max.gilead, Olivier_Thomann, stephan.herrmann
Version: 3.5Keywords: helpwanted
Target Milestone: 4.7 M1   
Hardware: PC   
OS: Windows Server 2003   
Whiteboard:

Description Mauro Molinari CLA 2009-09-23 09:48:36 EDT
Consider the following code:

if (v1 == v2)
  return true;

if ((v1 == null && v2 != null) || (v1 != null && v2 == null))
  return false;

return v1.equals(v2);

A potential null pointer access on v1 warning is raised on the last line, but neither v1 nor v2 can be null at that point.
Comment 1 Stephan Herrmann CLA 2011-02-28 06:31:35 EST
Fixing this would require analysis of the correlation of v1 and v2,
which we currently don't do.
Comment 2 Stephan Herrmann CLA 2011-02-28 06:42:01 EST
*** Bug 244222 has been marked as a duplicate of this bug. ***
Comment 3 Manoj N Palat CLA 2013-05-06 23:34:04 EDT
import org.eclipse.jdt.annotation.*;
public class X {
    @Nullable Object o1, o2;
    /**
	 * @param other  
	 */
    public void foo(X other) {
      if (o1 == null && o1 != o2)
          System.out.println(o2.toString());//get a "potential null pointer accesss warning here.
    }
}

Stephan: I believe the issue mentioned in the test code above is related to this bug.
Comment 4 Stephan Herrmann CLA 2013-05-07 08:12:03 EDT
(In reply to comment #3)

If you change your example to using local variables instead of 
nullable fields, the warning disappears as you expect.

Generally we do consider correlation against known-to-be-null values
(as compared to general (v1==v2) correlation for unknown values).
So in this example the reason for not detecting the pattern is not because of
correlation in itself, but because a level of sophistication is needed
that simply is not (and shall not) be provided by syntactic analysis
for nullable fields.
Comment 5 Stephan Herrmann CLA 2016-06-28 17:17:49 EDT
Bulk closing all compiler bugs tagged [null][correlation], because we have no plans to add such a feature: it would be a tremendous implementation effort, beyond our current man power, and may be impossible to achieve within the desired performance bounds.

If s.o. has a viable strategy or even implementation for such a feature, I'm all ears.
Comment 6 Jay Arthanareeswaran CLA 2016-08-03 07:58:19 EDT
Verified for 4.7 M1.
Comment 7 Stephan Herrmann CLA 2017-03-14 08:44:41 EDT
*** Bug 513639 has been marked as a duplicate of this bug. ***
Comment 8 Stephan Herrmann CLA 2018-08-30 10:43:06 EDT
I created a new umbrella RFE outlining what would be needed to address this issue.

*** This bug has been marked as a duplicate of bug 538421 ***