Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid MemberName IOOBE on lambda parameters inside overriding methods #3976

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially I wrote a fix that tests for index < 0 below, but then malformed lambda parameter names aren't flagged. I updated the test accordingly. This does make me wonder: are there possibly other expression types that should be excluded? (Nothing comes to mind, but.)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix!

IIUC symbol.owner for the lambda parameter is the syntactically enclosing method (toString in the test below), which is unfortunate. I can't think of any non-lambda cases that seem likely to be affected by this either.

I wondered about explicitly handling the case where the parameter symbol isn't in the owning method symbol's parameter list, vs. checking if the parent is a lambda, but either way is probably fine for now.

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();
}
}