Bug 428205 - [null][correlation] Null annotation heuristics do not follow boolean and logic
Summary: [null][correlation] Null annotation heuristics do not follow boolean and logic
Status: CLOSED DUPLICATE of bug 538421
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.4   Edit
Hardware: PC Linux
: P3 enhancement (vote)
Target Milestone: 4.7 M1   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords: helpwanted
Depends on:
Blocks:
 
Reported: 2014-02-14 10:19 EST by Jari Juslin CLA
Modified: 2018-08-30 10:33 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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 ***