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

Lint option for redundant casts

    Details

    • Subcomponent:
    • Resolved In Build:
      b55
    • CPU:
      x86
    • OS:
      linux, windows_xp
    • Verification:
      Verified

      Description

      A DESCRIPTION OF THE REQUEST :
      I would like a lint option (perhaps "-Xlint:casts") for the compiler to issue a warning where a redundant cast is made. More precisely, I would like to see a warning if the type of an expression is cast to a value that it is already assignment compatible with. Though I am more interested in this applying to reference types (see the Justification section I have provided) it could also apply to primitive types.

      JUSTIFICATION :
      This feature is almost essential to help developers migrate their code to use generics without leaving any artifacts from the dark ages before generics.

      I have been migrating our codebase to take advantage of generics, and have repeatedly found the following pattern:

        Old code:

      ArrayList list = new ArrayList(); // List of Strings
      ...
      String val = (String)list.get(0); // Cast necessary as get() returns an Object.

      Migrated code:

      ArrayList<String> list = new ArrayList<String>();
      ...
      String val = (String)list.get(0);

      Thanks to the use of a generic ArrayList, the cast is no longer necessary; however, there is a risk that you might not find and remove them all. The same problem can arise when migrating to use covariant returns.

      Stray casts lying around in the code are not only ugly, but they could potentially cause problems later if the type of the ArrayList is changed. With such a simple class hierarchy as Object-String it is difficult to demonstrate this, but with a more complicated hierarchy it is conceivable that the List might be changed to contain objects of a different type that would cause the formerly redundant cast to throw a ClassCastException at run-time.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The following lines should all generate a "redundant cast" warning with the lint option enabled:

      String val = (String)"test";
      Object val2 = (String)val;
      Object val3 = (Object)val;
      ACTUAL -
      Code compiles without warning or error.

      ---------- BEGIN SOURCE ----------

      class TestCastWarnings
      {
          String val = (String)"test";
          Object val2 = (String)val;
          Object val3 = (Object)val;
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Manually search for redundant casts. Tedious and easy to overlook instances.
      ###@###.### 2005-2-10 04:19:15 GMT

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                jjg Jonathan Gibbons
                Reporter:
                rmandalasunw Ranjith Mandala (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: