Affects PMD Version: 7.2.0
With 7.1.0 the false-positive does not occur.
Rule: UnnecessaryCast
Code Sample demonstrating the issue:
package org.example; import io.vavr.collection.Array; import io.vavr.collection.Seq; import io.vavr.control.Either; public class Main { public Either<Seq<String>, String> loadAll() { return load() .mapLeft(it -> (Seq<String>) Array.of(it)) .map(foo -> "bar"); } private Either<String, String> load() { return Either.left("hello"); } }
Expected outcome: no UnnecessaryCast reported
PMD reports a violation at line 11, but that's wrong. That's a false positive.
[INFO] >>> maven-pmd-plugin:3.22.0:check (pmd) > :pmd @ pmd-unnecessary-cast-false-positive >>>
[INFO]
[INFO] --- maven-pmd-plugin:3.22.0:pmd (pmd) @ pmd-unnecessary-cast-false-positive ---
[INFO] PMD version: 7.2.0
[INFO] Rendering content with org.apache.maven.skins:maven-default-skin:jar:1.3 skin.
[INFO]
[INFO] <<< maven-pmd-plugin:3.22.0:check (pmd) < :pmd @ pmd-unnecessary-cast-false-positive <<<
[INFO]
[INFO]
[INFO] --- maven-pmd-plugin:3.22.0:check (pmd) @ pmd-unnecessary-cast-false-positive ---
[INFO] PMD version: 7.2.0
[INFO] PMD Failure: org.example.Main:11 Rule:UnnecessaryCast Priority:3 Unnecessary cast (Seq<String>).
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.453 s
[INFO] Finished at: 2024-06-18T18:09:11+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-pmd-plugin:3.22.0:check (pmd) on project pmd-unnecessary-cast-false-positive: You have 1 PMD violation.
but when I remove the cast I get a compilation failure:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:compile (default-compile) on project pmd-unnecessary-cast-false-positive: Compilation failure
[ERROR] /pmd-unnecessary-cast-false-positive/src/main/java/org/example/Main.java:[12,13] incompatible types: no instance(s) of type variable(s) U exist so that io.vavr.control.Either<io.vavr.collection.Array<java.lang.String>,U> conforms to io.vavr.control.Either<io.vavr.collection.Seq<java.lang.String>,java.lang.String>
Running PMD through:
Apache Maven 3.8.6
maven-pmd-plugin 3.22.0
Java 21
sabberworm, boris-petrov and emouty
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