Skip to content

Commit

Permalink
Handle arrow switches in MissingDefault
Browse files Browse the repository at this point in the history
Fixes #2256

PiperOrigin-RevId: 376482444
  • Loading branch information
cushon authored and Error Prone Team committed Jun 1, 2021
1 parent 6fe895e commit fed5957
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
Expand Up @@ -30,8 +30,10 @@
import com.google.errorprone.util.ASTHelpers;
import com.google.errorprone.util.Reachability;
import com.sun.source.tree.CaseTree;
import com.sun.source.tree.StatementTree;
import com.sun.source.tree.SwitchTree;
import com.sun.tools.javac.code.Type;
import java.util.List;
import java.util.Optional;
import javax.lang.model.element.ElementKind;

Expand Down Expand Up @@ -62,8 +64,10 @@ public Description matchSwitch(SwitchTree tree, VisitorState state) {
// for the switch statement. Hopefully we don't often see switches with zero cases.
CaseTree lastCase = getLast(tree.getCases());
String replacement;
if (lastCase.getStatements().isEmpty()
|| Reachability.canCompleteNormally(Iterables.getLast(lastCase.getStatements()))) {
List<? extends StatementTree> statements = lastCase.getStatements();
if (statements == null
|| statements.isEmpty()
|| Reachability.canCompleteNormally(Iterables.getLast(statements))) {
replacement = "\nbreak;\ndefault: // fall out\n";
} else {
replacement = "\ndefault: // fall out\n";
Expand All @@ -73,7 +77,8 @@ public Description matchSwitch(SwitchTree tree, VisitorState state) {
return description.build();
}
CaseTree defaultCase = maybeDefault.get();
if (!defaultCase.getStatements().isEmpty()) {
List<? extends StatementTree> statements = defaultCase.getStatements();
if (statements != null && !statements.isEmpty()) {
return NO_MATCH;
}
// If `default` case is empty, and last in switch, add `// fall out` comment
Expand Down
Expand Up @@ -17,9 +17,11 @@
package com.google.errorprone.bugpatterns;

import static com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH;
import static org.junit.Assume.assumeTrue;

import com.google.errorprone.BugCheckerRefactoringTestHelper;
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 @@ -225,4 +227,40 @@ public void multipleStatementsInGroup() {
"}")
.doTest(TEXT_MATCH);
}

@Test
public void arrowSwitch() {
assumeTrue(RuntimeVersion.isAtLeast14());
compilationHelper
.addSourceLines(
"Test.java",
"class Test {",
" void m(int i) {",
" // BUG: Diagnostic contains:",
" switch (i) {",
" case 1 -> {}",
" case 2 -> {}",
" }",
" }",
"}")
.doTest();
}

@Test
public void arrowSwitchNegative() {
assumeTrue(RuntimeVersion.isAtLeast14());
compilationHelper
.addSourceLines(
"Test.java",
"class Test {",
" void m(int i) {",
" switch (i) {",
" case 1 -> {}",
" case 2 -> {}",
" default -> {} // fall out",
" }",
" }",
"}")
.doTest();
}
}

0 comments on commit fed5957

Please sign in to comment.