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

Bug 134933

Summary: [compiler][null][correlation] use of an intermediate boolean results into a false positive detection
Product: [Eclipse Project] JDT Reporter: Simon Archer <sja.eclipse>
Component: CoreAssignee: Ayushman Jain <amj87.iitr>
Status: CLOSED DUPLICATE QA Contact:
Severity: enhancement    
Priority: P5 CC: stephan.herrmann
Version: 3.2Keywords: helpwanted
Target Milestone: 4.7 M1   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Simon Archer CLA 2006-04-04 22:53:12 EDT
Using Eclipse 3.2/M6.

Given the following Java class:

  public class Test {
    private Object getObject() {
      return "Hello World";
    }
	
    public void foo() {
      Object o = getObject();
      boolean legal = o != null;
      if (legal == false) return;
      String s = o.toString();  // WARNING: "The variable o may be null"
      System.out.println(s);
    }	
  }

The "Java > Compiler > Errors/Warnings" preference for "Null reference" is being somewhat over zealous, issuing the warning, "The variable o may be null".  While clearly the use the word "may" indicates possibilities, it is rather unfortunate that the JDT cannot detect this usage pattern.  

Of course, for this simply test case it would be fine to fold the validation into the "if" statement, but for many real applications where validation is more complicated the use of temporary variables is useful.

Sadly, as it works today I think many people would choose to disable this feature due to the large number of false positives.
Comment 1 Maxime Daniel CLA 2006-04-05 05:00:28 EDT
Compared to bug 129907, this case introduces two extra difficulties:
- the boolean variable legal is not final, which means that changes to its relationship to o null status should be tracked;
- the expression that guards o being null is composite (even if it is a simple composite in this case).
Hence the case would call for a more general variables correlation analysis than what we can do with the current technology.
Not in 3.2.
Comment 2 Simon Archer CLA 2006-04-05 09:32:49 EDT
Fair enough.  I guess I'll either change my coding style or adust my preferences.  Thanks for considering this one.
Comment 3 Maxime Daniel CLA 2007-06-19 08:07:34 EDT
Reopening as P5 (since RESOLVED LATER is deprecated).
Comment 4 Stephan Herrmann CLA 2016-06-28 17:18:00 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 5 Sasikanth Bharadwaj CLA 2016-08-02 04:33:34 EDT
Verified for 4.7 M1
Comment 6 Stephan Herrmann CLA 2018-08-30 10:38:49 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 ***