Bug 101208 - [compiler] instanceof check cannot be unnecessary on null values
Summary: [compiler] instanceof check cannot be unnecessary on null values
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.1   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.1.1   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 111026 (view as bug list)
Depends on:
Blocks:
 
Reported: 2005-06-22 04:38 EDT by Philipe Mulet CLA
Modified: 2006-01-31 08:56 EST (History)
3 users (show)

See Also:


Attachments
Patch for InstanceOfExpression (929 bytes, patch)
2005-06-22 09:40 EDT, Philipe Mulet CLA
no flags Details | Diff
Patch for CastTest (1.85 KB, patch)
2005-06-22 09:41 EDT, Philipe Mulet CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
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.