From 1c997abe7dd33c725e8b652f683ac08ca7c1440f Mon Sep 17 00:00:00 2001 From: Sumit Bhagwani Date: Fri, 3 Dec 2021 00:57:40 -0800 Subject: [PATCH] Don't crash for CaseKind.RULE in SwitchDefault Similar to http://https://github.com/google/error-prone/commit/e5d52dddb7ce3861e4929a640b70e566370111f3 Fixes #2705 PiperOrigin-RevId: 413868350 --- .../errorprone/bugpatterns/SwitchDefault.java | 9 ++- .../bugpatterns/SwitchDefaultTest.java | 67 +++++++++++++++++++ 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/SwitchDefault.java b/core/src/main/java/com/google/errorprone/bugpatterns/SwitchDefault.java index 4a27a64813c..75c1d7e4375 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/SwitchDefault.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/SwitchDefault.java @@ -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( @@ -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(); } } @@ -119,4 +120,8 @@ public Description matchSwitch(SwitchTree tree, VisitorState state) { } return description.build(); } + + private static boolean isNullOrEmpty(@Nullable List elementList) { + return elementList == null || elementList.isEmpty(); + } } diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/SwitchDefaultTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/SwitchDefaultTest.java index 244245f53d0..7c11cf4d03b 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/SwitchDefaultTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/SwitchDefaultTest.java @@ -16,7 +16,11 @@ 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; @@ -24,6 +28,10 @@ /** {@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()); @@ -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(); + } }