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

Handle arrow switches in MissingDefault #2371

Merged
merged 1 commit into from Jun 1, 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 @@ -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();
}
}