Skip to content

Commit c573889

Browse files
graememorganError Prone Team
authored andcommitted
Avoid one way in which ExplicitArrayForVarargs could cause recursion.
Fixes external #5335. PiperOrigin-RevId: 830889725
1 parent 5f0cca6 commit c573889

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

core/src/main/java/com/google/errorprone/bugpatterns/ExplicitArrayForVarargs.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.errorprone.bugpatterns;
1818

1919
import static com.google.common.collect.Iterables.getOnlyElement;
20+
import static com.google.common.collect.Streams.stream;
2021
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING;
2122
import static com.google.errorprone.matchers.Description.NO_MATCH;
2223
import static com.google.errorprone.util.ASTHelpers.constValue;
@@ -64,6 +65,16 @@ private Description handle(
6465
if (args.isEmpty()) {
6566
return NO_MATCH;
6667
}
68+
// Bail out if we're calling an overload of the same method: it's likely we'll introduce
69+
// problems (delegating constructors are a common case).
70+
if (stream(state.getPath().getParentPath())
71+
.anyMatch(
72+
t ->
73+
getSymbol(t) instanceof MethodSymbol ms
74+
&& ms.owner.equals(symbol.owner)
75+
&& ms.name.contentEquals(symbol.name))) {
76+
return NO_MATCH;
77+
}
6778
// The last argument isn't substituting for varargs if it isn't in place of the varargs
6879
// parameter.
6980
if (args.size() != symbol.getParameters().size()) {

core/src/test/java/com/google/errorprone/bugpatterns/ExplicitArrayForVarargsTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,21 @@ List<Object> nonempty() {
103103
""")
104104
.doTest();
105105
}
106+
107+
@Test
108+
public void refactoringWouldCauseRecursion() {
109+
helper
110+
.addSourceLines(
111+
"Test.java",
112+
"""
113+
class Test {
114+
Test() {
115+
this(new String[0]);
116+
}
117+
118+
protected Test(String... xs) {}
119+
}
120+
""")
121+
.doTest();
122+
}
106123
}

0 commit comments

Comments
 (0)