GeezerVison is showing github.com — [java] Fix #6611: UnnecessaryVarargsArrayCreation ignores overload ambiguity by fudianchn · Pull Request #6949 · pmd/pmd
Conversation
fudianchn
commented
Aug 6, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
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.
|
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)
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Why does GitHub think there are merge conflicts, when I just merged main into this branch???
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
…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]>
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.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
…ad-ambiguity-6611
# Conflicts:
# pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UnnecessaryVarargsArrayCreationRule.java
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
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.
|