Bug 246328 - RFE: Compiler Warning: unnecessary lower casting
Summary: RFE: Compiler Warning: unnecessary lower casting
Status: VERIFIED WORKSFORME
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.4   Edit
Hardware: PC Windows XP
: P3 enhancement (vote)
Target Milestone: 3.5 M2   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-09-05 03:38 EDT by Ahmed Ashour CLA
Modified: 2008-09-15 09:43 EDT (History)
1 user (show)

See Also:


Attachments

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