Community
Participate
Working Groups
Hi, i think that the "unnecessary casts" feature of the compiler is beautifull, but let's say we have 2 int variables and a long variable. int a, b; long d; this statement will cause a warning: d = (long)a; That's 100% correct, but this will cause a warning too: d = (long)a + b; but the cast will rise the precision that used to calculate the sum, so it is not unnecessary but very important! As far as i know, this cast would be an unnecessary again: d = d + a + (long)b because the expression is evaluated from left to right, and the addition of b will be an addition to a long-value resulting in a long-value again. To work araound the above issue, one can write d = 0L+a+b or d = 1L*a*b to get long-precision for the whole expression without a cast, but i wonder if this is correctly compiled and optimized (i don't want to see something like "add 0" or "multiply with 1" in the bytecode)
In any situation where the generated code would be altered, the warning shouldn't occur. Thanks for pointing out this defect.
For the following program: public class X { public static void main(String[] args) { int a = 0, b = 1; long d; d = (long)a; // unnecessary d = (long)a + b; // necessary d = d + a + (long)b; // unnecessary } } I am exactly obtaining 2 warnings: ---------- 1. WARNING in ...\X.java (at line 6) d = (long)a; ^^^^^^^ Unnecessary cast to type long for expression of type int ---------- ---------- 2. WARNING in ...\X.java (at line 8) d = d + a + (long)b; ^^^^^^^ Unnecessary cast to type long for expression of type int. It is already compatible with the argument type long ---------- 2 problems (2 warnings) Do you see anything wrong with them ? Or is it ok to close ?
Well, Eclipse warned me about some casts that were necessary. I will reopen this Bug again if i have found how to reproduce it.
Ok, please find reproduceable steps and reopen. Closing for now
Well, my fault. I took a deeper look at the code and found out, that Eclipse was right.
No problem, thanks for confirming this point.