Skip to content

Commit

Permalink
Don't try to convert recursive lambdas to methods
Browse files Browse the repository at this point in the history
In principle this is possible to get right, but it's a lot of work for a very rare code pattern. Instead, just give up on it.

PiperOrigin-RevId: 449810510
  • Loading branch information
amalloy authored and Error Prone Team committed May 19, 2022
1 parent 85f55f2 commit 9ff2d42
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,34 @@ private boolean canFix(Tree type, Symbol sym, VisitorState state) {
class Scanner extends TreePathScanner<Void, Void> {

boolean fixable = true;
boolean inInitializer = false;

@Override
public Void visitMethodInvocation(MethodInvocationTree node, Void unused) {
check(node);
return super.visitMethodInvocation(node, null);
}

@Override
public Void visitVariable(VariableTree node, Void unused) {
boolean wasInInitializer = inInitializer;
if (sym.equals(getSymbol(node))) {
inInitializer = true;
}
super.visitVariable(node, null);
inInitializer = wasInInitializer;
return null;
}

@Override
public Void visitMemberSelect(MemberSelectTree node, Void unused) {
if (inInitializer && sym.equals(getSymbol(node))) {
// We're not smart enough to rewrite a recursive lambda.
fixable = false;
}
return super.visitMemberSelect(node, unused);
}

private void check(MethodInvocationTree node) {
ExpressionTree lhs = node.getMethodSelect();
if (!(lhs instanceof MemberSelectTree)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,17 @@ public void variable_notAFunctionalInterface() {
.expectUnchanged()
.doTest();
}

@Test
public void recursiveLambda_ignored() {
testHelper
.addInputLines(
"Test.java",
"import java.util.function.Predicate;",
"class Test {",
" private static final Predicate<String> F = x -> Test.F.test(x);",
"}")
.expectUnchanged()
.doTest();
}
}

0 comments on commit 9ff2d42

Please sign in to comment.