GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] Fix #6611: UnnecessaryVarargsArrayCreation ignores overload ambiguity by fudianchn · Pull Request #6949 · pmd/pmd

Conversation

@fudianchn

@fudianchn fudianchn commented Aug 6, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@pmd-actions-helper

pmd-actions-helper Bot commented Aug 8, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

UncleOwen
UncleOwen requested changes Aug 11, 2026
View reviewed changes
Comment thread ...va/net/sourceforge/pmd/lang/java/rule/bestpractices/UnnecessaryVarargsArrayCreationRule.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread ...va/net/sourceforge/pmd/lang/java/rule/bestpractices/UnnecessaryVarargsArrayCreationRule.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread ...va/net/sourceforge/pmd/lang/java/rule/bestpractices/UnnecessaryVarargsArrayCreationRule.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@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
UncleOwen
UncleOwen requested changes Aug 12, 2026
View reviewed changes
Comment thread ...va/net/sourceforge/pmd/lang/java/rule/bestpractices/UnnecessaryVarargsArrayCreationRule.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread ...va/net/sourceforge/pmd/lang/java/rule/bestpractices/UnnecessaryVarargsArrayCreationRule.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@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 approved these changes Aug 14, 2026
View reviewed changes

@UncleOwen UncleOwen left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thanks!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@UncleOwen

UncleOwen commented Aug 14, 2026

Copy link
Copy Markdown
Member

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@UncleOwen
UncleOwen force-pushed the fix/unnecessaryvarargs-overload-ambiguity-6611 branch from f20c5a1 to 112424e Compare August 14, 2026 10:36
@fudianchn @UncleOwen
[java] UnnecessaryVarargsArrayCreation: suppress when removing array …
f66d5dc
…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

UncleOwen commented Aug 14, 2026

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

UncleOwen added 2 commits August 14, 2026 12:42
@UncleOwen
Merge branch 'main' into fork/fudianchn/fix/unnecessaryvarargs-overlo…
5f05c2d
…ad-ambiguity-6611

# Conflicts:
#	pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UnnecessaryVarargsArrayCreationRule.java
@UncleOwen
import order
15d52a8
@UncleOwen
UncleOwen merged commit f722cfb into pmd:main Aug 14, 2026
11 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@Eljees Eljees mentioned this pull request Sep 24, 2026
[java] UnnecessaryVarargsArrayCreation: add regression tests for #5069 #7103
Merged
2 of 4 tasks
@UncleOwen UncleOwen mentioned this pull request Sep 25, 2026
[java] UnnecessaryVarargsArrayCreation should not warn when a method is overloaded #5069
Closed
@saryn17 saryn17 mentioned this pull request Oct 2, 2026
[java] Fix #7134, #7135: UnnecessaryBoxing: check whether another overload is applicable without the unboxing #7136
Draft
3 of 4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@UncleOwen UncleOwen UncleOwen approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

7.27.0

Development

Successfully merging this pull request may close these issues.

[java] UnnecessaryVarargsArrayCreation: False positive when removing array creates overload ambiguity

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@fudianchn @UncleOwen
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.