Bug 338718

Summary: [compiler][null]redundant null check not caught by warnings checker
Product: [Eclipse Project] JDT Reporter: Jon Stevens <latchkey>
Component: CoreAssignee: Ayushman Jain <amj87.iitr>
Status: VERIFIED WORKSFORME QA Contact:
Severity: normal    
Priority: P3 CC: amj87.iitr, Olivier_Thomann, satyam.kandula, stephan.herrmann
Version: 3.7   
Target Milestone: 3.7 M6   
Hardware: Macintosh   
OS: Mac OS X - Carbon (unsup.)   
Whiteboard:

Description Jon Stevens CLA 2011-03-02 13:50:15 EST
Build Identifier: 20100917-0705

I've got some code that boils down to something like this:

    private Foo getFoo(Var vo) {
        Foo ack;
        switch (vo.getBlah()) {
            default:
                break;
        }
        ack.setBoo(vo == null ? null : vo.getBoo());
        return ack;
    }

findbugs correctly reports this as a redundant null check (in the ternary operation). Eclipse doesn't find this with redundant null check turned on to warnings. It seems like it should because vo couldn't be null in setBoo() because the vo.getBlah() would have thrown an NPE in the switch statement.

Thanks!

Reproducible: Always
Comment 1 Ayushman Jain CLA 2011-03-03 01:54:34 EST
I don't see this problem on either 3.6.2 or 3.7 latest I build. Though it does exist on 3.6.1.
Since it now seems fixed, closing this as WORKSFORME.
Comment 2 Stephan Herrmann CLA 2011-03-03 07:13:45 EST
While trying to find the bug by which this issue was fixed I couldn't 
even reproduce in any version I tried. So, WORKSFORME, seems to be the
correct resolution after all.

BTW: this is the full source I used:

public class Bug {
  private Foo getFoo(Var vo) {
        Foo ack;
        switch (vo.getBlah()) {
            default:
                break;
        }
        ack.setBoo(vo == null ? null : vo.getBoo());
        return ack;
    }
class Foo {
  void setBoo(String s) {}
}
class Var{
  int getBlah() { return 0; }
  String getBoo() { return ""; }
}
}

This invocation:

$ java -jar ecj-3.6.1.jar -warn:+null Bug.java

Gives these problems:

----------
1. WARNING in Bug.java (at line 2)
        private Foo getFoo(Var vo) {
                    ^^^^^^^^^^^^^^
The method getFoo(Bug.Var) from the type Bug is never used locally
----------
2. ERROR in Bug.java (at line 8)
        ack.setBoo(vo == null ? null : vo.getBoo());
        ^^^
The local variable ack may not have been initialized
----------
3. WARNING in Bug.java (at line 8)
        ack.setBoo(vo == null ? null : vo.getBoo());
                   ^^
Null comparison always yields false: The variable vo cannot be null at this location
----------
4. ERROR in Bug.java (at line 9)
        return ack;
               ^^^
The local variable ack may not have been initialized
----------
4 problems (2 errors, 2 warnings)
Comment 3 Jon Stevens CLA 2011-03-03 12:39:20 EST
Weird. Sorry about that. Maybe it has something to do with my Eclipse install? The upgrade process always seems kind of dubious to me. =) I'll download a new version of 3.6.2, reinstall my plugins, wipe my .metadata and try it again.
Comment 4 Satyam Kandula CLA 2011-03-07 06:52:11 EST
Verified for 3.7M6