Community
Participate
Working Groups
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.
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.
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.
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
Verified for 3.4M6 using I20080324-1300.