Repository navigation
[java] Fix #5158: SingletonClassReturningNewInstance: a local holding the existing instance is not new - #7109
Merged
Conversation
…ng instance is not new Report a returned local variable only when a value assigned to it is a constructor call, looking through casts, chained assignments and conditional expressions. Fixes pmd#5158
symbol.tryGetNode() already infers ASTVariableId from the assignment target (see ConstantFolder, InternalApiBridge and others for the same pattern); the explicit cast tripped the UnnecessaryCast self-check. Signed-off-by: Eljees <[email protected]>
Signed-off-by: Eljees <[email protected]>
…ningNewInstance ParameterizedMetricKey#getInstance uses the standard pool/putIfAbsent pattern this rule now recognizes as returning an existing instance (see the parent commit's fix). PMD's own dogfood run flags the suppression as dead via UnnecessaryWarningSuppression once the false positive here is gone. Signed-off-by: Eljees <[email protected]>
…icKey#getInstance The released PMD that checks pmd-core in the verify job still reports a returned local here, while the snapshot dogfood run reports a suppression for it as unnecessary. Returning the cast lookup directly satisfies both, so no suppression is needed. Behaviour is unchanged. Signed-off-by: Eljees <[email protected]>
Contributor
|
Compared to main: (comment created at 2026-10-23 14:06:12+00:00 for d190b3b) |
adangel
approved these changes
Oct 23, 2026
adangel
left a comment
Member
There was a problem hiding this comment.
Thanks, looks good.
I'll revert your changes in ParameterizedMetricKey and exclude that file from pmd-dogfood run. So that only the SuppressWarnings for PMD.SingletonClassReturningNewInstance is removed.
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
SingletonClassReturningNewInstancereported everygetInstance()that returns a local variable, unless the local takes part in one of the two double-assignment forms from #932. So the code from #5158, which only casts the existing instance through a local to attach@SuppressWarnings("unchecked")to it, was reported:while
return (ObjectArrayValidator<T, E>) INSTANCE;is not.With this change a returned local is reported only when a value assigned to it, in its initializer or in a later assignment, is a constructor call. Casts are looked through, as are chained assignments (
a = b = new Foo()) and both branches of a conditional expression. The #932 exemptions are applied first, unchanged. An unresolved variable keeps the old behaviour and is reported.Tests
Three cases in
SingletonClassReturningNewInstance.xml:mainwithExpected 0 problem(s), 1 problem(s) found;new Singleton()in a branch: still reported;instance != null ? instance : (Object) new Singleton(): still reported, so a cast or a conditional does not hide a new instance.The existing cases, including both #932 variants, pass unchanged.
SingletonClassReturningNewInstanceTest: 8 tests, 0 failures.Change in pmd-core
ParameterizedMetricKey#getInstancereturned its pooled key through a local and suppressedSingletonClassReturningNewInstancefor it. With this fix the snapshot used by the dogfood job no longer reports it, so it flags the suppression as unnecessary. The released PMD used by theverifyjob still reports the local without it. The method now returns the cast lookup directly and needs no suppression, which satisfies both. Behaviour is unchanged.Related issues
Ready?
./mvnw clean verifypasses (checked automatically by github actions)