Skip to content

Commit 38c1472

Browse files
committed
Java: Fix CFG to include exhaustiveness requirement.
1 parent 5f6d750 commit 38c1472

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

java/ql/lib/semmle/code/java/ControlFlowGraph.qll

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -449,10 +449,17 @@ private module Input implements InputSig1, InputSig2 {
449449
predicate matchAll(Ast::Case c) {
450450
c instanceof DefaultCase
451451
or
452-
// Switch expressions must be exhaustive, so the last case matches all remaining values
453-
exists(SwitchExpr switchExpr, int last |
454-
switchExpr.getCase(last) = c and
455-
not exists(switchExpr.getCase(last + 1))
452+
// Switch expressions and enhanced switch blocks (those that use pattern
453+
// cases or match null) must be exhaustive, so the last case matches all
454+
// remaining values.
455+
// See https://docs.oracle.com/javase/specs/jls/se21/html/jls-14.html#jls-14.11.2
456+
exists(Ast::Switch switch, int last |
457+
switch instanceof SwitchExpr or
458+
exists(switch.(SwitchStmt).getAPatternCase()) or
459+
switch.(SwitchStmt).hasNullCase()
460+
|
461+
switch.getCase(last) = c and
462+
not exists(switch.getCase(last + 1))
456463
)
457464
or
458465
c.(J::PatternCase).getAPattern().getType() instanceof TypeObject

0 commit comments

Comments
 (0)