-
Notifications
You must be signed in to change notification settings - Fork 725
Expand file tree
/
Copy pathSwitchAtLeastThreeCasesCheckSample.java
More file actions
111 lines (96 loc) · 2.22 KB
/
SwitchAtLeastThreeCasesCheckSample.java
File metadata and controls
111 lines (96 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package checks.tests;
public class SwitchAtLeastThreeCasesCheckSample {
record MyRecord(int x, int y) {}
static void recordSwitch1(Object object) {
switch (object) { // Compliant
case String x when x.length() > 42 -> { }
default -> { }
}
}
static void recordSwitch2(Object object) {
switch (object) { // Compliant
case MyRecord(int x, int y) -> { }
default -> { }
}
}
public interface SealedClass {
sealed interface Shape permits Box, Circle {}
record Box() implements Shape { }
record Circle() implements Shape {}
default void foo(Shape shape) {
switch (shape) { // Compliant because of type pattern matching
case Box ignored -> { }
case Circle ignored -> System.out.println();
}
}
default void goo(Shape shape) {
switch (shape) { // Compliant because of type pattern matching
case Box ignored -> { }
default -> System.out.println();
}
}
}
private static void doSomething() {}
private static void doSomethingElse() {}
public void f(int variable) {
switch (variable) { // Noncompliant {{Replace this "switch" statement by "if" statements to increase readability.}}
// ^^^^^^
case 0:
doSomething();
break;
default:
doSomethingElse();
break;
}
switch (variable) {
case 0:
case 1:
doSomething();
break;
default:
doSomethingElse();
break;
}
switch (variable) {
case 0, 1:
doSomething();
break;
default:
doSomethingElse();
break;
}
switch (variable) { // Noncompliant
}
if (variable == 0) {
doSomething();
} else {
doSomethingElse();
}
}
public enum SmallEnum {
ONE,
TWO
}
public void switchOverSmallEnum1(SmallEnum smallEnum) {
switch (smallEnum) {
case ONE -> {
System.out.println("1");
}
case TWO -> {
System.out.println("2");
}
};
}
public int switchOverSmallEnum2(SmallEnum smallEnum) {
int ret = -1;
switch (smallEnum) {
case ONE:
ret = 1;
break;
case TWO:
ret = 2;
break;
};
return ret;
}
}