Bug 42289 - "unnecessary casts" feature is not intelligent enough, yet
Summary: "unnecessary casts" feature is not intelligent enough, yet
Status: RESOLVED WORKSFORME
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.0   Edit
Hardware: PC All
: P3 normal (vote)
Target Milestone: 3.0 M4   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2003-08-30 07:02 EDT by Sven Köhler CLA
Modified: 2003-09-01 12:44 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sven Köhler CLA 2003-08-30 07:02:06 EDT
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)
Comment 1 Philipe Mulet CLA 2003-09-01 04:19:44 EDT
In any situation where the generated code would be altered, the warning 
shouldn't occur. Thanks for pointing out this defect.
Comment 2 Philipe Mulet CLA 2003-09-01 09:57:23 EDT
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 ?
Comment 3 Sven Köhler CLA 2003-09-01 10:35:45 EDT
Well, Eclipse warned me about some casts that were necessary. I will reopen this
Bug again if i have found how to reproduce it.
Comment 4 Philipe Mulet CLA 2003-09-01 11:24:18 EDT
Ok, please find reproduceable steps and reopen. Closing for now
Comment 5 Sven Köhler CLA 2003-09-01 12:24:41 EDT
Well, my fault.
I took a deeper look at the code and found out, that Eclipse was right.
Comment 6 Philipe Mulet CLA 2003-09-01 12:44:30 EDT
No problem, thanks for confirming this point.