Bug 198967

Summary: [compiler][null] False positives for "null comparison always yields false" after instanceof operation
Product: [Eclipse Project] JDT Reporter: Daniel Lew <daniel.lew>
Component: CoreAssignee: Maxime Daniel <maxime_daniel>
Status: RESOLVED DUPLICATE QA Contact:
Severity: normal    
Priority: P3    
Version: 3.3.1   
Target Milestone: 3.3   
Hardware: PC   
OS: Linux   
Whiteboard:

Description Daniel Lew CLA 2007-08-06 13:53:25 EDT
Build ID: I20070625-1500

Steps To Reproduce:
1. In Window->Preferences->Java->Compiler->Errors/Warnings->Unnecessary Code->Redundant null checks, set it to warning/error.
2. Try out this class in Eclipse:

public class Test {

	public static void main(String[] args) {
		Object foo = randomObject();
		
		//gives null comparison always yields false warning/error below
		if(!(foo instanceof String) || foo == null) { 
			System.out.println("bar");
		}
	}
	
	public static Object randomObject() {
		if(Math.random() < .5)
			return new String();
		
		return null;
	}
}

3. Observe the warning/error on the "foo == null" check, stating that foo must be null.  This isn't true at all; in fact, an instanceof operation will give false if foo is null.

More information:
Comment 1 Maxime Daniel CLA 2007-08-13 03:19:07 EDT
Reproduced, will investigate.
Comment 2 Maxime Daniel CLA 2007-09-10 04:09:42 EDT
Revisiting.

If foo was null, then foo instanceof String would yield false, !(foo instanceof String) would yield true, and the second member of the || expression would not get evaluated at all. This is reflected by the message, which, for 3.4 M1, states that 'The variable foo cannot be null at that [the comparison] location' (instead of the 'foo must be null' that the bug submitter observed.

Checked 3.3.1 again, which has the same behavior.

I would contend that our current behavior is correct. Which already was the conclusion of bug 145202, of which this one happens to be a duplicate.


*** This bug has been marked as a duplicate of bug 145202 ***