Bug 367660 - Null pointer access analysis does not understand assert statements
Summary: Null pointer access analysis does not understand assert statements
Status: VERIFIED WONTFIX
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.8   Edit
Hardware: PC Windows Vista
: P3 trivial (vote)
Target Milestone: 3.8 M5   Edit
Assignee: Stephan Herrmann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-12-30 11:33 EST by Mikko Tommila CLA
Modified: 2012-01-23 02:07 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 Mikko Tommila CLA 2011-12-30 11:33:22 EST
Build Identifier: Version: Indigo Service Release 1 Build id: 20110916-0149














Reproducible: Always

Steps to Reproduce:
This is an example test program. The second assert statement unnecessarily produces a warning. If assertions are enabled then the variable in question can't be null. If assertions are disabled then the expression is not evaluated.

public class AssertTest
{
    public static void main(String[] args) {
        int[] ints = null;
        assert ((ints = getData()) != null);
        assert (ints.length == 2);  // Null pointer access: The variable ints can only be null at this location
    }

    private static int[] getData()
    {
        int[] data = { 1, 2 };
        return data;
    }
}
Comment 1 Mikko Tommila CLA 2011-12-30 11:38:40 EST
If there is a variable that is initially null, and then two assert statements, one which assigns a non-null value to the variable, and another that tests it, then the null pointer access analysis does not understand this. It thinks that the second assert statement must be accessing a null pointer, even if the variable was set in the previous assert statement.
Comment 2 Stephan Herrmann CLA 2011-12-30 13:14:29 EST
The warning disappears if you check the option
  [x] Include 'assert' in null analysis

The idea is that side effects of an assert statement are not
considered without this option enabled, because these side
effects might be missing at runtime if assertions are disabled.

OTOH suppressing all warnings inside assert statements would hide
relevant warnings, too.

One might argue the example in this bug is a special case, since
we are talking about an effect link from an assert to another assert.
However, I'm afraid that implementing special treatment for this
pattern would likely be very expensive (in terms of added complexity
and maybe even performance).

Since the mentioned option can be used to silence the warning I'm 
leaning towards closing as wontfix.

I wonder if we could give a hint about the option (quick fix?),
but the option affects how the *first* assert is analysed, and at
that location we have no warning to offer a quick fix for.
Comment 3 Ayushman Jain CLA 2012-01-03 02:23:30 EST
(In reply to comment #2)
> I wonder if we could give a hint about the option (quick fix?),
> but the option affects how the *first* assert is analysed, and at
> that location we have no warning to offer a quick fix for.

Note that from M5 onwards all null pointer access warnings are grouped under "Null analysis" in the prefs page. I think this will make it considerably easier for the user to spot that there's a special option to include asserts.
+1 to close as wontfix.
Comment 4 Stephan Herrmann CLA 2012-01-03 04:10:22 EST
No action planned -> closing.