Skip to content

[java] Fix #6611: UnnecessaryVarargsArrayCreation ignores overload ambiguity - #6949

Merged
UncleOwen merged 3 commits into
pmd:mainfrom
fudianchn:fix/unnecessaryvarargs-overload-ambiguity-6611
Aug 14, 2026
Merged

UncleOwen merged 3 commits into
pmd:mainfrom
fudianchn:fix/unnecessaryvarargs-overload-ambiguity-6611

Conversation

@fudianchn

@fudianchn fudianchn commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

What

UnnecessaryVarargsArrayCreation no longer suggests removing an explicit array creation passed to a varargs parameter when removing it would make the call ambiguous between overloads.

Why

For overloads like Demo(String...) and Demo(String, String...), the call new Demo(x, new String[]{...}) is unambiguous, but removing the array creation (new Demo(x, ...)) lets the second overload match as well. The rule nonetheless told the user to remove the array, producing non-compiling code. Closes #6611.

Root cause

After matching array.getTypeMirror().equals(lastFormal), the rule fired addViolation without checking whether the "un-arrayed" actual-parameter list could be satisfied by more than one overload of the same name in scope.

How

Before reporting, enumerate the sibling overloads of the selected executable and test whether any of them is applicable to the expanded actual-parameter list (leading actuals + the array element type):

  • Constructors: JExecutableSymbol.getEnclosingClass().getConstructors() (exhaustive, since constructors are not inherited).
  • Methods: JTypeMirror.streamMethods(name) from the selected executable's declaring type, which includes overloads inherited from supertypes.

Applicability uses TypeOps.isConvertible in both fixed-arity and variable-arity modes (JLS 15.12.2.2-4); type variables are approximated by their upper bound. If a competing overload is applicable, the suggestion is suppressed.

Testing

  • 4 new cases: constructor ambiguity, method ambiguity in the same class, method ambiguity where the competing overload is inherited from a supertype, and a no-competing-overload control (still reported).
  • Verified the ambiguity cases fail on the old code (1 violation each) and pass after the fix; the control case stays at 1 violation. The inherited-overload case specifically reports with getDeclaredMethods and is suppressed with streamMethods.
  • ./mvnw -pl pmd-java -am test -Dtest=UnnecessaryVarargsArrayCreationTest -Dsurefire.failIfNoSpecifiedTests=false -DfailIfNoTests=false -> Tests run: 8, Failures: 0, Errors: 0.
  • ConfusingArgumentToVarargsMethodTest (the adjacent varargs rule) also passes: no collateral regression.
  • checkstyle: 0 violations; dogfood (pmd on pmd-java source): 0 violations.

Notes

  • Constructors are fully covered (not inherited, all overloads live in one class); this is the issue's reproducer.
  • Methods consider the whole hierarchy of the selected executable's declaring type, so a competing overload declared in a supertype is now covered.
  • Generics: type variables are approximated by their upper bound (conservative: leans towards applicable -> suppress); exact for non-generic cases (the issue scenario).

Fixes #6611

@pmd-actions-helper

pmd-actions-helper Bot commented Aug 8, 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 21 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:10:45+00:00 for 15d52a8)

@fudianchn
fudianchn marked this pull request as draft August 12, 2026 00:37
@fudianchn
fudianchn force-pushed the fix/unnecessaryvarargs-overload-ambiguity-6611 branch from 83c70e3 to bf07006 Compare August 12, 2026 05:57
@fudianchn
fudianchn marked this pull request as ready for review August 12, 2026 05:59
@fudianchn
fudianchn force-pushed the fix/unnecessaryvarargs-overload-ambiguity-6611 branch from bf07006 to 13ffc03 Compare August 12, 2026 17:40
@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

Copy link
Copy Markdown
Member

Why does GitHub think there are merge conflicts, when I just merged main into this branch???

@UncleOwen
UncleOwen force-pushed the fix/unnecessaryvarargs-overload-ambiguity-6611 branch from f20c5a1 to 112424e Compare August 14, 2026 10:36
…creates overload ambiguity

When an explicit array creation (e.g. new String[]{...}) is passed to a
varargs parameter, the rule suggested removing it. But when the callee
has another overload that is also applicable to the expanded varargs
argument list, removing the array would make the call ambiguous (or
select a different overload), so the explicit array is required for
overload resolution and the suggested fix does not compile.

Before reporting, check the sibling overloads of the selected executable:
if any of them is applicable (fixed- or variable-arity) to the argument
list obtained by expanding the array initializer into varargs elements,
suppress the violation. Constructors are enumerated from the selected
executable's own class (exhaustive, since constructors are not inherited);
methods are streamed from the declaring type via
JTypeMirror.streamMethods, which includes overloads inherited from
supertypes.

The applicability check is a conservative approximation of JLS
15.12.2.2-4 based on subtyping, treating generic parameter types by
their upper bound.

Fixes pmd#6611

Signed-off-by: 付典 <[email protected]>
@UncleOwen
UncleOwen force-pushed the fix/unnecessaryvarargs-overload-ambiguity-6611 branch from 112424e to f66d5dc Compare August 14, 2026 10:38
@UncleOwen

Copy link
Copy Markdown
Member

Why does GitHub think there are merge conflicts, when I just merged main into this branch???

Because there were new changes on main that I didn't have locally.

…ad-ambiguity-6611

# Conflicts:
#	pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UnnecessaryVarargsArrayCreationRule.java
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] UnnecessaryVarargsArrayCreation: False positive when removing array creates overload ambiguity

2 participants