Files
openide/java/java-tests/testData/inspection/dataFlow/fixture/SwitchStatements.java
Tagir Valeev 42f43778e2 [java-dfa] Do not report unreachable switch branches if they are throwing
GitOrigin-RevId: 5164e1b5f86abb797f7aa1a33b61fd28d85ca1d5
2022-05-18 13:21:53 +00:00

74 lines
2.1 KiB
Java

public class SwitchStatements {
void testMultiLabel(int x) {
if(x > 0) {
switch (x) {
case 1, 2, <warning descr="Switch label '-1' is unreachable">-1</warning>:
System.out.println("oops");
break;
}
switch (x) {
case 1, 2, <warning descr="Switch label '-1' is unreachable">-1</warning> -> {
System.out.println("oops");
break;
}
}
}
}
void testThrowRule(int x) {
switch (x) {
case 0 -> throw new IllegalArgumentException();
default -> System.out.println(<warning descr="Condition 'x == 0' is always 'false'">x == 0</warning>);
}
if (<warning descr="Condition 'x == 0' is always 'false'">x == 0</warning>) System.out.println("impossible");
}
void testFallthrough(int x) {
switch (x) {
case 0 -> System.out.println(x);
case 1 -> {
System.out.println(<warning descr="Condition 'x == 0' is always 'false'">x == 0</warning>);
System.out.println(<warning descr="Condition 'x == 1' is always 'true'">x == 1</warning>);
}
}
switch (x) {
case 0: System.out.println(x);
case 1: {
System.out.println(x == 0);
System.out.println(x == 1);
}
}
}
void testOnlyBranch(int x) {
if (x > 1) return;
switch (x) {
case 0 -> System.out.println("zero");
case 1 -> System.out.println("one");
case <warning descr="Switch label '2' is unreachable">2</warning> -> System.out.println("two");
}
if (x < 1) return;
switch (x) {
case 0 -> System.out.println("zero");
case <warning descr="Switch label '1' is the only reachable in the whole switch">1</warning> -> System.out.println("one");
case 2 -> System.out.println("two");
}
}
enum X {A, B, C}
void testThrowingBranch(X x) {
if (x == X.A) return;
switch(x) {
case A -> throw new AssertionError();
case B -> System.out.println(1);
case C -> System.out.println(2);
}
switch(x) {
case B: System.out.println(1);break;
case C: System.out.println(2);break;
case A: default: throw new AssertionError();
}
}
}