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

Don't crash on 'arrow' switches in FallThrough #2124

Merged
merged 1 commit into from Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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();
}
}