Repository navigation
[java] Fix #6409: UnnecessaryCast false positive for anonymous class - #7193
Merged
adangel merged 2 commits intoOct 29, 2026
Merged
Conversation
PMD types an anonymous class creation like `new Foo() {}` as `Foo`,
so the rule saw `(Foo) new Foo() {}` as an identity cast and reported
it. The type of the expression is the anonymous class, a strict subtype
of `Foo`. In a lambda returned to a generic method such as
`stream.map(i -> (Foo) new Foo() {}).toList()` the cast decides the
inferred type, and removing it does not compile.
The rule now uses the anonymous class type for the operand, so these
casts follow the same checks as casts of any other subtype.
Contributor
|
Compared to main: (comment created at 2026-10-17 20:50:37+00:00 for 02cc797) |
zbynek
approved these changes
Oct 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe the PR
UnnecessaryCastreports the cast inbut the cast is needed. Without it, javac infers
Stream<anonymous Foo>and fails withList<<anonymous Foo>> cannot be converted to List<Foo>.Cause: PMD gives
new Foo() {}the typeFoo, the instantiated type, not the anonymousclass. So
operandType.equals(coercionType)inisCastUnnecessaryis true, and the castis reported as an identity cast before the existing lambda-return check (#5073, #5440)
can run.
Fix: when the operand is an anonymous class creation, the rule uses
getAnonymousClassDeclaration().getTypeMirror()as the operand type. The unresolved-typeearly return still runs on the original type first. After that the cast goes through the same
checks as a cast of a named subtype (
(Foo) new FooImpl()), which is already not reportedin this position.
Behaviour change to be aware of: a cast of an anonymous class in a lambda return position
with an explicit target, e.g.
Function<Integer, Foo> f = i -> (Foo) new Foo(){};, is nolonger reported. That matches what the rule already does for
i -> (Foo) new FooImpl()andi -> (Object) "s"onmain. Casts in assignmentcontexts (
Foo f = (Foo) new Foo(){};,Runnable r = (Runnable) new Runnable() {...};)are still reported. A new test covers those.
Tests added to
UnnecessaryCast.xml:#6409 False positive for anonymous class in lambda return of generic method: theStream.map(...).toList()case from the issue, plus anOptional.map(...).orElse(...)variant. Expects 0 violations. I compiled both with javac 21: they compile with the
casts and fail without them.
#6409 Unnecessary cast of anonymous class to its supertype: three assignment-contextcasts that should still be reported.
Related issues
Ready?
pmd-javamodule was run locally; see below)./mvnw clean verifypasses (checked automatically by github actions)What was run locally (macOS, JDK 21.0.12):
./mvnw -pl pmd-java test -Dtest=UnnecessaryCastTest: the new FP test fails without thefix (
expected: <0> but was: <2>) and passes with it../mvnw -pl pmd-java verify: BUILD SUCCESS, 10319 tests, 0 failures, 21 skipped.0 Checkstyle violations. The PMD self-check reports the same 6 existing
AlwaysCallSuperWhenNotUsingRuleChainwarnings asmain(none in the changed file).japicmp ran. On
mainat 66860e2 the same command gives 10317 tests and the same6 warnings.
./mvnw clean verify, and the pmd-regression-tester.The latter is the one that would show the effect on real-world projects.
The release notes have an entry under java-codestyle.