Skip to content

[java] Fix #5158: SingletonClassReturningNewInstance: a local holding the existing instance is not new - #7109

Merged
adangel merged 10 commits into
pmd:mainfrom
Eljees:fix/5158-singleton-local-from-field
Oct 23, 2026
Merged

adangel merged 10 commits into
pmd:mainfrom
Eljees:fix/5158-singleton-local-from-field

Conversation

@Eljees

@Eljees Eljees commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Describe the PR

SingletonClassReturningNewInstance reported every getInstance() 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:

private static final ObjectArrayValidatorNoOp INSTANCE = new ObjectArrayValidatorNoOp();

public static <T, E> ObjectArrayValidator<T, E> getInstance() {
    @SuppressWarnings("unchecked")
    ObjectArrayValidator<T, E> temp = (ObjectArrayValidator<T, E>) INSTANCE;
    return temp;
}

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:

  • the code from [java] SingletonClassReturningNewInstance: False positive due to type cast #5158: no violation. Fails on main with Expected 0 problem(s), 1 problem(s) found;
  • a local initialized from the field and assigned new Singleton() in a branch: still reported;
  • a local initialized with 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#getInstance returned its pooled key through a local and suppressed SingletonClassReturningNewInstance for 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 the verify job 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?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

…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]>
…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]>
@pmd-actions-helper

pmd-actions-helper Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 8 violations, 0 errors and 0 configuration errors.
There are 0 changed duplications, 0 new duplications and 0 removed duplications.
There are 0 changed CPD errors, 0 new CPD errors and 0 removed CPD errors.

Regression Tester Report

(comment created at 2026-10-23 14:06:12+00:00 for d190b3b)

@adangel adangel changed the title [java] SingletonClassReturningNewInstance: a local holding the existing instance is not new [java] Fix #5158: SingletonClassReturningNewInstance: a local holding the existing instance is not new Oct 23, 2026

@adangel adangel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@adangel adangel added this to the 7.29.0 milestone Oct 23, 2026
@adangel
adangel merged commit 05004ef into pmd:main Oct 23, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[java] SingletonClassReturningNewInstance: False positive due to type cast

2 participants