Bug 101208

Summary: [compiler] instanceof check cannot be unnecessary on null values
Product: [Eclipse Project] JDT Reporter: Philipe Mulet <philippe_mulet>
Component: CoreAssignee: Philipe Mulet <philippe_mulet>
Status: VERIFIED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: david.hergert, maxime_daniel, ryenus
Version: 3.1   
Target Milestone: 3.1.1   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Attachments:
Description Flags
Patch for InstanceOfExpression
none
Patch for CastTest none

Description Philipe Mulet CLA 2005-06-22 04:38:26 EDT
Build 3.1rc3

The following program should not raise a warning for unnecessary instanceof
operation.

public class X {
	void foo() {
		System.out.println(null instanceof Object);
	}
}
Comment 1 Philipe Mulet CLA 2005-06-22 04:39:46 EDT
This one is simple to address, but this one is not:

public class X {
	void foo() {
                Object o = null;
		System.out.println(o instanceof Object);
	}
}
Comment 2 Philipe Mulet CLA 2005-06-22 05:00:38 EDT
Would vote to only address the obvious case (original report).
Comment 3 Philipe Mulet CLA 2005-06-22 09:40:25 EDT
Created attachment 23727 [details]
Patch for InstanceOfExpression
Comment 4 Philipe Mulet CLA 2005-06-22 09:40:50 EDT
Non critical for 3.1, will consider for 3.1.1
Comment 5 Philipe Mulet CLA 2005-06-22 09:41:21 EDT
Created attachment 23728 [details]
Patch for CastTest
Comment 6 Philipe Mulet CLA 2005-06-29 04:11:18 EDT
Fixed in 3.1 maintenance branch
Comment 7 Maxime Daniel CLA 2005-08-09 09:44:40 EDT
Verified in 3.2 M1 with build I20050808-2000.
As announced, the case of Comment #1 is not addressed.
Comment 8 David Audel CLA 2005-09-26 09:59:27 EDT
Verified using M20050923-1430 for 3.1.1
Comment 9 Philipe Mulet CLA 2005-09-29 04:38:53 EDT
*** Bug 111026 has been marked as a duplicate of this bug. ***
Comment 10 David Hergert CLA 2006-01-31 01:31:24 EST
There still exists a problem as you mentioned in Comment #1.  Is there another bug open to correct that error?  

What is the reasoning behind warning the user of an unnecessary instanceof?  Maybe it shouldn't warn the user at all because it very well may be necessary, as the code below shows.  I think the JDT should not warn me rather than incorrectly warn me.

Object o = null;
System.out.println(o instanceof Object);
                   ^^^^^^^^^^^^^^^^^^^
Comment 11 Philipe Mulet CLA 2006-01-31 08:56:32 EST
Never warning on instanceof checks would be harsh... but now we have some support for null ref analysys, we should consider some cross fertilization.
Maxime - pls enter a separate PR to track this enhancement.