Skip to content

Commit f8058e4

Browse files
authored
Merge pull request #17915 from hvitved/rust/pat-literal-cfg
Rust: Include literals in the CFG for literal patterns
2 parents 6054855 + 9392bf7 commit f8058e4

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ module ExprTrees {
611611
* Provides `ControlFlowTree`s for patterns.
612612
*
613613
* Since patterns destruct values, they are modeled in pre-order, except for
614-
* `OrPat`s and `IdentPat`s.
614+
* `LiteralPat`s, `OrPat`s, and `IdentPat`s.
615615
*/
616616
module PatternTrees {
617617
abstract class StandardPatTree extends StandardTree {
@@ -665,7 +665,9 @@ module PatternTrees {
665665

666666
class RestPatTree extends LeafTree, RestPat { }
667667

668-
class LiteralPatTree extends LeafTree, LiteralPat { }
668+
class LiteralPatTree extends StandardPostOrderTree, LiteralPat {
669+
override AstNode getChildNode(int i) { i = 0 and result = this.getLiteral() }
670+
}
669671

670672
class MacroPatTree extends PreOrderPatTree, MacroPat {
671673
override Pat getPat(int i) { i = 0 and result = this.getMacroCall().getExpanded() }

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ edges
313313
| test.rs:153:9:160:9 | IfExpr | test.rs:152:44:161:5 | BlockExpr | |
314314
| test.rs:153:13:156:9 | [boolean(false)] MatchExpr | test.rs:159:13:159:13 | 0 | false |
315315
| test.rs:153:13:156:9 | [boolean(true)] MatchExpr | test.rs:157:13:157:13 | 1 | true |
316-
| test.rs:153:19:153:19 | a | test.rs:154:13:154:13 | LiteralPat | |
316+
| test.rs:153:19:153:19 | a | test.rs:154:13:154:13 | 0 | |
317+
| test.rs:154:13:154:13 | 0 | test.rs:154:13:154:13 | LiteralPat | |
317318
| test.rs:154:13:154:13 | LiteralPat | test.rs:154:18:154:21 | true | match |
318319
| test.rs:154:13:154:13 | LiteralPat | test.rs:155:13:155:13 | WildcardPat | no-match |
319320
| test.rs:154:18:154:21 | true | test.rs:153:13:156:9 | [boolean(true)] MatchExpr | true |
@@ -590,13 +591,15 @@ edges
590591
| test.rs:274:9:277:9 | MatchExpr | test.rs:273:71:278:5 | BlockExpr | |
591592
| test.rs:274:15:274:15 | b | test.rs:274:15:274:16 | TryExpr | |
592593
| test.rs:274:15:274:16 | TryExpr | test.rs:273:5:278:5 | exit test_question_mark_operator_2 (normal) | return |
593-
| test.rs:274:15:274:16 | TryExpr | test.rs:275:13:275:16 | LiteralPat | match |
594+
| test.rs:274:15:274:16 | TryExpr | test.rs:275:13:275:16 | true | match |
594595
| test.rs:275:13:275:16 | LiteralPat | test.rs:275:21:275:24 | PathExpr | match |
595-
| test.rs:275:13:275:16 | LiteralPat | test.rs:276:13:276:17 | LiteralPat | no-match |
596+
| test.rs:275:13:275:16 | LiteralPat | test.rs:276:13:276:17 | false | no-match |
597+
| test.rs:275:13:275:16 | true | test.rs:275:13:275:16 | LiteralPat | |
596598
| test.rs:275:21:275:24 | PathExpr | test.rs:275:26:275:30 | false | |
597599
| test.rs:275:21:275:31 | CallExpr | test.rs:274:9:277:9 | MatchExpr | |
598600
| test.rs:275:26:275:30 | false | test.rs:275:21:275:31 | CallExpr | |
599601
| test.rs:276:13:276:17 | LiteralPat | test.rs:276:22:276:25 | PathExpr | match |
602+
| test.rs:276:13:276:17 | false | test.rs:276:13:276:17 | LiteralPat | |
600603
| test.rs:276:22:276:25 | PathExpr | test.rs:276:27:276:30 | true | |
601604
| test.rs:276:22:276:31 | CallExpr | test.rs:274:9:277:9 | MatchExpr | |
602605
| test.rs:276:27:276:30 | true | test.rs:276:22:276:31 | CallExpr | |
@@ -735,21 +738,25 @@ edges
735738
| test.rs:348:31:355:5 | BlockExpr | test.rs:348:5:355:5 | exit range_pattern (normal) | |
736739
| test.rs:349:9:354:9 | MatchExpr | test.rs:348:31:355:5 | BlockExpr | |
737740
| test.rs:349:15:349:16 | 42 | test.rs:350:13:350:15 | RangePat | |
738-
| test.rs:350:13:350:15 | RangePat | test.rs:350:15:350:15 | LiteralPat | match |
741+
| test.rs:350:13:350:15 | RangePat | test.rs:350:15:350:15 | 0 | match |
739742
| test.rs:350:13:350:15 | RangePat | test.rs:351:13:351:16 | RangePat | no-match |
743+
| test.rs:350:15:350:15 | 0 | test.rs:350:15:350:15 | LiteralPat | |
740744
| test.rs:350:15:350:15 | LiteralPat | test.rs:350:20:350:20 | 1 | match |
741745
| test.rs:350:15:350:15 | LiteralPat | test.rs:351:13:351:16 | RangePat | no-match |
742746
| test.rs:350:20:350:20 | 1 | test.rs:349:9:354:9 | MatchExpr | |
743-
| test.rs:351:13:351:13 | LiteralPat | test.rs:351:16:351:16 | LiteralPat | match |
747+
| test.rs:351:13:351:13 | 1 | test.rs:351:13:351:13 | LiteralPat | |
748+
| test.rs:351:13:351:13 | LiteralPat | test.rs:351:16:351:16 | 2 | match |
744749
| test.rs:351:13:351:13 | LiteralPat | test.rs:352:13:352:15 | RangePat | no-match |
745-
| test.rs:351:13:351:16 | RangePat | test.rs:351:13:351:13 | LiteralPat | match |
750+
| test.rs:351:13:351:16 | RangePat | test.rs:351:13:351:13 | 1 | match |
746751
| test.rs:351:13:351:16 | RangePat | test.rs:352:13:352:15 | RangePat | no-match |
752+
| test.rs:351:16:351:16 | 2 | test.rs:351:16:351:16 | LiteralPat | |
747753
| test.rs:351:16:351:16 | LiteralPat | test.rs:351:21:351:21 | 2 | match |
748754
| test.rs:351:16:351:16 | LiteralPat | test.rs:352:13:352:15 | RangePat | no-match |
749755
| test.rs:351:21:351:21 | 2 | test.rs:349:9:354:9 | MatchExpr | |
756+
| test.rs:352:13:352:13 | 5 | test.rs:352:13:352:13 | LiteralPat | |
750757
| test.rs:352:13:352:13 | LiteralPat | test.rs:352:20:352:20 | 3 | match |
751758
| test.rs:352:13:352:13 | LiteralPat | test.rs:353:13:353:14 | RestPat | no-match |
752-
| test.rs:352:13:352:15 | RangePat | test.rs:352:13:352:13 | LiteralPat | match |
759+
| test.rs:352:13:352:15 | RangePat | test.rs:352:13:352:13 | 5 | match |
753760
| test.rs:352:13:352:15 | RangePat | test.rs:353:13:353:14 | RestPat | no-match |
754761
| test.rs:352:20:352:20 | 3 | test.rs:349:9:354:9 | MatchExpr | |
755762
| test.rs:353:13:353:14 | RestPat | test.rs:353:19:353:19 | 4 | match |

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,9 @@ edges
260260
| variables.rs:114:5:122:5 | ExprStmt | variables.rs:114:11:114:12 | x6 | |
261261
| variables.rs:114:5:122:5 | MatchExpr | variables.rs:124:5:124:18 | ExprStmt | |
262262
| variables.rs:114:11:114:12 | x6 | variables.rs:115:9:115:16 | TupleStructPat | |
263-
| variables.rs:115:9:115:16 | TupleStructPat | variables.rs:115:14:115:15 | LiteralPat | match |
263+
| variables.rs:115:9:115:16 | TupleStructPat | variables.rs:115:14:115:15 | 50 | match |
264264
| variables.rs:115:9:115:16 | TupleStructPat | variables.rs:116:9:116:16 | TupleStructPat | no-match |
265+
| variables.rs:115:14:115:15 | 50 | variables.rs:115:14:115:15 | LiteralPat | |
265266
| variables.rs:115:14:115:15 | LiteralPat | variables.rs:115:21:115:29 | PathExpr | match |
266267
| variables.rs:115:14:115:15 | LiteralPat | variables.rs:116:9:116:16 | TupleStructPat | no-match |
267268
| variables.rs:115:21:115:29 | PathExpr | variables.rs:115:31:115:38 | "Got 50" | |
@@ -358,21 +359,25 @@ edges
358359
| variables.rs:172:9:174:9 | RecordPat | variables.rs:173:31:173:35 | RangePat | match |
359360
| variables.rs:172:9:174:9 | RecordPat | variables.rs:175:9:175:38 | RecordPat | no-match |
360361
| variables.rs:173:17:173:35 | [match(true)] id_variable | variables.rs:174:14:174:22 | PathExpr | match |
361-
| variables.rs:173:31:173:31 | LiteralPat | variables.rs:173:35:173:35 | LiteralPat | match |
362+
| variables.rs:173:31:173:31 | 3 | variables.rs:173:31:173:31 | LiteralPat | |
363+
| variables.rs:173:31:173:31 | LiteralPat | variables.rs:173:35:173:35 | 7 | match |
362364
| variables.rs:173:31:173:31 | LiteralPat | variables.rs:175:9:175:38 | RecordPat | no-match |
363-
| variables.rs:173:31:173:35 | RangePat | variables.rs:173:31:173:31 | LiteralPat | match |
365+
| variables.rs:173:31:173:35 | RangePat | variables.rs:173:31:173:31 | 3 | match |
364366
| variables.rs:173:31:173:35 | RangePat | variables.rs:175:9:175:38 | RecordPat | no-match |
367+
| variables.rs:173:35:173:35 | 7 | variables.rs:173:35:173:35 | LiteralPat | |
365368
| variables.rs:173:35:173:35 | LiteralPat | variables.rs:173:17:173:35 | [match(true)] id_variable | match |
366369
| variables.rs:173:35:173:35 | LiteralPat | variables.rs:175:9:175:38 | RecordPat | no-match |
367370
| variables.rs:174:14:174:22 | PathExpr | variables.rs:174:24:174:34 | id_variable | |
368371
| variables.rs:174:14:174:35 | CallExpr | variables.rs:171:5:180:5 | MatchExpr | |
369372
| variables.rs:174:24:174:34 | id_variable | variables.rs:174:14:174:35 | CallExpr | |
370373
| variables.rs:175:9:175:38 | RecordPat | variables.rs:175:30:175:36 | RangePat | match |
371374
| variables.rs:175:9:175:38 | RecordPat | variables.rs:178:9:178:29 | RecordPat | no-match |
372-
| variables.rs:175:30:175:31 | LiteralPat | variables.rs:175:35:175:36 | LiteralPat | match |
375+
| variables.rs:175:30:175:31 | 10 | variables.rs:175:30:175:31 | LiteralPat | |
376+
| variables.rs:175:30:175:31 | LiteralPat | variables.rs:175:35:175:36 | 12 | match |
373377
| variables.rs:175:30:175:31 | LiteralPat | variables.rs:178:9:178:29 | RecordPat | no-match |
374-
| variables.rs:175:30:175:36 | RangePat | variables.rs:175:30:175:31 | LiteralPat | match |
378+
| variables.rs:175:30:175:36 | RangePat | variables.rs:175:30:175:31 | 10 | match |
375379
| variables.rs:175:30:175:36 | RangePat | variables.rs:178:9:178:29 | RecordPat | no-match |
380+
| variables.rs:175:35:175:36 | 12 | variables.rs:175:35:175:36 | LiteralPat | |
376381
| variables.rs:175:35:175:36 | LiteralPat | variables.rs:176:22:176:51 | MacroStmts | match |
377382
| variables.rs:175:35:175:36 | LiteralPat | variables.rs:178:9:178:29 | RecordPat | no-match |
378383
| variables.rs:175:43:177:9 | BlockExpr | variables.rs:171:5:180:5 | MatchExpr | |

0 commit comments

Comments
 (0)