Bug 287676 - [1.5][compiler] Useless cast warning not emited
Summary: [1.5][compiler] Useless cast warning not emited
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.5   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.6 M3   Edit
Assignee: Ayushman Jain CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-08-26 06:02 EDT by utilisateur_768 CLA
Modified: 2010-03-01 14:25 EST (History)
4 users (show)

See Also:
kent_johnson: review+


Attachments
proposed fix v0.5 + regression test (4.77 KB, patch)
2009-09-23 01:28 EDT, Ayushman Jain CLA
no flags Details | Diff
proposed fix v1.0 + regression test (3.91 KB, patch)
2009-09-24 12:53 EDT, Ayushman Jain CLA
Olivier_Thomann: iplog+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description utilisateur_768 CLA 2009-08-26 06:02:43 EDT
Build id: M20090807-0800

In the following code, the cast to Integer is useless and there is no warning for that:
import java.util.Vector;

public class UselessCastWarning {
	void test() {
	      Vector<Integer> v= new Vector<Integer>(1);
	      if (((Integer) v.get(0))==1) // <= here
	          System.out.println();
	   } 
}
Comment 1 Ayushman Jain CLA 2009-09-23 01:28:20 EDT
Created attachment 147852 [details]
proposed fix v0.5 + regression test

The inconsistent behaviour was observed in cases where autoboxing took place. Added a check for that in org.eclipse.jdt.internal.compiler.ast.CastExpression#checkNeedForArgumentCasts.
Added regression test org.eclipse.jdt.core.tests.compiler.regression.CastTest#test047
Comment 2 Kent Johnson CLA 2009-09-23 11:43:09 EDT
Why add the 2 local variables : unnecessaryLeftCast & unnecessaryRightCast ?

They are only read in one place each.

But if you want to add them to make the code cleaner, then why not add each one in the local scope where its used ?


Also its confusing to define a local boolean as :

boolean unnecessaryLeftCast = (left.bits & ASTNode.UnnecessaryCast) != 0;

and then negate it :

if (!unnecessaryLeftCast && ...
Comment 3 Ayushman Jain CLA 2009-09-23 13:52:49 EDT
> Also its confusing to define a local boolean as :
> 
> boolean unnecessaryLeftCast = (left.bits & ASTNode.UnnecessaryCast) != 0;
> 
> and then negate it :
> 
> if (!unnecessaryLeftCast && ...


My intention was indeed to have a cleaner code, but yes i admit that it seems confusing. Will correct it. Are you ok with the fix otherwise?
Comment 4 Kent Johnson CLA 2009-09-24 10:09:58 EDT
yes - its fine
Comment 5 Ayushman Jain CLA 2009-09-24 12:53:59 EDT
Created attachment 148029 [details]
proposed fix v1.0 + regression test
Comment 6 Kent Johnson CLA 2009-09-25 12:58:59 EDT
Released in HEAD for 3.6M3
Comment 7 Frederic Fusier CLA 2009-10-26 10:51:23 EDT
Verified for 3.6M3 using build I20091025-2000