Bug 385664 - [null]Apply nullness warnings in reverse direction
Summary: [null]Apply nullness warnings in reverse direction
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.2   Edit
Hardware: PC Linux
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-07-21 10:31 EDT by Holger Klene CLA
Modified: 2012-07-21 19:18 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 Holger Klene CLA 2012-07-21 10:31:36 EDT
Build Identifier: Version: 4.2.0 Build id: I20120608-1400

Have a look at the example method.

The check if (unknown == null) sheds doubt on unknown, which manifests in a Potential null pointer access (=PNPA) below.

Copying the very same block above the check will go unnoticed.

In addition modifying the check to:
if (b && unknown == null) or
if (!b && unknown == null)
will swallow the PNPA while
if (unknown == null && b) or
if (unknown == null && !b)
both preserve it. I wonder if short-circuiting boolean expression could be suppressed for gathering null-info, as b may or may not depend on the nullness. Naively I consider the check against null as a deliberately placed warning sign.

It would be great, if the first dereference in a method could be flagged, possibly hinting, that a nullcheck follows. The other way round may also be worthwile to flag the nullcheck as "Potentially dead code, as the variable may have been dereferenced before."

Reproducible: Always

Steps to Reproduce:
public void someMethod(final Object unknown, final boolean b) {
    if (b) {
        // No Warning/Error here :-(
        unknown.toString();
    }

    if (unknown == null) {
        System.out.println("is null");
    }

    if (b) {
        // Potential null pointer access:
        // The variable unknown may be null at this location
        unknown.toString();
    }
}
Comment 1 Stephan Herrmann CLA 2012-07-21 19:18:18 EDT
Warning potential null *before* the check that sheds doubt doesn't work in a linear one-pass flow analysis. We won't spend much efforts in improving this hinting system. The way forward is to use null annotations.

I'm leaving this open for now because I'm not sure about the short-circuiting boolean expression thing. I image people have previously shown examples where doubt due to a check that wasn't even evaluated (short-circuited) is considered irrelevant, i.e., the requested additional warning will be considered a false positive.

Re "potentially dead code": that's not an interesting message. Unless its definitely dead there's no action to be taken by the user. Most conditionals introduce potentially dead code :)