Repository navigation
Conversation
Contributor
|
Compared to main: (comment created at 2026-08-14 11:10:45+00:00 for 15d52a8) |
UncleOwen
requested changes
Aug 11, 2026
fudianchn
marked this pull request as draft
August 12, 2026 00:37
fudianchn
force-pushed
the
fix/unnecessaryvarargs-overload-ambiguity-6611
branch
from
August 12, 2026 05:57
83c70e3 to
bf07006
Compare
fudianchn
marked this pull request as ready for review
August 12, 2026 05:59
UncleOwen
requested changes
Aug 12, 2026
fudianchn
force-pushed
the
fix/unnecessaryvarargs-overload-ambiguity-6611
branch
from
August 12, 2026 17:40
bf07006 to
13ffc03
Compare
Member
|
Why does GitHub think there are merge conflicts, when I just merged main into this branch??? |
UncleOwen
force-pushed
the
fix/unnecessaryvarargs-overload-ambiguity-6611
branch
from
August 14, 2026 10:36
f20c5a1 to
112424e
Compare
…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
force-pushed
the
fix/unnecessaryvarargs-overload-ambiguity-6611
branch
from
August 14, 2026 10:38
112424e to
f66d5dc
Compare
Member
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
2 of 4 tasks
3 of 4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What
UnnecessaryVarargsArrayCreationno 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...)andDemo(String, String...), the callnew 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 firedaddViolationwithout 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):
JExecutableSymbol.getEnclosingClass().getConstructors()(exhaustive, since constructors are not inherited).JTypeMirror.streamMethods(name)from the selected executable's declaring type, which includes overloads inherited from supertypes.Applicability uses
TypeOps.isConvertiblein 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
getDeclaredMethodsand is suppressed withstreamMethods../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.Notes
Fixes #6611