Bug 329547 - Incorrect redundant null check warning
Summary: Incorrect redundant null check warning
Status: VERIFIED INVALID
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.7   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 3.7 M4   Edit
Assignee: Ayushman Jain CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-11-05 09:43 EDT by Deepak Azad CLA
Modified: 2010-12-07 00:15 EST (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Deepak Azad CLA 2010-11-05 09:43:30 EDT
I20101102-0800

void foo() {
	String s="123";
	String nullString= null;
	if(s.charAt(2)=='1') {
		nullString="not null";
	}
	System.out.println(nullString.length()); //Potential null pointer access
	if(nullString!=null) {                   //Redundant null check
		System.out.println(nullString);
	}
}

'Potential null pointer access' is correct, but 'Redundant null check' warning looks incorrect to me. 

If the sysout statement is removed 'Redundant null check' warning goes away. Whether the variable is accessed or not should not make a difference in the analysis about its null state. The only thing that should matter is whether the variable is assigned an object or not.
Comment 1 Olivier Thomann CLA 2010-11-05 09:59:53 EDT
(In reply to comment #0)
> If the sysout statement is removed 'Redundant null check' warning goes away.
> Whether the variable is accessed or not should not make a difference in the
> analysis about its null state. The only thing that should matter is whether the
> variable is assigned an object or not.
Maybe the wording can be improved, but I disagree with this. If the variable is used to invoke a method, then if it is null it would fail with a NPE. So in order to reach the if statement, nullString cannot be null. So if you check null, then it is redundant as it cannot be null at this location without a runtime failure with a NPE.
Comment 2 Deepak Azad CLA 2010-11-05 10:20:13 EDT
ok, maybe the warning is not incorrect, but seeing the 2 warnings I was a bit confused for a moment - is the variable null or is it not null?
Comment 3 Ayushman Jain CLA 2010-11-05 10:21:13 EDT
(In reply to comment #1)
>[..]
> Maybe the wording can be improved, but I disagree with this. If the variable is
> used to invoke a method, then if it is null it would fail with a NPE. So in
> order to reach the if statement, nullString cannot be null. So if you check
> null, then it is redundant as it cannot be null at this location without a
> runtime failure with a NPE.

Exactly. The warning's wording is "Redundant null check: The variable nullString cannot be null at *this location*". And so, it is better to see the warnings independently of each other. Since the if statement will only be reached when nullString is not null, the null check is redundant at *that location*. In that sense, both warnings are correct.

In this case since they occur one after the other, I agree that they can be a little confusing sometimes. But they also tell the user the complete story only when they both are displayed. In the following example, 

void foo() {
    String s="123";
    String nullString1= null;
    if(s.charAt(2)=='1') {
        nullString="not null";
    }
    System.out.println(nullString1.length()); //Potential null pointer access
    if(nullString1!=null || nullString2!= null) {                   //Redundant null check on nullString1
        System.out.println(nullString);
    }
}

this warning helps the user to convert the always true if statement to a conditional one by simply removing the redundant null check on nullString1. There may be other cases where the user can benefit from both warnings.
Comment 4 Ayushman Jain CLA 2010-11-08 04:02:37 EST
Closing as invalid, since the warnings are correct.
Comment 5 Srikanth Sankaran CLA 2010-12-07 00:15:12 EST
Verified for 3.7M4