Skip to content

Commit 60b5e49

Browse files
committed
Java: Limit the amount of results that MissingEnumInSwitch produces per switch
The tool status page warns: An analysis file contained multiple alerts that included more related locations than our allowed limit of 100. These alerts correspond to the rule java/missing-case-in-switch. Only 100 locations were stored for these alerts.
1 parent 916b1e9 commit 60b5e49

File tree

1 file changed

+47
-6
lines changed

1 file changed

+47
-6
lines changed

java/ql/src/Likely Bugs/Statements/MissingEnumInSwitch.ql

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,51 @@
1313

1414
import java
1515

16-
from SwitchStmt switch, EnumType enum, EnumConstant missing
17-
where
18-
switch.getExpr().getType() = enum and
19-
missing.getDeclaringType() = enum and
16+
EnumConstant nthMissing(SwitchStmt switch, int index) {
2017
not exists(switch.getDefaultCase()) and
21-
not switch.getAConstCase().getValue() = missing.getAnAccess()
22-
select switch, "Switch statement does not have a case for $@.", missing, missing.getName()
18+
exists(EnumType enum |
19+
switch.getExpr().getType() = enum and
20+
result =
21+
rank[index](EnumConstant ec |
22+
ec.getDeclaringType() = enum and
23+
not switch.getAConstCase().getValue() = ec.getAnAccess()
24+
|
25+
ec order by ec.getName()
26+
)
27+
)
28+
}
29+
30+
predicate first3(string msg, SwitchStmt switch, EnumConstant e1, EnumConstant e2, EnumConstant e3) {
31+
exists(int n | n = strictcount(nthMissing(switch, _)) |
32+
if n > 3
33+
then msg = "Switch statement does not have a case for $@, $@, $@ or " + (n - 3) + " more."
34+
else msg = "Switch statement does not have a case for $@, $@ or $@."
35+
) and
36+
e1 = nthMissing(switch, 1) and
37+
e2 = nthMissing(switch, 2) and
38+
e3 = nthMissing(switch, 3)
39+
}
40+
41+
predicate only2(string msg, SwitchStmt switch, EnumConstant e1, EnumConstant e2) {
42+
msg = "Switch statement does not have a case for $@ or $@." and
43+
e1 = nthMissing(switch, 1) and
44+
e2 = nthMissing(switch, 2)
45+
}
46+
47+
predicate only1(string msg, SwitchStmt switch, EnumConstant e) {
48+
msg = "Switch statement does not have a case for $@." and
49+
e = nthMissing(switch, 1)
50+
}
51+
52+
from string msg, SwitchStmt switch, EnumConstant e1, EnumConstant e2, EnumConstant e3
53+
where
54+
if first3(_, switch, _, _, _)
55+
then first3(msg, switch, e1, e2, e3)
56+
else
57+
if only2(_, switch, _, _)
58+
then (
59+
only2(msg, switch, e1, e2) and e1 = e3
60+
) else (
61+
only1(msg, switch, e1) and e1 = e2 and e1 = e3
62+
)
63+
select switch, msg, e1, e1.getName(), e2, e2.getName(), e3, e3.getName()

0 commit comments

Comments
 (0)