Skip to content

Commit

Permalink
Avoid MemberName IOOBE on lambda parameters inside overriding methods
Browse files Browse the repository at this point in the history
Fixes #3976

COPYBARA_INTEGRATE_REVIEW=#3976 from PicnicSupermarket:sschroevers/MemberName-IOOBE 28a9d48
PiperOrigin-RevId: 541971277
  • Loading branch information
Stephan202 authored and Error Prone Team committed Jun 20, 2023
1 parent 0382648 commit f1226a3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import com.google.errorprone.suppliers.Supplier;
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.Tree.Kind;
import com.sun.source.tree.VariableTree;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
Expand Down Expand Up @@ -143,7 +144,9 @@ private static boolean hasTestAnnotation(MethodSymbol symbol) {
public Description matchVariable(VariableTree tree, VisitorState state) {
VarSymbol symbol = getSymbol(tree);
String name = tree.getName().toString();
if (symbol.owner instanceof MethodSymbol && symbol.getKind().equals(ElementKind.PARAMETER)) {
if (symbol.owner instanceof MethodSymbol
&& symbol.getKind() == ElementKind.PARAMETER
&& state.getPath().getParentPath().getLeaf().getKind() != Kind.LAMBDA_EXPRESSION) {
var methodSymbol = (MethodSymbol) symbol.owner;
int index = methodSymbol.getParameters().indexOf(symbol);
var maybeSuper = ASTHelpers.streamSuperMethods(methodSymbol, state.getTypes()).findFirst();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,4 +413,21 @@ public void initialismsInVariableNames_magicNamesExempt() {
"}")
.doTest();
}

@Test
public void lambdaExpressionParameterInsideOverridingMethod() {
helper
.addSourceLines(
"Test.java",
"import java.util.function.Function;",
"class Test {",
" @Override",
" public String toString() {",
" // BUG: Diagnostic contains: fooBar",
" Function<String, String> f = foo_bar -> foo_bar;",
" return f.apply(\"foo\");",
" }",
"}")
.doTest();
}
}

0 comments on commit f1226a3

Please sign in to comment.