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.
This commit is contained in:
Ian Lynagh
2024-03-18 15:56:21 +00:00
parent 916b1e959e
commit 60b5e49905

View File

@@ -13,10 +13,51 @@
import java
from SwitchStmt switch, EnumType enum, EnumConstant missing
where
switch.getExpr().getType() = enum and
missing.getDeclaringType() = enum and
EnumConstant nthMissing(SwitchStmt switch, int index) {
not exists(switch.getDefaultCase()) and
not switch.getAConstCase().getValue() = missing.getAnAccess()
select switch, "Switch statement does not have a case for $@.", missing, missing.getName()
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()