-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathMissingEnumInSwitch.ql
More file actions
64 lines (58 loc) · 1.96 KB
/
MissingEnumInSwitch.ql
File metadata and controls
64 lines (58 loc) · 1.96 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
/**
* @name Missing enum case in switch
* @description A 'switch' statement that is based on an 'enum' type and does not have cases for all
* the 'enum' constants is usually a coding mistake.
* @kind problem
* @problem.severity warning
* @precision medium
* @id java/missing-case-in-switch
* @tags quality
* reliability
* correctness
* external/cwe/cwe-478
*/
import java
EnumConstant nthMissing(SwitchStmt switch, int index) {
not exists(switch.getDefaultCase()) and
exists(EnumType enum |
switch.getExpr().getType() = enum and
result =
rank[index](EnumConstant ec |
ec.getDeclaringType() = enum and
not switch.getAConstCase().getValue() = ec.getAnAccess()
|
ec order by ec.getName()
)
)
}
predicate first3(string msg, SwitchStmt switch, EnumConstant e1, EnumConstant e2, EnumConstant e3) {
exists(int n | n = strictcount(nthMissing(switch, _)) |
if n > 3
then msg = "Switch statement does not have a case for $@, $@, $@, or " + (n - 3) + " more."
else msg = "Switch statement does not have a case for $@, $@, or $@."
) and
e1 = nthMissing(switch, 1) and
e2 = nthMissing(switch, 2) and
e3 = nthMissing(switch, 3)
}
predicate only2(string msg, SwitchStmt switch, EnumConstant e1, EnumConstant e2) {
msg = "Switch statement does not have a case for $@ or $@." and
e1 = nthMissing(switch, 1) and
e2 = nthMissing(switch, 2)
}
predicate only1(string msg, SwitchStmt switch, EnumConstant e) {
msg = "Switch statement does not have a case for $@." and
e = nthMissing(switch, 1)
}
from string msg, SwitchStmt switch, EnumConstant e1, EnumConstant e2, EnumConstant e3
where
if first3(_, switch, _, _, _)
then first3(msg, switch, e1, e2, e3)
else
if only2(_, switch, _, _)
then (
only2(msg, switch, e1, e2) and e1 = e3
) else (
only1(msg, switch, e1) and e1 = e2 and e1 = e3
)
select switch, msg, e1, e1.getName(), e2, e2.getName(), e3, e3.getName()