From 7c1c99bf2c62114b8be65b90e5c406921c2e2078 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Sat, 17 Jun 2023 14:22:47 +0200 Subject: [PATCH 1/2] Avoid `MemberName` IOOBE on lambda parameters inside overriding methods --- .../errorprone/bugpatterns/MemberName.java | 7 ++++++- .../errorprone/bugpatterns/MemberNameTest.java | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/MemberName.java b/core/src/main/java/com/google/errorprone/bugpatterns/MemberName.java index 15b7bee933f..174085c28b4 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/MemberName.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/MemberName.java @@ -49,6 +49,8 @@ import com.google.errorprone.suppliers.Supplier; import com.google.errorprone.util.ASTHelpers; import com.sun.source.tree.MethodTree; +import com.sun.source.tree.Tree; +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; @@ -143,7 +145,10 @@ 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 + && ASTHelpers.findEnclosingNode(state.getPath(), Tree.class).getKind() + != Kind.LAMBDA_EXPRESSION) { var methodSymbol = (MethodSymbol) symbol.owner; int index = methodSymbol.getParameters().indexOf(symbol); var maybeSuper = ASTHelpers.streamSuperMethods(methodSymbol, state.getTypes()).findFirst(); diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/MemberNameTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/MemberNameTest.java index d12764996c0..4a4438ee603 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/MemberNameTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/MemberNameTest.java @@ -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 f = foo_bar -> foo_bar;", + " return f.apply(\"foo\");", + " }", + "}") + .doTest(); + } } From 28a9d48e81fc65a601003582182e39e1b3a17c36 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Sat, 17 Jun 2023 16:18:36 +0200 Subject: [PATCH 2/2] Simpler solution --- .../java/com/google/errorprone/bugpatterns/MemberName.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/MemberName.java b/core/src/main/java/com/google/errorprone/bugpatterns/MemberName.java index 174085c28b4..5c2f63e7ae0 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/MemberName.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/MemberName.java @@ -49,7 +49,6 @@ import com.google.errorprone.suppliers.Supplier; import com.google.errorprone.util.ASTHelpers; import com.sun.source.tree.MethodTree; -import com.sun.source.tree.Tree; import com.sun.source.tree.Tree.Kind; import com.sun.source.tree.VariableTree; import com.sun.tools.javac.code.Symbol; @@ -147,8 +146,7 @@ public Description matchVariable(VariableTree tree, VisitorState state) { String name = tree.getName().toString(); if (symbol.owner instanceof MethodSymbol && symbol.getKind() == ElementKind.PARAMETER - && ASTHelpers.findEnclosingNode(state.getPath(), Tree.class).getKind() - != Kind.LAMBDA_EXPRESSION) { + && 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();