Files
openide/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/switchExpressions/EnhancedSwitchStatements.java
Mikhail Pyltsin cd14483af8 [java-highlighting] IDEA-357866 Spurious "Different case kinds used in the switch" error on incomplete code
- improve error message

GitOrigin-RevId: 2a8da472e07dd10bdb0ae75e679b6056ab884b72
2024-08-22 16:03:56 +00:00

91 lines
3.2 KiB
Java

import java.util.Random;
class EnhancedSwitchStatements {
static final int FI = 4;
enum E { E1, E2 }
void m(String... args) {
String count;
switch (args.length) {
case 0 -> throw new IllegalStateException("no args");
case 1 -> count = "one";
default -> { count = "many"; }
}
switch (new Random().nextInt()) {
case 0 -> throw new IllegalStateException("no args");
<error descr="Statement must be prepended with case label">break;</error>
}
switch (new Random().nextInt()) {
case 0 -> throw new IllegalStateException("no args");
<error descr="Different 'case' kinds used in 'switch'">case 1:</error> break;
}
switch (new Random().nextInt()) {
case 0: throw new IllegalStateException("no args"); break;
<error descr="Different 'case' kinds used in 'switch'">case 1 -> { System.out.println("one"); }</error>
}
{ <error descr="Case statement outside switch">case 11 -> System.out.println("hi there");</error> }
{ <error descr="Case statement outside switch">default -> System.out.println("hi there");</error> }
switch (new Random().nextInt()) {
case 42 -> <error descr="Not a statement">"bingo";</error>
}
switch (new Random().nextInt()) {
<error descr="Duplicate default label">default</error> -> noop();
case 1 -> noop();
<error descr="Duplicate default label">default</error> -> noop();
}
switch (new Random().nextInt()) {
case <error descr="Duplicate label '1'">FI/2 - 1</error> -> noop();
case <error descr="Duplicate label '1'">(1 + 35/16) % 2</error> -> noop();
case FI - 8 -> noop();
}
final byte b = 127;
switch (new Random().nextInt()) {
case <error descr="Duplicate label '127'">b</error> -> System.out.println("b=" + b + ";");
case <error descr="Duplicate label '127'">127</error> -> System.out.println("sweet spot");
}
switch (0) {
case 0 -> noop();
case "\410" == "!0" ? 1 : 0 -> noop();
case "" == "" + "" ? 3 : 0 -> noop();
}
switch (E.valueOf("E1")) {
case E1 -> noop();
}
switch (E.valueOf("E1")) {
case E1 -> noop();
case E2 -> noop();
case <error descr="Patterns in switch are not supported at language level '15'">null</error> -> noop();
}
switch (E.valueOf("E1")) {
case <error descr="An enum switch case label must be the unqualified name of an enumeration constant">E.E1</error> -> noop();
case E2 -> noop();
case <error descr="Incompatible types. Found: 'int', required: 'EnhancedSwitchStatements.E'">1</error> -> noop();
}
switch (new Random().nextInt()) {
case <error descr="Duplicate label '1'">1</error>, <error descr="Duplicate label '1'">1</error> -> noop();
}
switch (new Random().nextInt()) {
case 1, <error descr="Duplicate label '2'">2</error>:
noop(); break;
case 3, <error descr="Duplicate label '2'">2</error>:
noop(); break;
}
switch (<error descr="Incompatible types. Found: 'java.lang.Object', required: 'char, byte, short, int, Character, Byte, Short, Integer, String, or an enum'">new Object()</error>) { }
}
private static void noop() { }
}