Bug 69359

Summary: [1.5] Trouble with "unnecassary cast" warnings
Product: [Eclipse Project] JDT Reporter: Stefan Matthias Aust <sma>
Component: CoreAssignee: Philipe Mulet <philippe_mulet>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3    
Version: 3.0   
Target Milestone: 3.1 M1   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Stefan Matthias Aust CLA 2004-07-06 08:12:36 EDT
Please have a look at this code:

 import java.util.Iterator;
 import java.util.List;
 public class A {
  List list() { return null; }
  void m0() { List<A> l = list(); }
  void m1() { for (A a : list()); }
  void m2() { for (Iterator<A> i = list().iterator(); i.hasNext();); }
}

I get an unsafe operation warning in m0 and m2 and an error in m1.

Now replace "list()" with "((List<A>) list())".  Is this an attempt to get rid
of the unsafe operation warnings.  m2 doesn't raise a warning anymore.  But m0
and m1 get an "unnecassary cast" warning.  At least in the case of m1, this is a
wrong warning because if I let QuickFix remove the cast, it's an error.  In the
case of m0, I can choose between two warnings but don't have a way to get rid of
the warning :)

Now, replace "List list()" with "<X> List<X> list()".

This removes the warning from m1 but adds an "unnecessary cast" warning to m1. 
If I remove that cast, a "type mismatch" error is raised.

It also adds the same warning to m0, and if I remove that cast, Eclipse is
happy. However, I doubt that m0 is now any better than before and it seems that
I fouled the type inferer.
Comment 1 Stefan Matthias Aust CLA 2004-07-06 08:13:21 EDT
I forgot: I'm using Eclipse 3.0 final and latest jdt.core from HEAD and jdt.ui
from JDK_1.5 branch.
Comment 2 Philipe Mulet CLA 2004-07-06 12:22:54 EDT
The unnecessary cast diagnosis definitely looks like a bug.
Comment 3 Philipe Mulet CLA 2004-07-07 06:37:21 EDT
Actually, the diagnosis is fine, as conversion from raw is always legite. We 
should however issue an unsafe type operation warning.

Comment 4 Philipe Mulet CLA 2004-07-07 06:38:49 EDT
Tuned unsafe type operation warnings. Added regression tests 
GenericTypeTest#test227,test228.

Fixed.
Comment 5 Stefan Matthias Aust CLA 2004-07-12 03:45:54 EDT
This example raises two warnings now - both the unneeded cast and the unsafe
case operation.  I think, that's too much :)

import java.util.List;
public class A {
 List find() { return null; }
 void m() {
  for (A a : (List<A>) find()) {
   System.out.println(a);
  }
 }	
}
Comment 6 Philipe Mulet CLA 2004-07-12 04:56:19 EDT
I don't think it is that bad. Remember that these 2 diagnosises are 
independant one from each other. They happen to occur at same location, and 
are 2 reasons for getting rid of this cast.
Comment 7 Stefan Matthias Aust CLA 2004-07-12 14:54:13 EDT
Well, my problem is that both warnings are contradictory.  If you let QuickFix
"repair" the unneccessary cast warning, this creates an error.  OTOH, you have
to use the specialisation (and live with the other warning) because otherwise
the for loop doesn't work. Therefore, I don't think that the cast is uneccessary.