Affects PMD Version: 7.9.0 (but not 6.55.0)
Rule: UnnecessaryCast
Please provide the rule name and a link to the rule documentation: https://pmd.github.io/pmd/pmd_rules_java_codestyle.html#unnecessarycast
Description:
Casting to a more generic interface in a stream pipeline's map
method in order to satisfy the wanted generic type is reported as an uncessary cast by PMD.
Code Sample demonstrating the issue:
package test; import java.util.List; class Test { interface Parent {} interface Child extends Parent {} void test() { List<Child> children = List.of(); List<Parent> result = children.stream() .map(child -> (Parent)child) .toList(); } }
Expected outcome:
PMD reports a violation at line 12, but that's wrong. That's a false positive.
Test.java:12: UnnecessaryCast: Unnecessary cast (Parent)
Running PMD through: Gradle 8.10
jochenberger and toKrause
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4