Bug 246328

Summary: RFE: Compiler Warning: unnecessary lower casting
Product: [Eclipse Project] JDT Reporter: Ahmed Ashour <asashour>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: VERIFIED WORKSFORME QA Contact:
Severity: enhancement    
Priority: P3 CC: philippe_mulet
Version: 3.4   
Target Milestone: 3.5 M2   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Ahmed Ashour CLA 2008-09-05 03:38:50 EDT
Build ID: I20080617-2000

Steps To Reproduce:
It would be nice to have warning/error/ignore if unneeded casting is used.

E.g.:

class A {
  public void aMethod() {}
}

class B extends A {
  public void bMethod() {}
}

class C extends B {
  public void cMethod() {}
}


Now suppose the following:

public void test(A a) {
   ((B) a).aMethod();
   ((C) a).bMethod();// -> should be ((B) a).bMethod()
}

Both casts are not needed, note that this applies only if casting is directly used to execute method (without assignment to a local variable).
Comment 1 Philipe Mulet CLA 2008-09-05 04:29:55 EDT
Unused cast warning is already available, please check:
Preferences > Java > Compiler > Unnecessary code > Unnecessary cast or 'instanceof' operation > Warning

Note that in the specific case you entered, the cast looks useless on the surface, but will induce some subtle changes in the generated classfile (since the actual receiver type is recorded in bytecode instruction) and can alter binary compatibility. Hence the compiler will not suggest removing the cast for this very specific pattern (which is not frequent btw).

Comment 2 Philipe Mulet CLA 2008-09-05 04:30:36 EDT
Closing as worksforme.
Comment 3 Frederic Fusier CLA 2008-09-15 09:43:41 EDT
Verified for 3.5M2 using I20080914-2000.