Skip to content

Commit

Permalink
Fix a crash in LambdaFunctionalInterfaces.
Browse files Browse the repository at this point in the history
Fixes external #3579

PiperOrigin-RevId: 497258940
  • Loading branch information
graememorgan authored and Error Prone Team committed Jan 3, 2023
1 parent b6b1b4e commit e806402
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.google.errorprone.bugpatterns;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.Streams.findLast;
import static com.google.common.collect.Streams.stream;
import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION;
import static com.google.errorprone.util.ASTHelpers.getReceiver;
import static com.google.errorprone.util.ASTHelpers.getSymbol;
Expand All @@ -25,7 +27,6 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Streams;
import com.google.errorprone.BugPattern;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher;
Expand Down Expand Up @@ -231,11 +232,11 @@ private boolean methodCallsMeetConditions(Symbol sym, VisitorState state) {
}

private static ClassTree getTopLevelClassTree(VisitorState state) {
return (ClassTree)
Streams.findLast(
Streams.stream(state.getPath().iterator())
.filter((Tree t) -> t.getKind() == Kind.CLASS))
.orElseThrow(() -> new IllegalArgumentException("No enclosing class found"));
return findLast(
stream(state.getPath().iterator())
.filter(ClassTree.class::isInstance)
.map(ClassTree.class::cast))
.orElseThrow(() -> new IllegalArgumentException("No enclosing class found"));
}

private ImmutableMultimap<String, MethodInvocationTree> methodCallsForSymbol(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,19 @@
import com.google.errorprone.BugCheckerRefactoringTestHelper;
import com.google.errorprone.BugCheckerRefactoringTestHelper.FixChoosers;
import com.google.errorprone.CompilationTestHelper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** {@link LambdaFunctionalInterface}Test */
@RunWith(JUnit4.class)
public class LambdaFunctionalInterfaceTest {
CompilationTestHelper compilationHelper;
private final CompilationTestHelper compilationHelper =
CompilationTestHelper.newInstance(LambdaFunctionalInterface.class, getClass());

private final BugCheckerRefactoringTestHelper refactoringHelper =
BugCheckerRefactoringTestHelper.newInstance(LambdaFunctionalInterface.class, getClass());

@Before
public void setUp() {
compilationHelper =
CompilationTestHelper.newInstance(LambdaFunctionalInterface.class, getClass());
}

@Test
public void testPositiveCase() {
compilationHelper.addSourceFile("LambdaFunctionalInterfacePositiveCases.java").doTest();
Expand Down Expand Up @@ -215,4 +209,18 @@ public void testRefactoringGenericToPrimitive() {
.setFixChooser(FixChoosers.FIRST)
.doTest();
}

@Test
public void onEnum() {
compilationHelper
.addSourceLines(
"E.java",
"import java.util.function.Function;",
"public enum E {",
" VALUE(String::length);",
" // BUG: Diagnostic contains:",
" E(Function<String, Integer> func) {}",
"}")
.doTest();
}
}

0 comments on commit e806402

Please sign in to comment.