Skip to content

Commit

Permalink
Don't crash for CaseKind.RULE in UnnecessaryDefaultInEnumSwitch
Browse files Browse the repository at this point in the history
Fixes #2029

PiperOrigin-RevId: 351247217
  • Loading branch information
sumitbhagwani authored and Error Prone Team committed Jan 11, 2021
1 parent ec44083 commit e5d52dd
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ public Description matchSwitch(SwitchTree switchTree, VisitorState state) {
private Description fixDefault(
SwitchTree switchTree, CaseTree caseBeforeDefault, CaseTree defaultCase, VisitorState state) {
List<? extends StatementTree> defaultStatements = defaultCase.getStatements();
if (defaultStatements == null) {
// TODO(b/177258673): provide fixes for `case -> ...`
return buildDescription(defaultCase).setMessage(DESCRIPTION_REMOVED_DEFAULT).build();
}
if (trivialDefault(defaultStatements)) {
// deleting `default:` or `default: break;` is a no-op
return buildDescription(defaultCase)
Expand Down Expand Up @@ -184,6 +188,10 @@ private Description fixUnrecognized(
List<? extends StatementTree> defaultStatements = defaultCase.getStatements();
Description.Builder unrecognizedDescription =
buildDescription(defaultCase).setMessage(DESCRIPTION_UNRECOGNIZED);
if (defaultStatements == null) {
// TODO(b/177258673): provide fixes for `case -> ...`
return unrecognizedDescription.build();
}
if (trivialDefault(defaultStatements)) {
// the default case is empty or contains only `break` -- replace it with `case UNRECOGNIZED:`
// with fall out.
Expand Down
Original file line number Diff line number Diff line change
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 @@ -779,4 +781,44 @@ public void messages() {
"}")
.doTest();
}

@Test
public void defaultCaseKindRule() {
assumeTrue(RuntimeVersion.isAtLeast14());
compilationHelper
.addSourceLines(
"Test.java",
"class Test {",
" enum Case { ONE, TWO }",
" void m(Case c) {",
" switch (c) {",
" case ONE -> {}",
" case TWO -> {}",
" // BUG: Diagnostic contains: UnnecessaryDefaultInEnumSwitch",
" default -> {}",
" }",
" }",
"}")
.doTest();
}

@Test
public void unrecognizedCaseKindRule() {
assumeTrue(RuntimeVersion.isAtLeast14());
compilationHelper
.addSourceLines(
"Test.java",
"class Test {",
" enum Case { ONE, TWO, UNRECOGNIZED }",
" void m(Case c) {",
" switch (c) {",
" case ONE -> {}",
" case TWO -> {}",
" // BUG: Diagnostic contains: UnnecessaryDefaultInEnumSwitch",
" default -> {}",
" }",
" }",
"}")
.doTest();
}
}

0 comments on commit e5d52dd

Please sign in to comment.