Bug 218797 - Quickfix and cleanup yield error-prone code by removing "unnecessary" cast
Summary: Quickfix and cleanup yield error-prone code by removing "unnecessary" cast
Status: VERIFIED INVALID
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.3.1   Edit
Hardware: PC Linux
: P3 normal with 1 vote (vote)
Target Milestone: 3.4 M6   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-02-13 10:16 EST by Tobias Langner CLA
Modified: 2008-03-25 12:47 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 Tobias Langner CLA 2008-02-13 10:16:42 EST
Build ID: M20071023-1652

Steps To Reproduce:
1. Enable compiler warnings for unnecessary casts (Preferences -> Java -> Compiler -> Errors/Warnings -> "Unneccesary cast or 'instanceof' operation" -> set to Warning)

2. Create a class that contains the following methods:

   public void takeParam(int i) {
     // do something
   }

   void test() {
     char c = 'a';
*    takeParam((int) c);
   }

3. In the line marked with *, Eclipse reports "Unnecessary cast from char to int".

I don't consider this cast an unnecessary one, since - when the cast is removed - an API extension like adding a new method

   public void takeParam(char i) {
     // do something different than takeParam(int)
   }

would silently cause the code in test() to act differently whereas the version with the code would still work as before.
Comment 1 Olivier Thomann CLA 2008-02-13 12:35:36 EST
The warning is returned considering the code that is given. We cannot make any assumption on the potential changes in a framework.
When you add the new method that takes a char, the unnecessary cast is no longer reported.

Philippe, I would close as INVALID since the warning works as expected.
Comment 2 Marco Schulze CLA 2008-02-13 13:56:43 EST
IMHO Eclipse should assist the developer in writing good and robust code.

Calling a method doSomething(int) with a char variable without a cast is error-prone and definitely not robust code.

When you write a big project using hundreds of libraries, it easily happens that a new version of a library introduces additional overloaded methods. If this causes your code to malfunction, you definitely wrote bad code in the first place. I don't think that Eclipse should support such a way of coding.
Comment 3 Philipe Mulet CLA 2008-02-20 06:16:06 EST
I hear you Marco, but we cannot predict what possible extensions could occur. As Olivier said, as soon as you add another method, and recompile, it will stop warning about the cast.

If you do not recompile, the #takeParam(int) method is still going to be invoked, since bound at (previous) compile time. So your code would still be robust.

Closing as INVALID
Comment 4 Frederic Fusier CLA 2008-03-25 12:47:56 EDT
Verified for 3.4M6 using I20080324-1300.