Skip to content

Commit 5f36630

Browse files
committed
Rust: Remove spurious CFG edges in match expressions
1 parent 3b753da commit 5f36630

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

rust/ql/lib/codeql/rust/controlflow/internal/ControlFlowGraphImpl.qll

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -356,24 +356,27 @@ class MatchArmTree extends ControlFlowTree instanceof MatchArm {
356356

357357
class MatchExprTree extends PostOrderTree instanceof MatchExpr {
358358
override predicate propagatesAbnormal(AstNode child) {
359-
child = [super.getExpr(), super.getMatchArmList().getAnArm().getExpr()]
359+
child = [super.getExpr(), super.getAnArm().getExpr()]
360360
}
361361

362362
override predicate first(AstNode node) { first(super.getExpr(), node) }
363363

364364
override predicate succ(AstNode pred, AstNode succ, Completion c) {
365365
// Edge from the scrutinee to the first arm.
366-
last(super.getExpr(), pred, c) and succ = super.getMatchArmList().getArm(0).getPat()
366+
last(super.getExpr(), pred, c) and succ = super.getArm(0).getPat()
367367
or
368368
// Edge from a failed match/guard in one arm to the beginning of the next arm.
369369
exists(int i |
370-
last(super.getMatchArmList().getArm(i), pred, c) and
371-
first(super.getMatchArmList().getArm(i + 1), succ) and
370+
last(super.getArm(i), pred, c) and
371+
first(super.getArm(i + 1), succ) and
372372
c.(ConditionalCompletion).failed()
373373
)
374374
or
375375
// Edge from the end of each arm to the match expression.
376-
last(super.getMatchArmList().getArm(_), pred, c) and succ = this and completionIsNormal(c)
376+
last(super.getArm(_).getExpr(), pred, c) and succ = this and completionIsNormal(c)
377+
or
378+
// Edge from the end of last arm to the match expression.
379+
last(super.getLastArm().getExpr(), pred, c) and succ = this and completionIsNormal(c)
377380
}
378381
}
379382

rust/ql/lib/codeql/rust/elements/internal/MatchExprImpl.qll

Lines changed: 21 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/ql/test/library-tests/controlflow/Cfg.expected

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,9 @@
181181
| test.rs:94:12:94:46 | ParenExpr | test.rs:97:13:97:13 | LiteralExpr | false |
182182
| test.rs:94:13:94:45 | MatchExpr | test.rs:94:12:94:46 | ParenExpr | |
183183
| test.rs:94:19:94:19 | PathExpr | test.rs:94:23:94:23 | LiteralPat | |
184-
| test.rs:94:23:94:23 | LiteralPat | test.rs:94:13:94:45 | MatchExpr | no-match |
185184
| test.rs:94:23:94:23 | LiteralPat | test.rs:94:28:94:31 | LiteralExpr | match |
186185
| test.rs:94:23:94:23 | LiteralPat | test.rs:94:34:94:34 | WildcardPat | no-match |
187186
| test.rs:94:28:94:31 | LiteralExpr | test.rs:94:13:94:45 | MatchExpr | |
188-
| test.rs:94:34:94:34 | WildcardPat | test.rs:94:13:94:45 | MatchExpr | no-match |
189187
| test.rs:94:34:94:34 | WildcardPat | test.rs:94:39:94:43 | LiteralExpr | match |
190188
| test.rs:94:39:94:43 | LiteralExpr | test.rs:94:13:94:45 | MatchExpr | |
191189
| test.rs:94:48:96:9 | BlockExpr | test.rs:94:9:98:9 | IfExpr | |
@@ -239,16 +237,13 @@
239237
| test.rs:122:44:128:1 | BlockExpr | test.rs:122:1:128:1 | exit test_match (normal) | |
240238
| test.rs:123:5:127:5 | MatchExpr | test.rs:122:44:128:1 | BlockExpr | |
241239
| test.rs:123:11:123:21 | PathExpr | test.rs:124:9:124:23 | TupleStructPat | |
242-
| test.rs:124:9:124:23 | TupleStructPat | test.rs:123:5:127:5 | MatchExpr | no-match |
243240
| test.rs:124:9:124:23 | TupleStructPat | test.rs:124:28:124:28 | PathExpr | match |
244241
| test.rs:124:9:124:23 | TupleStructPat | test.rs:125:9:125:23 | TupleStructPat | no-match |
245242
| test.rs:124:28:124:28 | PathExpr | test.rs:124:32:124:33 | LiteralExpr | |
246243
| test.rs:124:32:124:33 | LiteralExpr | test.rs:124:28:124:33 | BinaryExpr | |
247-
| test.rs:125:9:125:23 | TupleStructPat | test.rs:123:5:127:5 | MatchExpr | no-match |
248244
| test.rs:125:9:125:23 | TupleStructPat | test.rs:125:28:125:28 | PathExpr | match |
249245
| test.rs:125:9:125:23 | TupleStructPat | test.rs:126:9:126:20 | PathPat | no-match |
250246
| test.rs:125:28:125:28 | PathExpr | test.rs:123:5:127:5 | MatchExpr | |
251-
| test.rs:126:9:126:20 | PathPat | test.rs:123:5:127:5 | MatchExpr | no-match |
252247
| test.rs:126:9:126:20 | PathPat | test.rs:126:25:126:25 | LiteralExpr | match |
253248
| test.rs:126:25:126:25 | LiteralExpr | test.rs:123:5:127:5 | MatchExpr | |
254249
| test.rs:131:5:136:5 | enter test_infinite_loop | test.rs:132:9:134:9 | ExprStmt | |

0 commit comments

Comments
 (0)