Bug 413874 - Warning about automatic cast when using compound assignment operators
Summary: Warning about automatic cast when using compound assignment operators
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.3   Edit
Hardware: PC Mac OS X
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-07-28 03:53 EDT by Siegmar Alber CLA
Modified: 2013-07-28 07:56 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 Siegmar Alber CLA 2013-07-28 03:53:35 EDT
(This is a feature request, not a bug)

As stated in the Java Language Specification (http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.26.2) a compound assignment operator (e.g. +=, -=…) does an implicit cast to the type of the variable the value is assigned to. This is potentially not obvious to the programmer (it wasn't to me) and can cause bizarre bugs.

Quote from the link above:
	For example, the following code is correct:
		short x = 3;
		x += 4.6;
	and results in x having the value 7 because it is equivalent to:
		short x = 3;
		x = (short)(x + 4.6);

It would be nice if eclipse would issue a warning on the line "x += 4.6", i.e. if it detects that an implicit cast (caused by using a compound assignment operator) could result in loss of precision.

Build Identifier: 20130614-0229