Bug 35442 - flag unnecessary casts
Summary: flag unnecessary casts
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 2.1   Edit
Hardware: PC Windows XP
: P3 enhancement (vote)
Target Milestone: 3.0 M2   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2003-03-21 02:04 EST by Lee Breisacher CLA
Modified: 2003-07-16 07:19 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Lee Breisacher CLA 2003-03-21 02:04:58 EST
It would be nice to have a compiler option to flag unnecessary explicit type 
casts.
Comment 1 Philipe Mulet CLA 2003-03-21 04:52:56 EST
Good suggestion. Post 2.1
Comment 2 Gary Gregory CLA 2003-03-21 13:01:21 EST
Yes indeed, that would be super. 

The best reason for the feature is the case we have right now where we have 
discovered some nice interface refactoring that will clean things up greatly 
for us. But the most tedious part of the clean up is to go through our giant 
pile of code and remove the no longer needed type casts. 

Thank you for considering this request as soon as the 2.1 tree re-opens!
Comment 3 Philipe Mulet CLA 2003-03-21 13:16:54 EST
Note though that unnecessary casts are optimized out by the compiler.
Comment 4 Philipe Mulet CLA 2003-04-04 04:53:37 EST
reopen
Comment 5 Philipe Mulet CLA 2003-05-27 07:16:23 EDT
One thing to keep in mind:

String s = ...;
Thread t = ...;
Object o = bool ? s : (Object)t;

The cast isn't unnecessary (optimized out, but necessary for conditional 
operator type resolution).
Comment 6 Philipe Mulet CLA 2003-07-04 18:19:11 EDT
Other nasty case:

class X {

   void foo(String s) { ... }
   void foo(Thread t) { ... }

   void bar(Object o) {
      if (o instance String) {
        foo((String)o); // necessary cast
      } else if (o instanceof Thread){
        foo((Thread)o); // necessary cast
      }
   }
}
Comment 7 Philipe Mulet CLA 2003-07-04 18:21:20 EDT
Actually my previous case was fine, but the following is problematic:

However the following is a nasty case:

class X {

   void foo(String s) { ... }
   void foo(Object o) { ... }

   void bar() {
      foo((Object)"hello");
      foo((String) null);
   }
}
Comment 8 Philipe Mulet CLA 2003-07-07 12:48:28 EDT
Previous case also affects allocation expression, anonymous type declarations 
and explicit constructor calls.

Also numeric promotion in operators is troublesome.

Finally, instanceof operator should also diagnose unnecessary case (quite 
trivial).
Comment 9 Philipe Mulet CLA 2003-07-08 07:37:45 EDT
I am not flagging unnecessary narrowing cast, since removing these may change 
the end program behavior, however it is still useful information for end-user.

foo((String) new Object()); 
// program would have thrown ClassCastException at runtime with cast in.

void foo(Object o) {}

Will not check for this (for now)
Comment 10 Philipe Mulet CLA 2003-07-10 12:16:10 EDT
Considered narrowing scenario as well.


Added optional diagnosis for unnecessary cast or instanceof operations 
(matching problem IDs are IProblem.UnnecessaryCast, 
IProblem.UnnecessaryArgumentCast, IProblem.UnnecessaryInstanceof). 
* COMPILER / Reporting Unnecessary Type Check
*    When enabled, the compiler will issue an error or a warning when a cast or 
an instanceof operation 
*    is unnecessary.
*     - option 
id:         "org.eclipse.jdt.core.compiler.problem.unnecessaryTypeCheck"
*     - possible values:   { "error", "warning", "ignore" }
*     - default:           "ignore"

Comment 11 Philipe Mulet CLA 2003-07-11 06:03:32 EDT
Note: will not complain about unnecessary narrowing casts on receiver (message 
or field access) since these may induce change in constant pool for target >= 
1.2 (pool references contain the actual receiver type, instead of the declaring 
class).
Comment 12 David Audel CLA 2003-07-16 07:19:43 EDT
Verified.