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

Bug 428205

Summary: [null][correlation] Null annotation heuristics do not follow boolean and logic
Product: [Eclipse Project] JDT Reporter: Jari Juslin <zds>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: CLOSED DUPLICATE QA Contact:
Severity: enhancement    
Priority: P3 CC: stephan.herrmann
Version: 4.4Keywords: helpwanted
Target Milestone: 4.7 M1   
Hardware: PC   
OS: Linux   
Whiteboard:

Description Jari Juslin CLA 2014-02-14 10:19:56 EST
import org.eclipse.jdt.annotation.Nullable;

public class NullAnnotationTestCase {
    void case2(@Nullable String license) {
        boolean haveLicense = (license != null && !license.isEmpty());

        if (haveLicense) {
            license.toString();
        }
    }
}

Now, the line inside the if results in warning "Potential null pointer access: The variable license may be null at this location".

Expected result: no warning.

In this form the logic is the same, but the error message goes away:
    void case2(@Nullable String license) {
        if ((license != null && !license.isEmpty())) {
            license.toString();
        }
    }

Unfortunately in our case the code needs the boolean variable elsewhere, too, so it's not practical to refactor all the conditions straight to the if statements.
Comment 1 Jari Juslin CLA 2014-02-14 10:29:43 EST
A slightly different case, but I think it boils down to the same problem:

Produces warning:
    void case3(@Nullable Date date, List<String> strings) {
        for (String string : strings) {
            boolean dateOk = (date != null);
            
            if (!dateOk) {
                continue;
            }
            
            date.toString();
        }
    }

Again, if I embed the condition to the if statement, the problem goes away:
    void case3(@Nullable Date date, List<String> strings) {
        for (String string : strings) {
            if (!(date != null)) {
                continue;
            }
            
            date.toString();
        }
    }
Comment 2 Stephan Herrmann CLA 2014-02-14 21:13:37 EST
This RFE is a member of a group of existing requests marked as [correlation]: https://bugs.eclipse.org/bugs/buglist.cgi?short_desc=[correlation]&classification=Eclipse&query_format=advanced&short_desc_type=allwordssubstr&component=Core&product=JDT&list_id=7605356

It is a known limitation of the existing analysis that it cannot track the correlation between local variables, like: "I know that when (haveLicense==true) then also (license!=null)".

Adding correlation analysis would be a complex task, with significant impact on compiler performance. I don't have a strategy how to make both ends meet.
Comment 3 Stephan Herrmann CLA 2016-06-28 17:17:02 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 4 Jay Arthanareeswaran CLA 2016-08-03 07:57:52 EDT
Verified for 4.7 M1.
Comment 5 Stephan Herrmann CLA 2018-08-30 10:33:44 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 ***