java-topology/test/langtools/tools/javac/switchexpr/ExpressionSwitchUnreachable.java
russell@unturf.com 0a580b313d undefect. CWE-407 — 63 sites patched across 27 ecosystems
Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com

Patches, unit tests, benchmarks, whitepaper, and outreach briefs.
Public domain — no copyright claimed. Use freely.
2026-03-26 17:11:57 -04:00

57 lines
1.5 KiB
Java

/*
* @test /nodynamiccopyright/
* @bug 8206986
* @summary Verify reachability in switch expressions.
* @compile/fail/ref=ExpressionSwitchUnreachable.out -XDrawDiagnostics ExpressionSwitchUnreachable.java
*/
public class ExpressionSwitchUnreachable {
public static void meth() {
int z = 42;
int i = switch (z) {
case 0 -> {
yield 42;
System.out.println("Unreachable"); //Unreachable
}
default -> 0;
};
i = switch (z) {
case 0 -> {
yield 42;
yield 42; //Unreachable
}
default -> 0;
};
i = switch (z) {
case 0:
System.out.println("0");
yield 42;
System.out.println("1"); //Unreachable
default : yield 42;
};
i = switch (z) {
case 0 -> 42;
default -> {
yield 42;
System.out.println("Unreachable"); //Unreachable
}
};
i = switch (z) {
case 0: yield 42;
default:
System.out.println("0");
yield 42;
System.out.println("1"); //Unreachable
};
i = switch (z) {
case 0:
default:
System.out.println("0");
yield 42;
System.out.println("1"); //Unreachable
};
}
}