Skip to content

[java] Fix #6393: UnusedPrivateMethod FP on overloaded methods when overload resolution fails - #6947

Merged
UncleOwen merged 2 commits into
pmd:mainfrom
fudianchn:fix/unusedprivatemethod-overload-6393
Aug 14, 2026
Merged

UncleOwen merged 2 commits into
pmd:mainfrom
fudianchn:fix/unusedprivatemethod-overload-6393

Conversation

@fudianchn

Copy link
Copy Markdown
Contributor

What

Suppress the UnusedPrivateMethod false positive on an overloaded private method when overload resolution for a call to it fails (e.g. when an argument type is not on the auxclasspath).

Why

When overload resolution fails, PMD falls back to an arbitrary candidate symbol. The rule treated that fallback as the real selection and marked the other overloads as unused. Closes #6393.

Root cause

UnusedPrivateMethodRule only special-cased calledMethod.isUnresolved(). On a failed overload resolution (selectionInfo.isFailed(), with calledSymbol.isUnresolved() == false) the rule kept the fallback symbol as the chosen overload, so the real overload(s) were reported as unused.

How

Treat a failed selection like an unresolved call:

if (calledMethod.isUnresolved() || selectionInfo.isFailed()) {
    handleUnresolvedCall(ref, selectionInfo, candidates);
    return;
}

This reuses the existing whitelist path — handleUnresolvedCall already guards cross-class receivers via receiverMayBeInstanceOfThisClass — so no new abstraction or framework-level change is needed.

Testing

  • New case in UnusedPrivateMethod.xml reproducing the issue (expected 0 problems).
  • Verified it fails on the old code (UnusedPrivateMethod: 'transform(Long)') and passes after the fix.
  • ./mvnw -pl pmd-java -am test -Dtest=UnusedPrivateMethodTest -Dsurefire.failIfNoSpecifiedTests=false -DfailIfNoTests=false → Tests run: 111, Failures: 0, Errors: 0 (no regression; 2 pre-existing skipped).

Notes

  • This trades a possible false negative for the existing false positive, matching the rule's existing stance for isUnresolved() calls: when in doubt, do not report. As @oowekyala noted on the issue, the cleanest user-side fix is a correct auxclasspath; this change keeps the rule safe when that configuration is incomplete.

Fixes #6393

…n overload resolution fails

What:
  UnusedPrivateMethod reported a false positive on one of several overloaded
  private methods when a call's argument is the return value of a method
  declared in a type that is not on the auxiliary classpath (unresolved
  receiver/return type), causing overload resolution to fail.

Root cause:
  When overload resolution fails (OverloadSelectionResult#isFailed()), the
  selected method symbol is an arbitrary fallback, not the actually-called
  overload. The rule only branched into the conservative "whitelist all
  same-named candidates" path when the symbol was unresolved
  (JExecutableSymbol#isUnresolved()). A failed-but-resolved fallback
  therefore caused only that one overload to be marked used, flagging the
  remaining overload(s) as unused.

Fix:
  Treat a failed overload selection like an unresolved call, so all
  same-named private method candidates are whitelisted when the receiver
  may be an instance of the analysed class (existing handleUnresolvedCall
  logic). This is consistent with the rule's existing false-positive-avoiding
  trade-off for unresolved calls.

Testing:
  New test case in UnusedPrivateMethod.xml reproducing the issue (expects 0
  violations). Full UnusedPrivateMethodTest: 111 tests, 0 failures, 0 errors
  (2 pre-existing skipped).
Signed-off-by: 付典 <[email protected]>
@pmd-actions-helper

pmd-actions-helper Bot commented Aug 7, 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 0 violations, 16 errors and 9 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-08-14 11:51:01+00:00 for 05e0f8d)

@fudianchn
fudianchn marked this pull request as draft August 12, 2026 00:37
@fudianchn
fudianchn marked this pull request as ready for review August 12, 2026 16:21
…oad-6393

# Conflicts:
#	docs/pages/release_notes.md
@UncleOwen UncleOwen added this to the 7.27.0 milestone Aug 14, 2026

@UncleOwen UncleOwen 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!

@UncleOwen
UncleOwen merged commit 6dcf9f1 into pmd:main Aug 14, 2026
15 of 22 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] UnusedPrivateMethod: False positive with overloaded private methods called with values returned from methods defined in other classes

2 participants