You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Description: PMD reports "Unnecessary explicit conversion from Integer to Long" violation, but removing the explicit conversion will break compilation.
Code Sample demonstrating the issue:
@SuppressWarnings({"PMD.SystemPrintln", "PMD.NoPackage"})
publicfinalclassUnnecessaryBoxing {
privateUnnecessaryBoxing() {
}
publicstaticvoidaddLong(Longparameter) {
System.out.println("parameter = " + parameter);
}
publicstaticIntegergetValue() {
return42;
}
publicstaticvoidmain(String[] args) {
addLong(Long.valueOf(getValue())); // PMD complains: "Unnecessary explicit conversion from Integer to Long"//addLong(getValue()); // Does not compile: "incompatible types: Integer cannot be converted to Long"
}
}
Expected outcome:
PMD reports a violation at line 16 (see comment), but that's wrong. That's a false positive.
Running PMD through:Gradle
The text was updated successfully, but these errors were encountered:
That's because your code is doing the conversion chain Integer ~> int ~> long -> Long, where the first two are implicit. You can "simplify" that by writing getValue().longValue() and not call Long.valueOf. That way you do Integer -> long ~> Long. I haven't tested that this satisfies the rule but I believe it would.
Although the rule is still definitely wrong because the conversion is not unnecessary in this case.
Affects PMD Version: 7.0.0
Rule: UnnecessaryBoxing
Description: PMD reports "Unnecessary explicit conversion from Integer to Long" violation, but removing the explicit conversion will break compilation.
Code Sample demonstrating the issue:
Expected outcome:
PMD reports a violation at line 16 (see comment), but that's wrong. That's a false positive.
Running PMD through: Gradle
The text was updated successfully, but these errors were encountered: