|
13 | 13 |
|
14 | 14 | import java
|
15 | 15 |
|
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) { |
20 | 17 | 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