Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8286377

Add javac lint warning for possibly lossy conversion in compound assignments

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 20
    • tools
    • None
    • behavioral
    • minimal
    • New warning.
    • add/remove/modify command line option
    • JDK

      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.

            asotona Adam Sotona
            webbuggrp Webbug Group
            Jan Lahoda
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: