Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Java: Limit the amount of results that MissingEnumInSwitch produces p…
…er 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.
  • Loading branch information
igfoo committed Mar 18, 2024
commit 60b5e499050d75587bf65f5e0e3877caccd326a3
53 changes: 47 additions & 6 deletions java/ql/src/Likely Bugs/Statements/MissingEnumInSwitch.ql
Original file line number Diff line number Diff line change
Expand Up @@ -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 $@."
Comment thread
igfoo marked this conversation as resolved.
Outdated
) 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()