Skip to content

[java] Make UnnecessaryCast check casts in invocation contexts #3219

Open

Description

@oowekyala

Is your feature request related to a problem? Please describe. The pmd 7 rule UnnecessaryCast, as reimplemented in #3204 (refs #3218), ignores casts that occur in an invocation context (method arguments). This is because those casts may help select the correct overload. For instance

class MyClass {
     void m(int i) {}
     void m(Integer i) {}
     void m(Object o) {}
     
     {
         m(1); // select m(int)
         m((int) 1); // select m(int), unnecessary
         m((Integer) 1); // select m(Integer), cast is necessary
         m((Object) Integer.valueOf(2)); // select m(Object) cast is necessary
     }
}

A real world example are these casts: https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/AbstractJavaRule.java#L326-L338

In this different example, some casts necessary above are now unnecessary.

class MyClass {
     void m(int i) {}
     
     {
         m(1);   // select m(int)
         m((int) 1); // unnecessary
         m((Integer) 1); // unnecessary as the selected overload is anyway `int`
     }
}

Describe the solution you'd like Remove the FNs. We could at least easily do this when there is a single potentially applicable method. In other cases, we will have to use the overload resolution facilities of pmd 7.

Activity

  1. added
    an:enhancement
    a:false-negative
    on Apr 13, 2021
  2. added this to the 7.0.0 milestone on Apr 13, 2021
  3. modified the milestones: 7.0.0, 7.x on Jan 23, 2023
  4. added
    needs:pmd7-revalidation
    on Mar 17, 2024
  5. removed
    needs:pmd7-revalidation
    on Apr 2, 2024
  6. added a commit that references this issue on Apr 2, 2024
  7. jsotuyod commented on Apr 2, 2024

    @jsotuyod
    Member

    PMD will only fail to detect the very last scenario as it stands.

    The test cases are already in the code (even if one is disabled)

    <test-code>
    <description>Unnecessary cast to detect invocation part 1 - #3219</description>
    <expected-problems>1</expected-problems>
    <expected-linenumbers>8</expected-linenumbers>
    <code><![CDATA[
    class MyClass {
    void m(int i) {}
    void m(Integer i) {}
    void m(Object o) {}
    {
    m(1); // select m(int)
    m((int) 1); // select m(int), unnecessary
    m((Integer) 1); // select m(Integer), cast is necessary
    m((Object) Integer.valueOf(2)); // select m(Object) cast is necessary
    }
    }
    ]]></code>
    </test-code>
    <test-code disabled="true">
    <!-- currently disabled, the rule fails to detect the violation on line 7 -->
    <description>Unnecessary cast to detect invocation part 2 - #3219</description>
    <expected-problems>2</expected-problems>
    <expected-linenumbers>6,7</expected-linenumbers>
    <code><![CDATA[
    class MyClass {
    void m(int i) {}
    {
    m(1); // select m(int)
    m((int) 1); // unnecessary
    m((Integer) 1); // unnecessary as the selected overload is anyway `int`
    }
    }
    ]]></code>
    </test-code>

    The rule also includes a TODO to include this:

    if (context.hasKind(ExprContextKind.INVOCATION)) {
    // todo unsupported for now, the cast may be disambiguating overloads
    return false;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-negativean:enhancement

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions