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 for CaseKind.RULE in SwitchDefault #2728

Merged
merged 1 commit into from
Dec 4, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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 @@ -105,7 +106,7 @@ public Description matchSwitch(SwitchTree tree, VisitorState state) {
// If the default case isn't the last case in its statement group, move it to the end.
fix.delete(defaultTree);
CaseTree lastCase = getLast(defaultStatementGroup);
if (!lastCase.getStatements().isEmpty()) {
if (!isNullOrEmpty(lastCase.getStatements())) {
fix.prefixWith(lastCase.getStatements().get(0), state.getSourceForNode(defaultTree));
} else {
fix.postfixWith(lastCase, state.getSourceForNode(defaultTree));
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
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();
}
}