Summary
Add a new lint option, lossy-conversions
, to javac
to warn about type casts in compound assignments with possible lossy conversions.
Problem
If the type of the right-hand operand of a compound assignment is not assignment compatible with the type of the variable, a cast is implied and possible lossy conversion may silently occur. While similar situations are resolved as compilation errors for primitive assignments, there are no similar rules defined for compound assignments. Following simple assignments cause compilation errors:
error: incompatible types: possible lossy conversion from double to int
int i = 0; i = i * Double.NaN;
^
error: incompatible types: possible lossy conversion from int to byte
byte b = 257;
^
While following compound assignments compile without any error or warning:
int i = 0; i *= Double.NaN;
byte b = 0; b += 257;
Solution
Warn about possible lossy compound assignments conversions in situations where primitive assignments would fail to compile with possible lossy conversion error.
Specification
New text displayed as a key for javac
's -Xlint
:
lossy-conversions Warn about possible lossy conversions in compound assignment.
- csr of
-
JDK-8244681 Add a warning for possibly lossy conversion in compound assignments
- Resolved