Skip to content

[java] UnnecessaryBoxing: False positive when removing the conversion selects a more specific primitive overload #7135

Open

Description

@saryn17

Affects PMD Version: 7.28.0 (also 7.16.0)

Rule: UnnecessaryBoxing
https://docs.pmd-code.org/latest/pmd_rules_java_codestyle.html#unnecessaryboxing

Description:

With an Integer value, Math.abs(value.longValue()) calls abs(long). Without longValue(), javac selects abs(int). All four abs overloads are applicable in phase 2 (JLS 15.12.2.3), and abs(int) is the most specific (JLS 15.12.2.5). For Integer.MIN_VALUE the result changes from 2147483648 to -2147483648.

The overload check from #5971 only suppresses the warning for overloads with a reference type, so it does not cover this case. PMD 7.16.0 reports the call too.

Code Sample demonstrating the issue:

class Example {
    long test(Integer value) {
        return Math.abs(value.longValue());
    }
}

PMD reports Unnecessary explicit conversion from Integer to long at line 3.

Expected outcome:

PMD reports a violation at line 3, but that's wrong. That's a false positive.

Running PMD through: Other (PmdAnalysis API)

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions