[java-highlighting] Refactoring for fallthrough

GitOrigin-RevId: 88bf0c45eb23406f29f693659c8495b286512d36
This commit is contained in:
Mikhail Pyltsin
2023-07-05 20:46:12 +00:00
committed by intellij-monorepo-bot
parent b43535b550
commit d1ee298827
29 changed files with 272 additions and 205 deletions
@@ -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<Object, PsiElement> elementsToCheckDuplicates = new MultiMap<>();
List<List<PsiSwitchLabelStatementBase>> elementsToCheckFallThroughLegality = new SmartList<>();
List<PsiElement> elementsToCheckDominance = new ArrayList<>();
@@ -558,14 +557,9 @@ public class SwitchBlockHighlightingModel {
checkDuplicates(elementsToCheckDuplicates, holder);
if (holder.hasErrorResults()) return;
if (java20plus) {
HashSet<PsiElement> alreadyFallThroughElements = new HashSet<>();
checkFallThroughFromToPatternJava20(elementsToCheckFallThroughLegality, holder, alreadyFallThroughElements);
checkFallThroughInSwitchLabels(elementsToCheckFallThroughLegality, holder, alreadyFallThroughElements);
}
else {
checkFallThroughFromToPattern(elementsToCheckFallThroughLegality, holder);
}
HashSet<PsiElement> 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
* <ul>
* To ensure safe initialization of pattern variables fall through rules in common provide the restrictions
* of using different type of case label switchLabel:
* <li>patterns with patterns</li>
* <li>patterns with constants</li>
* <li>patterns with default</li>
* </ul>
*/
private static void checkFallThroughFromToPattern(@NotNull List<? extends List<PsiSwitchLabelStatementBase>> switchBlockGroup,
@NotNull HighlightInfoHolder holder) {
if (switchBlockGroup.isEmpty()) return;
Set<PsiElement> 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<List<PsiSwitchLabelStatementBase>> switchBlockGroup,
@NotNull HighlightInfoHolder holder,
@NotNull Set<PsiElement> alreadyFallThroughElements) {
private static void checkFallThroughFromPatternWithSeveralLabels(@NotNull List<List<PsiSwitchLabelStatementBase>> switchBlockGroup,
@NotNull HighlightInfoHolder holder,
@NotNull Set<PsiElement> 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<? extends List<? extends PsiSwitchLabelStatementBase>> switchBlockGroup,
@NotNull HighlightInfoHolder results,
@NotNull Set<PsiElement> alreadyFallThroughElements) {
private static void checkFallThroughToPatternPrecedingCompleteNormally(@NotNull List<? extends List<? extends PsiSwitchLabelStatementBase>> switchBlockGroup,
@NotNull HighlightInfoHolder results,
@NotNull Set<PsiElement> alreadyFallThroughElements) {
for (int i = 1; i < switchBlockGroup.size(); i++) {
List<? extends PsiSwitchLabelStatementBase> switchLabels = switchBlockGroup.get(i);
PsiSwitchLabelStatementBase firstSwitchLabelInGroup = switchLabels.get(0);
@@ -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 -> {}
}
@@ -2,9 +2,9 @@
class Main {
void ff(Object o) {
switch (o) {
case String s:
case <error descr="Multiple switch labels are permitted for a switch labeled statement group only if none of them declare any pattern variables">String s</error>:
case null:
case <error descr="Illegal fall-through to a pattern">Integer i</error>:
case <error descr="Multiple switch labels are permitted for a switch labeled statement group only if none of them declare any pattern variables">Integer i</error>:
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 <error descr="Multiple switch labels are permitted for a switch labeled statement group only if none of them declare any pattern variables">Integer i</error>:
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 <error descr="Illegal fall-through to a pattern">Integer i</error>:
case <error descr="Multiple switch labels are permitted for a switch labeled statement group only if none of them declare any pattern variables">String s</error>:
case <error descr="Multiple switch labels are permitted for a switch labeled statement group only if none of them declare any pattern variables">Integer i</error>:
System.out.println(i + 1);
default:
throw new IllegalStateException("Unexpected value: " + o);
@@ -0,0 +1,190 @@
class IllegalFallthroughIn21Java {
public static void test() {
Object o = "Hello";
switch (o) {
case String s:
System.out.println();
case <error descr="Illegal fall-through to a pattern">Integer i</error>:
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 <error descr="Illegal fall-through to a pattern">Integer i</error>:
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(<error descr="Cannot resolve symbol 's'">s</error>);
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(<error descr="Cannot resolve symbol 's'">s</error>);
break;
default:
throw new IllegalStateException("Unexpected value: " + o);
}
}
public static void test7() {
Object obj = "Hello";
switch (obj) {
case <error descr="Multiple switch labels are permitted for a switch labeled statement group only if none of them declare any pattern variables">String s</error>:
case <error descr="Multiple switch labels are permitted for a switch labeled statement group only if none of them declare any pattern variables">Integer i</error>:
System.out.println();
}
}
public static void test8() {
Object obj = "Hello";
switch (obj) {
case null:
case <error descr="Multiple switch labels are permitted for a switch labeled statement group only if none of them declare any pattern variables">Integer i</error>:
System.out.println();
}
}
public static void test9() {
String obj = "Hello";
switch (obj) {
case "a":
case <error descr="Multiple switch labels are permitted for a switch labeled statement group only if none of them declare any pattern variables">String i when i.length() == 1</error>:
System.out.println();
}
}
public static void test10() {
Object obj = "Hello";
switch (obj) {
case null:
case <error descr="Multiple switch labels are permitted for a switch labeled statement group only if none of them declare any pattern variables">Integer i</error>:
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(<error descr="Cannot resolve symbol 's'">s</error>);
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:
}
}
}
@@ -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();
}
<error descr="Unreachable statement">System.out.println();</error>
}
void testEnumUnreachable2(Day d) {
switch (d) {
case Day dd when true:
@@ -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) {
<error descr="Switch expression rule should produce result in all execution paths">case</error> default -> {
<error descr="Switch expression rule should produce result in all execution paths">default</error> -> {
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";
@@ -7,7 +7,7 @@ class Test {
break;
case null:
break;
case default:
default:
break;
}
}
@@ -16,7 +16,7 @@ class Test {
switch (<warning descr="Unboxing of 'i' may produce 'NullPointerException'">i</warning>) {
case 1:
break;
case default:
default:
break;
}
}
@@ -28,7 +28,7 @@ class Test {
break;
case <warning descr="Switch label 'null' is the only reachable in the whole switch">null</warning>:
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 (<warning descr="Unboxing of 'i' may produce 'NullPointerException'">i</warning>) {
case 1:
break;
case default:
default:
break;
}
}
@@ -72,7 +72,7 @@ class Test {
switch (i) {
case <warning descr="Switch label '1' is the only reachable in the whole switch">1</warning>:
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 <warning descr="Switch label 'null' is the only reachable in the whole switch">null</warning>:
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 (<warning descr="Unboxing of 'i' may produce 'NullPointerException'">i</warning>) {
case 1:
break;
case default:
default:
break;
}
}
@@ -136,7 +136,7 @@ class Test {
switch (i) {
case <warning descr="Switch label '1' is the only reachable in the whole switch">1</warning>:
break;
case default:
default:
break;
}
}
@@ -147,7 +147,7 @@ class Test {
break;
case <warning descr="Switch label 'null' is unreachable">null</warning>:
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 <warning descr="Switch label 'null' is the only reachable in the whole switch">null</warning>:
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 (<warning descr="Unboxing of 'i' may produce 'NullPointerException'">i</warning>) {
case 1:
break;
case default:
default:
break;
}
}
@@ -200,7 +200,7 @@ class Test {
switch (i) {
case <warning descr="Switch label '1' is the only reachable in the whole switch">1</warning>:
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 (<warning descr="Unboxing of 'createNullValue()' may produce 'NullPointerException'">createNullValue()</warning>) {
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 <warning descr="Switch label 'null' is unreachable">null</warning>:
break;
case default:
default:
break;
}
}
@@ -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");
}
}
@@ -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 <warning descr="Switch label 'String s when s.length() <= 3 && (s.length() > 1 || s.length() > 10)' is the only reachable in the whole switch">String s when <warning descr="Condition 's.length() <= 3 && (s.length() > 1 || s.length() > 10)' is always 'true'"><warning descr="Condition 's.length() <= 3' is always 'true'">s.length() <= 3</warning> && (<warning descr="Condition 's.length() > 1 || s.length() > 10' is always 'true' when reached"><warning descr="Condition 's.length() > 1' is always 'true' when reached">s.length() > 1</warning> || s.length() > 10</warning>)</warning></warning> -> 1;
case String s when Math.random() > 0.5 -> 2;
case default -> 3;
default -> 3;
};
}
@@ -66,7 +66,7 @@ class Test {
switch (FSD) {
case <warning descr="Switch label 'String s when s.length() > 2 && s.length() < 3' is unreachable">String s when <warning descr="Condition 's.length() > 2 && s.length() < 3' is always 'false'"><warning descr="Condition 's.length() > 2' is always 'true'">s.length() > 2</warning> && <warning descr="Condition 's.length() < 3' is always 'false' when reached">s.length() < 3</warning></warning></warning> -> System.out.println(1);
case <warning descr="Switch label 'String s when s.isEmpty()' is unreachable">String s when <warning descr="Result of 's.isEmpty()' is always 'false'">s.isEmpty()</warning></warning> -> <error descr="Not a statement">2;</error>
case default -> System.out.println(3);
default -> System.out.println(3);
};
}
@@ -81,7 +81,7 @@ class Test {
String s = "abc";
switch (s) {
case <error descr="'switch' has both an unconditional pattern and a default label">Object o</error> -> System.out.println("total");
case <error descr="'switch' has both an unconditional pattern and a default label">default</error> -> System.out.println("default");
<error descr="'switch' has both an unconditional pattern and a default label">default</error> -> System.out.println("default");
}
}
@@ -90,7 +90,7 @@ class Test {
switch (s) {
case <warning descr="Switch label '\"\"' is unreachable">""</warning> -> System.out.println("abc");
case <error descr="'switch' has both an unconditional pattern and a default label">Object o</error> -> System.out.println("total");
case <error descr="'switch' has both an unconditional pattern and a default label">default</error> -> System.out.println("default");
<error descr="'switch' has both an unconditional pattern and a default label">default</error> -> System.out.println("default");
}
}
@@ -97,7 +97,7 @@ class Test {
break;
case <warning descr="Switch label 'Object o when !new ArrayList<String>().isEmpty()' is unreachable">Object o when <warning descr="Condition '!new ArrayList<String>().isEmpty()' is always 'false'">!new ArrayList<String>().isEmpty()</warning></warning>:
break;
case default:
default:
break;
}
}
@@ -205,7 +205,7 @@ class Test {
return switch (<warning descr="Unboxing of 'createNullValue()' may produce 'NullPointerException'">createNullValue()</warning>) {
case 1 -> 1;
case Object o when Math.random() > 0.5 -> 2;
case default -> 3;
default -> 3;
};
}
@@ -29,7 +29,7 @@ public class DuplicateLabels {
break;
case <error descr="Label is dominated by a preceding case label 'String ss when true'">String ss</error>:
break;
case default:
default:
break;
}
}
@@ -5,21 +5,21 @@ public class Test {
break;
case <error descr="Label is dominated by a preceding case label 'String ss when true'">String ss</error>:
break;
case default:
default:
break;
}
}
int testDominatedConstLabel(Integer i, E e) {
switch (e) {
case E d when d == E.A: return 1;
case <error descr="Label is dominated by a preceding case label 'E d when d == E.A'">A</error>: return -1;
case <warning descr="Switch label 'E d when true' is the only reachable in the whole switch">E d when true</warning>: return 1;
case <error descr="Label is dominated by a preceding case label 'E d when true'">A</error>: return -1;
}
return switch (i) {
case Integer ii when ii > 2 -> 1;
case <error descr="Label is dominated by a preceding case label 'Integer ii when ii > 2'">2</error> -> 2;
case default -> 3;
case Integer ii when true -> 1;
case <error descr="Label is dominated by a preceding case label 'Integer ii when true'">2</error> -> 2;
default -> 3;
};
}
@@ -8,14 +8,14 @@ class Test {
int test3(String s1) {
return switch(s1) {
case String s when s.length() <= 3 && (s.length() > 1 || <warning descr="Condition 's.length() > 10' is always 'false' when reached">s.length() > 10</warning>) -> 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;
};
}
@@ -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);
@@ -6,6 +6,7 @@ import com.intellij.testFramework.LightProjectDescriptor;
import org.jetbrains.annotations.NotNull;
public class DataFlowInspection21Test extends DataFlowInspectionTestCase {
@NotNull
@Override
protected LightProjectDescriptor getProjectDescriptor() {
@@ -12,7 +12,7 @@ class Main {
break;
case ERROR:
break;
case default:
default:
throw new IllegalArgumentException("Unknown Status " + status);
}
}
@@ -10,7 +10,7 @@ class Main {
break;
case INACTIVE:
break;
case default:
default:
throw new IllegalArgumentException("Unknown Status " + status);
}
}
@@ -8,7 +8,7 @@ class Test {
break;
case null:
break;
case default:
default:
break;
}
}
@@ -6,7 +6,7 @@ class Test {
case 1:
System.out.println();
break;
case default:
default:
break;
}
}
@@ -8,7 +8,7 @@ class Test {
break;
case null:
break;
case default:
default:
break;
}
}
@@ -5,7 +5,7 @@ class Test {
switch ((<caret>i)) {
case 1:
System.out.println();
case default:
default:
break;
}
}
@@ -6,7 +6,7 @@ class Test {
case 1 -> System.out.println();
case null -> {
}
case default -> System.out.println();
default -> System.out.println();
}
}
}
@@ -4,7 +4,7 @@ class Test {
void test(@Nullable Integer i) {
switch ((<caret>i)) {
case 1 -> System.out.println();
case default -> System.out.println();
default -> System.out.println();
}
}
}
@@ -6,7 +6,7 @@ class Test {
case null -> 0;
case String s && s.length() <= 3 -> 1;
case "abc" -> 2;
case default -> 3;
default -> 3;
};
}
}
@@ -5,7 +5,7 @@ class Test {
return switch (<caret>s) {
case String s && s.length() <= 3 -> 1;
case "abc" -> 2;
case default -> 3;
default -> 3;
};
}
}
@@ -70,12 +70,12 @@ class SwitchStatementWithTooFewBranches {
}
<warning descr="'switch' statement has only 'default' case">switch</warning> (o) {
case default -> System.out.println();
default -> System.out.println();
}
<warning descr="'switch' statement has too few case labels (1), and should probably be replaced with an 'if' statement">switch</warning> (o) {
case null -> System.out.println();
case default -> System.out.println();
default -> System.out.println();
}
}
@@ -162,7 +162,7 @@ public class UnnecessaryDefault{
switch (e) {
case foo, bar, baz:
break;
case /**test**/ <warning descr="'default' branch is unnecessary">default</warning> /**test**/:
/**test**/ <warning descr="'default' branch is unnecessary">default</warning> /**test**/:
break;
}
}
@@ -182,7 +182,7 @@ public class UnnecessaryDefault{
break;
case C2 c2:
break;
case /**test**/ <warning descr="'default' branch is unnecessary">default</warning> /**test**/:
/**test**/ <warning descr="'default' branch is unnecessary">default</warning> /**test**/:
break;
}
}
@@ -204,7 +204,7 @@ public class UnnecessaryDefault{
break;
case C4 c4:
break;
case default:
default:
break;
}
}
@@ -22,6 +22,6 @@ public class SwitchStatementWithTooFewBranchesInspectionTest extends LightJavaIn
@Override
protected @NotNull LightProjectDescriptor getProjectDescriptor() {
return JAVA_20;
return JAVA_21;
}
}
@@ -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/*_*//**/, <error descr=\"Illegal fall-through to a pattern\">I ii when Math.random() > 0.5</error>:" +
" 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<Integer> j) {" +