Skip to content

Commit

Permalink
Don't crash for CaseKind.RULE in SwitchDefault
Browse files Browse the repository at this point in the history
Similar to http://e5d52dd

Fixes #2705

PiperOrigin-RevId: 413868350
  • Loading branch information
sumitbhagwani authored and Error Prone Team committed Dec 4, 2021
1 parent aa8f00b commit 1c997ab
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
Expand Up @@ -33,6 +33,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import javax.annotation.Nullable;

/** A {@link BugChecker}; see the associated {@link BugPattern} annotation for details. */
@BugPattern(
Expand All @@ -56,13 +57,13 @@ public Description matchSwitch(SwitchTree tree, VisitorState state) {
CaseTree caseTree = it.next();
defaultStatementGroup.add(caseTree);
if (caseTree.getExpression() == null) {
while (it.hasNext() && caseTree.getStatements().isEmpty()) {
while (it.hasNext() && isNullOrEmpty(caseTree.getStatements())) {
caseTree = it.next();
defaultStatementGroup.add(caseTree);
}
break;
}
if (!caseTree.getStatements().isEmpty()) {
if (!isNullOrEmpty(caseTree.getStatements())) {
defaultStatementGroup.clear();
}
}
Expand Down Expand Up @@ -119,4 +120,8 @@ public Description matchSwitch(SwitchTree tree, VisitorState state) {
}
return description.build();
}

private static <T> boolean isNullOrEmpty(@Nullable List<T> elementList) {
return elementList == null || elementList.isEmpty();
}
}
Expand Up @@ -16,14 +16,22 @@

package com.google.errorprone.bugpatterns;

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;

/** {@link SwitchDefault}Test */
@RunWith(JUnit4.class)
public class SwitchDefaultTest {

private final CompilationTestHelper compilationHelper =
CompilationTestHelper.newInstance(SwitchDefault.class, getClass());

private final BugCheckerRefactoringTestHelper testHelper =
BugCheckerRefactoringTestHelper.newInstance(SwitchDefault.class, getClass());

Expand Down Expand Up @@ -224,4 +232,63 @@ public void refactoring_outOfOrder() {
"}")
.doTest();
}

@Test
public void newNotation_validDefault() {
assumeTrue(RuntimeVersion.isAtLeast14());
compilationHelper
.addSourceLines(
"Test.java",
"class Test {",
" public static void nothing1() { }",
" public static void nothing2() { }",
" public static void nothing3() { }",
" public static void switchDefaultCrash(int i)",
" {",
" switch(i) {",
" case 0 -> nothing1();",
" case 1 -> nothing2();",
" default -> nothing3();",
" }",
" }",
"}")
.doTest();
}

@Test
public void newNotation_changeOrder() {
assumeTrue(RuntimeVersion.isAtLeast14());
testHelper
.addInputLines(
"Test.java",
"class Test {",
" public static void nothing1() { }",
" public static void nothing2() { }",
" public static void nothing3() { }",
" public static void switchDefaultCrash(int i)",
" {",
" switch(i) {",
" default -> nothing3();",
" case 0 -> nothing1();",
" case 1 -> nothing2();",
" }",
" }",
"}")
.addOutputLines(
"Test.java",
"class Test {",
" public static void nothing1() { }",
" public static void nothing2() { }",
" public static void nothing3() { }",
" public static void switchDefaultCrash(int i)",
" {",
" switch(i) {",
" case 0 -> nothing1();",
" case 1 -> nothing2();",
" default -> nothing3();",
" }",
" }",
"}")
.doTest();
}
}

0 comments on commit 1c997ab

Please sign in to comment.