diff --git a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/SwitchBlockHighlightingModel.java b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/SwitchBlockHighlightingModel.java index 6ba997f9df6b..9f48727a3d66 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/SwitchBlockHighlightingModel.java +++ b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/SwitchBlockHighlightingModel.java @@ -524,7 +524,6 @@ public class SwitchBlockHighlightingModel { void checkSwitchLabelValues(@NotNull HighlightInfoHolder holder) { PsiCodeBlock body = myBlock.getBody(); if (body == null) return; - boolean java20plus = PsiUtil.getLanguageLevel(holder.getProject()).isAtLeast(LanguageLevel.JDK_20_PREVIEW); MultiMap elementsToCheckDuplicates = new MultiMap<>(); List> elementsToCheckFallThroughLegality = new SmartList<>(); List elementsToCheckDominance = new ArrayList<>(); @@ -558,14 +557,9 @@ public class SwitchBlockHighlightingModel { checkDuplicates(elementsToCheckDuplicates, holder); if (holder.hasErrorResults()) return; - if (java20plus) { - HashSet alreadyFallThroughElements = new HashSet<>(); - checkFallThroughFromToPatternJava20(elementsToCheckFallThroughLegality, holder, alreadyFallThroughElements); - checkFallThroughInSwitchLabels(elementsToCheckFallThroughLegality, holder, alreadyFallThroughElements); - } - else { - checkFallThroughFromToPattern(elementsToCheckFallThroughLegality, holder); - } + HashSet alreadyFallThroughElements = new HashSet<>(); + checkFallThroughFromPatternWithSeveralLabels(elementsToCheckFallThroughLegality, holder, alreadyFallThroughElements); + checkFallThroughToPatternPrecedingCompleteNormally(elementsToCheckFallThroughLegality, holder, alreadyFallThroughElements); if (holder.hasErrorResults()) return; checkDominance(elementsToCheckDominance, holder); @@ -775,74 +769,9 @@ public class SwitchBlockHighlightingModel { return info; } - /** - * 14.11.1 Switch Blocks - *
    - * To ensure safe initialization of pattern variables fall through rules in common provide the restrictions - * of using different type of case label switchLabel: - *
  • patterns with patterns
  • - *
  • patterns with constants
  • - *
  • patterns with default
  • - *
- */ - private static void checkFallThroughFromToPattern(@NotNull List> switchBlockGroup, - @NotNull HighlightInfoHolder holder) { - if (switchBlockGroup.isEmpty()) return; - Set alreadyFallThroughElements = new HashSet<>(); - for (var switchLabel : switchBlockGroup) { - boolean existPattern = false; - boolean existsTypeTestPattern = false; - boolean existsConst = false; - boolean existsNull = false; - boolean existsDefault = false; - for (PsiSwitchLabelStatementBase switchLabelElement : switchLabel) { - if (switchLabelElement.isDefaultCase()) { - if (existPattern) { - PsiElement defaultKeyword = switchLabelElement.getFirstChild(); - addIllegalFallThroughError(defaultKeyword, "switch.illegal.fall.through.from", holder, alreadyFallThroughElements); - } - existsDefault = true; - continue; - } - PsiCaseLabelElementList labelElementList = switchLabelElement.getCaseLabelElementList(); - if (labelElementList == null) continue; - for (PsiCaseLabelElement currentElement : labelElementList.getElements()) { - if (currentElement instanceof PsiPattern || currentElement instanceof PsiPatternGuard) { - if (currentElement instanceof PsiTypeTestPattern) { - existsTypeTestPattern = true; - } - if (existPattern || existsConst || (existsNull && !existsTypeTestPattern) || existsDefault) { - addIllegalFallThroughError(currentElement, "switch.illegal.fall.through.to", holder, alreadyFallThroughElements); - } - existPattern = true; - } - else if (isNullType(currentElement)) { - if (existPattern && !existsTypeTestPattern) { - addIllegalFallThroughError(currentElement, "switch.illegal.fall.through.from", holder, alreadyFallThroughElements); - } - existsNull = true; - } - else if (isConstantLabelElement(currentElement)) { - if (existPattern) { - addIllegalFallThroughError(currentElement, "switch.illegal.fall.through.from", holder, alreadyFallThroughElements); - } - existsConst = true; - } - else if (currentElement instanceof PsiDefaultCaseLabelElement) { - if (existPattern) { - addIllegalFallThroughError(currentElement, "switch.illegal.fall.through.from", holder, alreadyFallThroughElements); - } - existsDefault = true; - } - } - } - } - checkFallThroughInSwitchLabels(switchBlockGroup, holder, alreadyFallThroughElements); - } - - private static void checkFallThroughFromToPatternJava20(@NotNull List> switchBlockGroup, - @NotNull HighlightInfoHolder holder, - @NotNull Set alreadyFallThroughElements) { + private static void checkFallThroughFromPatternWithSeveralLabels(@NotNull List> switchBlockGroup, + @NotNull HighlightInfoHolder holder, + @NotNull Set alreadyFallThroughElements) { if (switchBlockGroup.isEmpty()) return; for (var switchLabel : switchBlockGroup) { for (PsiSwitchLabelStatementBase switchLabelElement : switchLabel) { @@ -938,9 +867,9 @@ public class SwitchBlockHighlightingModel { holder.add(info.create()); } - private static void checkFallThroughInSwitchLabels(@NotNull List> switchBlockGroup, - @NotNull HighlightInfoHolder results, - @NotNull Set alreadyFallThroughElements) { + private static void checkFallThroughToPatternPrecedingCompleteNormally(@NotNull List> switchBlockGroup, + @NotNull HighlightInfoHolder results, + @NotNull Set alreadyFallThroughElements) { for (int i = 1; i < switchBlockGroup.size(); i++) { List switchLabels = switchBlockGroup.get(i); PsiSwitchLabelStatementBase firstSwitchLabelInGroup = switchLabels.get(0); diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlightingPatternsInSwitch/EffectivelyFinalWhen.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlightingPatternsInSwitch/EffectivelyFinalWhen.java index 19e4d9b81692..e08eeef2520c 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlightingPatternsInSwitch/EffectivelyFinalWhen.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlightingPatternsInSwitch/EffectivelyFinalWhen.java @@ -44,7 +44,7 @@ class Test { switch (o) { case Integer i when (switch (o) { case Integer ii when ii != mode -> 2; - case default -> 1; + default -> 1; }) == mode -> System.out.println(); default -> {} } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlightingPatternsInSwitch/FallthroughPatternMatchingSwitch.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlightingPatternsInSwitch/FallthroughPatternMatchingSwitch.java index b478d5139600..e5c9fa93ba7e 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlightingPatternsInSwitch/FallthroughPatternMatchingSwitch.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlightingPatternsInSwitch/FallthroughPatternMatchingSwitch.java @@ -2,9 +2,9 @@ class Main { void ff(Object o) { switch (o) { - case String s: + case String s: case null: - case Integer i: + case Integer i: System.out.println(i + 1); break; case Long l: @@ -29,7 +29,7 @@ class Main { void g(Object o) { switch (o) { case null: - case Integer i: + case Integer i: System.out.println(i + 1); default: throw new IllegalStateException("Unexpected value: " + o); @@ -88,8 +88,8 @@ class Main { void m(Object o) { switch (o) { - case String s: - case Integer i: + case String s: + case Integer i: System.out.println(i + 1); default: throw new IllegalStateException("Unexpected value: " + o); diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlightingPatternsInSwitch/IllegalFallthroughIn21Java.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlightingPatternsInSwitch/IllegalFallthroughIn21Java.java new file mode 100644 index 000000000000..46ebeaf3a140 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlightingPatternsInSwitch/IllegalFallthroughIn21Java.java @@ -0,0 +1,190 @@ +class IllegalFallthroughIn21Java { + + + public static void test() { + Object o = "Hello"; + switch (o) { + case String s: + System.out.println(); + case Integer i: + System.out.println(); + default: + throw new IllegalStateException("Unexpected value: " + o); + + } + } + + public static void test0() { + Object o = "Hello"; + switch (o) { + case null: + System.out.println(); + case Integer i: + System.out.println(); + default: + throw new IllegalStateException("Unexpected value: " + o); + + } + } + + public static void test2() { + Object o = "Hello"; + switch (o) { + case String s: + System.out.println(); + throw new UnsupportedOperationException(); + case Integer i: + System.out.println(); + break; + default: + throw new IllegalStateException("Unexpected value: " + o); + } + } + + public static void test3() { + String o = "Hello"; + switch (o) { + case String s when s.length() == 1: + System.out.println(); + case "1": + System.out.println(); + break; + default: + throw new IllegalStateException("Unexpected value: " + o); + } + } + + public static void test4() { + String o = "Hello"; + switch (o) { + case String s when s.length() == 1: + System.out.println(); + case "1": + System.out.println(s); + break; + default: + throw new IllegalStateException("Unexpected value: " + o); + } + } + + public static void test5() { + String o = "Hello"; + switch (o) { + case String s when s.length() == 1: + System.out.println(); + case null: + System.out.println(); + break; + default: + throw new IllegalStateException("Unexpected value: " + o); + } + } + + public static void test6() { + String o = "Hello"; + switch (o) { + case String s when s.length() == 1: + System.out.println(); + case null: + System.out.println(s); + break; + default: + throw new IllegalStateException("Unexpected value: " + o); + } + } + + public static void test7() { + Object obj = "Hello"; + switch (obj) { + case String s: + case Integer i: + System.out.println(); + } + } + + public static void test8() { + Object obj = "Hello"; + switch (obj) { + case null: + case Integer i: + System.out.println(); + } + } + + public static void test9() { + String obj = "Hello"; + switch (obj) { + case "a": + case String i when i.length() == 1: + System.out.println(); + } + } + + public static void test10() { + Object obj = "Hello"; + switch (obj) { + case null: + case Integer i: + System.out.println(); + } + } + + public static void test11() { + record R1(){} + record R2(){} + Object obj = "Hello"; + switch (obj) { + case String s: + System.out.println(s); + case R1(): + System.out.println(); + break; + default: + } + + } + + public static void test12() { + record R1(){} + record R2(){} + Object obj = "Hello"; + switch (obj) { + case String s: + System.out.println(s); + case R1(): + System.out.println(s); + break; + default: + } + + } + + public static void test13() { + record R1(){} + record R2(){} + Object obj = "Hello"; + switch (obj) { + case R1(): + case R2(): + System.out.println(); + break; + default: + } + + } + + public static void test14() { + record R1(){} + record R2(){} + Object obj = null; + switch (obj) { + case null: + case R1(): + case R2(): + System.out.println(); + break; + default: + } + + } +} diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlightingPatternsInSwitch/Reachability.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlightingPatternsInSwitch/Reachability.java index b93d58c7f3f6..897d5c94c03f 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlightingPatternsInSwitch/Reachability.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlightingPatternsInSwitch/Reachability.java @@ -1,7 +1,7 @@ import java.util.*; class Test { - void testEnumRechable(Day d) { + void testEnumRechable1(Day d) { switch (d) { case MONDAY, TUESDAY: throw new IllegalArgumentException(); @@ -11,14 +11,6 @@ class Test { System.out.println(); } - void testEnumUncreachable1(Day d) { - switch (d) { - case MONDAY, TUESDAY, default: - throw new IllegalArgumentException(); - } - System.out.println(); - } - void testEnumUnreachable2(Day d) { switch (d) { case Day dd when true: diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlightingPatternsInSwitch/SwitchExprHasResult.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlightingPatternsInSwitch/SwitchExprHasResult.java index ae8b5e0af22c..98d42921a745 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlightingPatternsInSwitch/SwitchExprHasResult.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlightingPatternsInSwitch/SwitchExprHasResult.java @@ -17,7 +17,7 @@ class Test { return switch (i) { case Integer ii -> { switch (i) { - case default -> { + default -> { yield "fsdfs"; } } @@ -39,12 +39,12 @@ class Test { String nestedPattern(Object o) { return switch (o) { - case default -> { + default -> { switch (o) { case Integer i -> { System.out.println(); // completes normally } - case default -> { + default -> { yield "fsdfsd"; } } @@ -56,7 +56,7 @@ class Test { String switchNestingExpressionStatement(Object o1, Object o2) { return switch (o1) { - case default -> { + default -> { switch (o2) { case String s2 -> { yield "string, string"; diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/BoxedTypeNullability.java b/java/java-tests/testData/inspection/dataFlow/fixture/BoxedTypeNullability.java index 8db0db51e5f7..9acb0be3e55f 100644 --- a/java/java-tests/testData/inspection/dataFlow/fixture/BoxedTypeNullability.java +++ b/java/java-tests/testData/inspection/dataFlow/fixture/BoxedTypeNullability.java @@ -7,7 +7,7 @@ class Test { break; case null: break; - case default: + default: break; } } @@ -16,7 +16,7 @@ class Test { switch (i) { case 1: break; - case default: + default: break; } } @@ -28,7 +28,7 @@ class Test { break; case null: break; - case default: + default: break; } } @@ -40,7 +40,7 @@ class Test { break; case 1: break; - case default: + default: break; } } @@ -52,7 +52,7 @@ class Test { break; case null: break; - case default: + default: break; } } @@ -62,7 +62,7 @@ class Test { switch (i) { case 1: break; - case default: + default: break; } } @@ -72,7 +72,7 @@ class Test { switch (i) { case 1: break; - case default: + default: break; } } @@ -83,7 +83,7 @@ class Test { break; case null: break; - case default: + default: break; } } @@ -92,7 +92,7 @@ class Test { switch (i) { case 1: break; - case default: + default: break; } } @@ -104,7 +104,7 @@ class Test { break; case null: break; - case default: + default: break; } } @@ -116,7 +116,7 @@ class Test { break; case null: break; - case default: + default: break; } } @@ -126,7 +126,7 @@ class Test { switch (i) { case 1: break; - case default: + default: break; } } @@ -136,7 +136,7 @@ class Test { switch (i) { case 1: break; - case default: + default: break; } } @@ -147,7 +147,7 @@ class Test { break; case null: break; - case default: + default: break; } } @@ -156,7 +156,7 @@ class Test { switch (i) { case 1: break; - case default: + default: break; } } @@ -168,7 +168,7 @@ class Test { break; case null: break; - case default: + default: break; } } @@ -180,7 +180,7 @@ class Test { break; case null: break; - case default: + default: break; } } @@ -190,7 +190,7 @@ class Test { switch (i) { case 1: break; - case default: + default: break; } } @@ -200,7 +200,7 @@ class Test { switch (i) { case 1: break; - case default: + default: break; } } @@ -211,7 +211,7 @@ class Test { break; case null: break; - case default: + default: break; } } @@ -220,7 +220,7 @@ class Test { switch (createNullValue()) { case 1: break; - case default: + default: break; } } @@ -231,7 +231,7 @@ class Test { break; case null: break; - case default: + default: break; } } @@ -240,7 +240,7 @@ class Test { switch (createValue()) { case 1: break; - case default: + default: break; } } @@ -251,7 +251,7 @@ class Test { break; case null: break; - case default: + default: break; } } diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/ParameterNullabilityFromSwitch.java b/java/java-tests/testData/inspection/dataFlow/fixture/ParameterNullabilityFromSwitch.java index e232c5abac07..8e6be7b00c12 100644 --- a/java/java-tests/testData/inspection/dataFlow/fixture/ParameterNullabilityFromSwitch.java +++ b/java/java-tests/testData/inspection/dataFlow/fixture/ParameterNullabilityFromSwitch.java @@ -12,7 +12,7 @@ class Test { static void notNullable(String s) { switch (s) { case "xyz" -> System.out.println("xyz"); - case default -> System.out.println("else"); + default -> System.out.println("else"); } } diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/Patterns.java b/java/java-tests/testData/inspection/dataFlow/fixture/Patterns.java index b382835bc694..72a71516d698 100644 --- a/java/java-tests/testData/inspection/dataFlow/fixture/Patterns.java +++ b/java/java-tests/testData/inspection/dataFlow/fixture/Patterns.java @@ -15,7 +15,7 @@ class Test { a = -1; break; } - case default: { + default: { a = 2; break; } @@ -35,7 +35,7 @@ class Test { a = 1; break; } - case default: { + default: { a = 2; break; } @@ -50,7 +50,7 @@ class Test { return switch(FSD) { case String s when s.length() <= 3 && (s.length() > 1 || s.length() > 10) -> 1; case String s when Math.random() > 0.5 -> 2; - case default -> 3; + default -> 3; }; } @@ -66,7 +66,7 @@ class Test { switch (FSD) { case String s when s.length() > 2 && s.length() < 3 -> System.out.println(1); case String s when s.isEmpty() -> 2; - case default -> System.out.println(3); + default -> System.out.println(3); }; } @@ -81,7 +81,7 @@ class Test { String s = "abc"; switch (s) { case Object o -> System.out.println("total"); - case default -> System.out.println("default"); + default -> System.out.println("default"); } } @@ -90,7 +90,7 @@ class Test { switch (s) { case "" -> System.out.println("abc"); case Object o -> System.out.println("total"); - case default -> System.out.println("default"); + default -> System.out.println("default"); } } diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/PatternsNullability.java b/java/java-tests/testData/inspection/dataFlow/fixture/PatternsNullability.java index fc504bb00f55..a261e12fc05f 100644 --- a/java/java-tests/testData/inspection/dataFlow/fixture/PatternsNullability.java +++ b/java/java-tests/testData/inspection/dataFlow/fixture/PatternsNullability.java @@ -97,7 +97,7 @@ class Test { break; case Object o when !new ArrayList().isEmpty(): break; - case default: + default: break; } } @@ -205,7 +205,7 @@ class Test { return switch (createNullValue()) { case 1 -> 1; case Object o when Math.random() > 0.5 -> 2; - case default -> 3; + default -> 3; }; } diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/SuspiciousLabelElements.java b/java/java-tests/testData/inspection/dataFlow/fixture/SuspiciousLabelElements.java index 1a5079b49197..7fb6a77c939d 100644 --- a/java/java-tests/testData/inspection/dataFlow/fixture/SuspiciousLabelElements.java +++ b/java/java-tests/testData/inspection/dataFlow/fixture/SuspiciousLabelElements.java @@ -29,7 +29,7 @@ public class DuplicateLabels { break; case String ss: break; - case default: + default: break; } } diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/SuspiciousLabelElementsJava19.java b/java/java-tests/testData/inspection/dataFlow/fixture/SuspiciousLabelElementsJava19.java index e408ef18deb0..91de4a775fc6 100644 --- a/java/java-tests/testData/inspection/dataFlow/fixture/SuspiciousLabelElementsJava19.java +++ b/java/java-tests/testData/inspection/dataFlow/fixture/SuspiciousLabelElementsJava19.java @@ -5,21 +5,21 @@ public class Test { break; case String ss: break; - case default: + default: break; } } int testDominatedConstLabel(Integer i, E e) { switch (e) { - case E d when d == E.A: return 1; - case A: return -1; + case E d when true: return 1; + case A: return -1; } return switch (i) { - case Integer ii when ii > 2 -> 1; - case 2 -> 2; - case default -> 3; + case Integer ii when true -> 1; + case 2 -> 2; + default -> 3; }; } diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/WhenPatterns.java b/java/java-tests/testData/inspection/dataFlow/fixture/WhenPatterns.java index 0bd40236ad9c..5d6d115a5e54 100644 --- a/java/java-tests/testData/inspection/dataFlow/fixture/WhenPatterns.java +++ b/java/java-tests/testData/inspection/dataFlow/fixture/WhenPatterns.java @@ -8,14 +8,14 @@ class Test { int test3(String s1) { return switch(s1) { case String s when s.length() <= 3 && (s.length() > 1 || s.length() > 10) -> 1; - case default -> 3; + default -> 3; }; } int test4(String s) { return switch (s) { case String ss when (ss.length() < 3 || ss.length() == 4) -> 1; - case default -> 3; + default -> 3; }; } diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/LightPatternsForSwitchHighlightingTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/LightPatternsForSwitchHighlightingTest.java index 3314af962cfb..b839c0933f48 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/LightPatternsForSwitchHighlightingTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/LightPatternsForSwitchHighlightingTest.java @@ -47,6 +47,9 @@ public class LightPatternsForSwitchHighlightingTest extends LightJavaCodeInsight public void testIllegalFallthroughIn20Java() { IdeaTestUtil.withLevel(getModule(), LanguageLevel.JDK_20_PREVIEW, this::doTest); } + public void testIllegalFallthroughIn21Java() { + IdeaTestUtil.withLevel(getModule(), LanguageLevel.JDK_21, this::doTest); + } public void testUnconditionalDestructuringAndDefaultIn21Java() { IdeaTestUtil.withLevel(getModule(), LanguageLevel.JDK_21, this::doTest); diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspection21Test.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspection21Test.java index 2fd9cda0827f..9bc880e5eed8 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspection21Test.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspection21Test.java @@ -6,6 +6,7 @@ import com.intellij.testFramework.LightProjectDescriptor; import org.jetbrains.annotations.NotNull; public class DataFlowInspection21Test extends DataFlowInspectionTestCase { + @NotNull @Override protected LightProjectDescriptor getProjectDescriptor() { diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/controlflow/enumswitch/afterBeforeCaseDefault.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/controlflow/enumswitch/afterBeforeCaseDefault.java index d70d5bd110bc..3ba55e6f98c0 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igfixes/controlflow/enumswitch/afterBeforeCaseDefault.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/controlflow/enumswitch/afterBeforeCaseDefault.java @@ -12,7 +12,7 @@ class Main { break; case ERROR: break; - case default: + default: throw new IllegalArgumentException("Unknown Status " + status); } } diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/controlflow/enumswitch/beforeBeforeCaseDefault.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/controlflow/enumswitch/beforeBeforeCaseDefault.java index df6f367a95e4..1a8dff59a546 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igfixes/controlflow/enumswitch/beforeBeforeCaseDefault.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/controlflow/enumswitch/beforeBeforeCaseDefault.java @@ -10,7 +10,7 @@ class Main { break; case INACTIVE: break; - case default: + default: throw new IllegalArgumentException("Unknown Status " + status); } } diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/create_null_branch/DefaultExists.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/create_null_branch/DefaultExists.after.java index 8cb27d4bedbb..85dbfd79fa37 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/create_null_branch/DefaultExists.after.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/create_null_branch/DefaultExists.after.java @@ -8,7 +8,7 @@ class Test { break; case null: break; - case default: + default: break; } } diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/create_null_branch/DefaultExists.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/create_null_branch/DefaultExists.java index fa28f98edc13..a8963261abb4 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/create_null_branch/DefaultExists.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/create_null_branch/DefaultExists.java @@ -6,7 +6,7 @@ class Test { case 1: System.out.println(); break; - case default: + default: break; } } diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/create_null_branch/PrevStatementCompletesNormally.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/create_null_branch/PrevStatementCompletesNormally.after.java index d3b3e24fd082..103376637c3f 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/create_null_branch/PrevStatementCompletesNormally.after.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/create_null_branch/PrevStatementCompletesNormally.after.java @@ -8,7 +8,7 @@ class Test { break; case null: break; - case default: + default: break; } } diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/create_null_branch/PrevStatementCompletesNormally.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/create_null_branch/PrevStatementCompletesNormally.java index 1735b6d5242f..1d3ac962f859 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/create_null_branch/PrevStatementCompletesNormally.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/create_null_branch/PrevStatementCompletesNormally.java @@ -5,7 +5,7 @@ class Test { switch ((i)) { case 1: System.out.println(); - case default: + default: break; } } diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/create_null_branch/RuleWithDefaultExists.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/create_null_branch/RuleWithDefaultExists.after.java index 9a335ea569f4..05dec4caf514 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/create_null_branch/RuleWithDefaultExists.after.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/create_null_branch/RuleWithDefaultExists.after.java @@ -6,7 +6,7 @@ class Test { case 1 -> System.out.println(); case null -> { } - case default -> System.out.println(); + default -> System.out.println(); } } } \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/create_null_branch/RuleWithDefaultExists.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/create_null_branch/RuleWithDefaultExists.java index 981587d8e8de..c1455ece4ec3 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/create_null_branch/RuleWithDefaultExists.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/create_null_branch/RuleWithDefaultExists.java @@ -4,7 +4,7 @@ class Test { void test(@Nullable Integer i) { switch ((i)) { case 1 -> System.out.println(); - case default -> System.out.println(); + default -> System.out.println(); } } } \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/create_null_branch/UnconditionalPatternNotExist.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/create_null_branch/UnconditionalPatternNotExist.after.java index cc10e4a8a4b5..1a56c082dff3 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/create_null_branch/UnconditionalPatternNotExist.after.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/create_null_branch/UnconditionalPatternNotExist.after.java @@ -6,7 +6,7 @@ class Test { case null -> 0; case String s && s.length() <= 3 -> 1; case "abc" -> 2; - case default -> 3; + default -> 3; }; } } \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/create_null_branch/UnconditionalPatternNotExist.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/create_null_branch/UnconditionalPatternNotExist.java index a2575687b15a..c77cadbdd078 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/create_null_branch/UnconditionalPatternNotExist.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/create_null_branch/UnconditionalPatternNotExist.java @@ -5,7 +5,7 @@ class Test { return switch (s) { case String s && s.length() <= 3 -> 1; case "abc" -> 2; - case default -> 3; + default -> 3; }; } } \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/switch_statement_with_too_few_branches/SwitchStatementWithTooFewBranches.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/switch_statement_with_too_few_branches/SwitchStatementWithTooFewBranches.java index a25eb90f0ec4..15deec9ceb66 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/switch_statement_with_too_few_branches/SwitchStatementWithTooFewBranches.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/switch_statement_with_too_few_branches/SwitchStatementWithTooFewBranches.java @@ -70,12 +70,12 @@ class SwitchStatementWithTooFewBranches { } switch (o) { - case default -> System.out.println(); + default -> System.out.println(); } switch (o) { case null -> System.out.println(); - case default -> System.out.println(); + default -> System.out.println(); } } diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/unnecessary_default/UnnecessaryDefault.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/unnecessary_default/UnnecessaryDefault.java index ba19242f2bed..798915d18a25 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/unnecessary_default/UnnecessaryDefault.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/unnecessary_default/UnnecessaryDefault.java @@ -162,7 +162,7 @@ public class UnnecessaryDefault{ switch (e) { case foo, bar, baz: break; - case /**test**/ default /**test**/: + /**test**/ default /**test**/: break; } } @@ -182,7 +182,7 @@ public class UnnecessaryDefault{ break; case C2 c2: break; - case /**test**/ default /**test**/: + /**test**/ default /**test**/: break; } } @@ -204,7 +204,7 @@ public class UnnecessaryDefault{ break; case C4 c4: break; - case default: + default: break; } } diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/SwitchStatementWithTooFewBranchesInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/SwitchStatementWithTooFewBranchesInspectionTest.java index 9d0578cfefb9..e54186100313 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/SwitchStatementWithTooFewBranchesInspectionTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/SwitchStatementWithTooFewBranchesInspectionTest.java @@ -22,6 +22,6 @@ public class SwitchStatementWithTooFewBranchesInspectionTest extends LightJavaIn @Override protected @NotNull LightProjectDescriptor getProjectDescriptor() { - return JAVA_20; + return JAVA_21; } } diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/UnnecessaryDefaultInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/UnnecessaryDefaultInspectionTest.java index a63418a80222..2856d65b0ef1 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/UnnecessaryDefaultInspectionTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/UnnecessaryDefaultInspectionTest.java @@ -107,26 +107,6 @@ public class UnnecessaryDefaultInspectionTest extends LightJavaInspectionTestCas "}"); } - public void testCaseDefaultWithEnumElements() { - doTest("class X {" + - " void x(E e) {" + - " switch (e) {" + - " case A, /*'default' branch is unnecessary*/default/*_*//**/, B:" + - " break;" + - " }" + - " }" + - "}"); - checkQuickFix("Remove 'default' branch", - "class X {" + - " void x(E e) {" + - " switch (e) {" + - " case A, B:" + - " break;" + - " }" + - " }" + - "}"); - } - public void testCaseDefaultInSealedSwitch() { doTest("class X {" + " void x(I i) {" + @@ -157,34 +137,6 @@ public class UnnecessaryDefaultInspectionTest extends LightJavaInspectionTestCas "}"); } - public void testCaseDefaultWithPattern() { - doTest("class X {" + - " void x(I i) {" + - " switch (i) {" + - " case /*'default' branch is unnecessary*/default/*_*//**/, I ii when Math.random() > 0.5:" + - " break;" + - " case C1 c1:" + - " break;" + - " case C2 c2:" + - " break;" + - " }" + - " }" + - "}"); - checkQuickFix("Remove 'default' branch", - "class X {" + - " void x(I i) {" + - " switch (i) {" + - " case I ii when Math.random() > 0.5:" + - " break;" + - " case C1 c1:" + - " break;" + - " case C2 c2:" + - " break;" + - " }" + - " }" + - "}"); - } - public void testDefaultInParameterizedSealedHierarchy() { doTest("class X {" + " void x(J j) {" +