Skip to content

Commit

Permalink
Don't crash on 'arrow' switches in FallThrough
Browse files Browse the repository at this point in the history
arrow switches don't fall through, so this check doesn't apply to them,
just exiting gracefully is all we need to do here.

Fixes #2118

PiperOrigin-RevId: 352602004
  • Loading branch information
cushon authored and Error Prone Team committed Jan 19, 2021
1 parent be94375 commit b84b80c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Expand Up @@ -35,6 +35,7 @@
import com.sun.source.tree.SwitchTree;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.util.Position;
import java.util.List;
import java.util.regex.Pattern;

/** A {@link BugChecker}; see the associated {@link BugPattern} annotation for details. */
Expand All @@ -57,13 +58,14 @@ public Description matchSwitch(SwitchTree tree, VisitorState state) {
break;
}
CaseTree next = it.peek();
if (caseTree.getStatements().isEmpty()) {
List<? extends StatementTree> statements = caseTree.getStatements();
if (statements == null || statements.isEmpty()) {
continue;
}
// We only care whether the last statement completes; javac would have already
// reported an error if that statement wasn't reachable, and the answer is
// independent of any preceding statements.
boolean completes = Reachability.canCompleteNormally(getLast(caseTree.getStatements()));
boolean completes = Reachability.canCompleteNormally(getLast(statements));
int endPos = caseEndPosition(state, caseTree);
if (endPos == Position.NOPOS) {
break;
Expand Down
Expand Up @@ -16,7 +16,10 @@

package com.google.errorprone.bugpatterns;

import static org.junit.Assume.assumeTrue;

import com.google.errorprone.CompilationTestHelper;
import com.google.errorprone.util.RuntimeVersion;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand Down Expand Up @@ -95,4 +98,23 @@ public void emptyBlock() {
"}")
.doTest();
}

@Test
public void arrowSwitch() {
assumeTrue(RuntimeVersion.isAtLeast14());
testHelper
.addSourceLines(
"Test.java",
"class Test {",
" enum Case { ONE, TWO }",
" void m(Case c) {",
" switch (c) {",
" case ONE -> {}",
" case TWO -> {}",
" default -> {}",
" }",
" }",
"}")
.doTest();
}
}

0 comments on commit b84b80c

Please sign in to comment.