Skip to content

Commit df2a110

Browse files
committed
Rust: Changes based on PR comments
1 parent 65bce9d commit df2a110

File tree

5 files changed

+72
-21
lines changed

5 files changed

+72
-21
lines changed

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

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -122,23 +122,17 @@ class BooleanCompletion extends ConditionalCompletion, TBooleanCompletion {
122122
* does not mean that `pat` is irrefutable, as its children might be the cause
123123
* of a failure.
124124
*/
125-
private predicate canCauseMatchFailure(Pat pat) {
126-
pat instanceof LiteralPat
127-
or
128-
// NOTE: a `TupleStructPat` can cause a failure if it resolves to a an enum
129-
// variant but not when it resolves to a tuple struct.
130-
pat instanceof TupleStructPat
131-
or
132-
pat instanceof SlicePat
133-
or
134-
pat instanceof PathPat
135-
or
136-
pat instanceof OrPat
137-
or
125+
private predicate cannotCauseMatchFailure(Pat pat) {
126+
pat instanceof RangePat or
138127
// Identifier patterns that are in fact path patterns can cause failures. For
139-
// instance `None`. Only if a `@ ...` part is present can we be sure that it's
140-
// an actual identifier pattern.
141-
pat = any(IdentPat p | not p.hasPat())
128+
// instance `None`. Only if an `@ ...` part is present can we be sure that
129+
// it's an actual identifier pattern.
130+
pat = any(IdentPat p | p.hasPat()) or
131+
pat instanceof WildcardPat or
132+
pat instanceof RestPat or
133+
pat instanceof RefPat or
134+
pat instanceof TuplePat or
135+
pat instanceof MacroPat
142136
}
143137

144138
/**
@@ -168,7 +162,7 @@ private predicate guaranteedMatchPosition(Pat pat) {
168162
}
169163

170164
private predicate guaranteedMatch(Pat pat) {
171-
(not canCauseMatchFailure(pat) or guaranteedMatchPosition(pat)) and
165+
(cannotCauseMatchFailure(pat) or guaranteedMatchPosition(pat)) and
172166
// In `for` loops we use a no-match edge from the pattern to terminate the
173167
// loop, hence we special case and always allow the no-match edge.
174168
not pat = any(ForExpr for).getPat()

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ module Impl {
1818
*/
1919
class Pat extends Generated::Pat {
2020
/**
21-
* If this pattern is immediately nested within another pattern, then get the
22-
* parent pattern.
21+
* Gets the pattern under which this pattern is immediately nested, if any.
2322
*/
2423
Pat getParentPat() {
2524
result = getImmediateParent(this)

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

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,9 +612,12 @@ dominates
612612
| test.rs:387:5:391:5 | enter fn empty_struct_pattern | test.rs:387:5:391:5 | enter fn empty_struct_pattern |
613613
| test.rs:393:5:398:5 | enter fn struct_pattern | test.rs:393:5:398:5 | enter fn struct_pattern |
614614
| test.rs:393:5:398:5 | enter fn struct_pattern | test.rs:394:9:397:9 | match st { ... } |
615+
| test.rs:393:5:398:5 | enter fn struct_pattern | test.rs:395:27:395:27 | 1 |
615616
| test.rs:393:5:398:5 | enter fn struct_pattern | test.rs:395:34:395:34 | 0 |
616617
| test.rs:393:5:398:5 | enter fn struct_pattern | test.rs:396:13:396:26 | MyStruct {...} |
617618
| test.rs:394:9:397:9 | match st { ... } | test.rs:394:9:397:9 | match st { ... } |
619+
| test.rs:395:27:395:27 | 1 | test.rs:395:27:395:27 | 1 |
620+
| test.rs:395:27:395:27 | 1 | test.rs:395:34:395:34 | 0 |
618621
| test.rs:395:34:395:34 | 0 | test.rs:395:34:395:34 | 0 |
619622
| test.rs:396:13:396:26 | MyStruct {...} | test.rs:396:13:396:26 | MyStruct {...} |
620623
| test.rs:400:5:407:5 | enter fn range_pattern | test.rs:400:5:407:5 | enter fn range_pattern |
@@ -727,11 +730,26 @@ dominates
727730
| test.rs:460:5:465:5 | enter fn irrefutable_pattern_and_dead_code | test.rs:460:5:465:5 | enter fn irrefutable_pattern_and_dead_code |
728731
| test.rs:460:5:465:5 | enter fn irrefutable_pattern_and_dead_code | test.rs:461:9:464:9 | match pair { ... } |
729732
| test.rs:460:5:465:5 | enter fn irrefutable_pattern_and_dead_code | test.rs:462:18:462:34 | MyStruct {...} |
733+
| test.rs:460:5:465:5 | enter fn irrefutable_pattern_and_dead_code | test.rs:462:32:462:32 | _ |
730734
| test.rs:460:5:465:5 | enter fn irrefutable_pattern_and_dead_code | test.rs:463:13:463:13 | _ |
731735
| test.rs:461:9:464:9 | match pair { ... } | test.rs:461:9:464:9 | match pair { ... } |
732736
| test.rs:462:18:462:34 | MyStruct {...} | test.rs:462:18:462:34 | MyStruct {...} |
737+
| test.rs:462:18:462:34 | MyStruct {...} | test.rs:462:32:462:32 | _ |
738+
| test.rs:462:32:462:32 | _ | test.rs:462:32:462:32 | _ |
733739
| test.rs:463:13:463:13 | _ | test.rs:463:13:463:13 | _ |
734740
| test.rs:475:5:481:5 | enter fn enum_pattern | test.rs:475:5:481:5 | enter fn enum_pattern |
741+
| test.rs:475:5:481:5 | enter fn enum_pattern | test.rs:476:9:480:9 | match e { ... } |
742+
| test.rs:475:5:481:5 | enter fn enum_pattern | test.rs:477:32:477:32 | _ |
743+
| test.rs:475:5:481:5 | enter fn enum_pattern | test.rs:478:13:478:27 | TupleVariant(...) |
744+
| test.rs:475:5:481:5 | enter fn enum_pattern | test.rs:478:26:478:26 | _ |
745+
| test.rs:475:5:481:5 | enter fn enum_pattern | test.rs:479:13:479:23 | UnitVariant |
746+
| test.rs:476:9:480:9 | match e { ... } | test.rs:476:9:480:9 | match e { ... } |
747+
| test.rs:477:32:477:32 | _ | test.rs:477:32:477:32 | _ |
748+
| test.rs:478:13:478:27 | TupleVariant(...) | test.rs:478:13:478:27 | TupleVariant(...) |
749+
| test.rs:478:13:478:27 | TupleVariant(...) | test.rs:478:26:478:26 | _ |
750+
| test.rs:478:13:478:27 | TupleVariant(...) | test.rs:479:13:479:23 | UnitVariant |
751+
| test.rs:478:26:478:26 | _ | test.rs:478:26:478:26 | _ |
752+
| test.rs:479:13:479:23 | UnitVariant | test.rs:479:13:479:23 | UnitVariant |
735753
| test.rs:485:5:490:5 | enter fn test_infinite_loop | test.rs:485:5:490:5 | enter fn test_infinite_loop |
736754
| test.rs:485:5:490:5 | enter fn test_infinite_loop | test.rs:487:13:487:14 | TupleExpr |
737755
| test.rs:487:13:487:14 | TupleExpr | test.rs:487:13:487:14 | TupleExpr |
@@ -1319,8 +1337,10 @@ postDominance
13191337
| test.rs:393:5:398:5 | enter fn struct_pattern | test.rs:393:5:398:5 | enter fn struct_pattern |
13201338
| test.rs:394:9:397:9 | match st { ... } | test.rs:393:5:398:5 | enter fn struct_pattern |
13211339
| test.rs:394:9:397:9 | match st { ... } | test.rs:394:9:397:9 | match st { ... } |
1340+
| test.rs:394:9:397:9 | match st { ... } | test.rs:395:27:395:27 | 1 |
13221341
| test.rs:394:9:397:9 | match st { ... } | test.rs:395:34:395:34 | 0 |
13231342
| test.rs:394:9:397:9 | match st { ... } | test.rs:396:13:396:26 | MyStruct {...} |
1343+
| test.rs:395:27:395:27 | 1 | test.rs:395:27:395:27 | 1 |
13241344
| test.rs:395:34:395:34 | 0 | test.rs:395:34:395:34 | 0 |
13251345
| test.rs:396:13:396:26 | MyStruct {...} | test.rs:396:13:396:26 | MyStruct {...} |
13261346
| test.rs:400:5:407:5 | enter fn range_pattern | test.rs:400:5:407:5 | enter fn range_pattern |
@@ -1416,10 +1436,22 @@ postDominance
14161436
| test.rs:461:9:464:9 | match pair { ... } | test.rs:460:5:465:5 | enter fn irrefutable_pattern_and_dead_code |
14171437
| test.rs:461:9:464:9 | match pair { ... } | test.rs:461:9:464:9 | match pair { ... } |
14181438
| test.rs:461:9:464:9 | match pair { ... } | test.rs:462:18:462:34 | MyStruct {...} |
1439+
| test.rs:461:9:464:9 | match pair { ... } | test.rs:462:32:462:32 | _ |
14191440
| test.rs:461:9:464:9 | match pair { ... } | test.rs:463:13:463:13 | _ |
14201441
| test.rs:462:18:462:34 | MyStruct {...} | test.rs:462:18:462:34 | MyStruct {...} |
1442+
| test.rs:462:32:462:32 | _ | test.rs:462:32:462:32 | _ |
14211443
| test.rs:463:13:463:13 | _ | test.rs:463:13:463:13 | _ |
14221444
| test.rs:475:5:481:5 | enter fn enum_pattern | test.rs:475:5:481:5 | enter fn enum_pattern |
1445+
| test.rs:476:9:480:9 | match e { ... } | test.rs:475:5:481:5 | enter fn enum_pattern |
1446+
| test.rs:476:9:480:9 | match e { ... } | test.rs:476:9:480:9 | match e { ... } |
1447+
| test.rs:476:9:480:9 | match e { ... } | test.rs:477:32:477:32 | _ |
1448+
| test.rs:476:9:480:9 | match e { ... } | test.rs:478:13:478:27 | TupleVariant(...) |
1449+
| test.rs:476:9:480:9 | match e { ... } | test.rs:478:26:478:26 | _ |
1450+
| test.rs:476:9:480:9 | match e { ... } | test.rs:479:13:479:23 | UnitVariant |
1451+
| test.rs:477:32:477:32 | _ | test.rs:477:32:477:32 | _ |
1452+
| test.rs:478:13:478:27 | TupleVariant(...) | test.rs:478:13:478:27 | TupleVariant(...) |
1453+
| test.rs:478:26:478:26 | _ | test.rs:478:26:478:26 | _ |
1454+
| test.rs:479:13:479:23 | UnitVariant | test.rs:479:13:479:23 | UnitVariant |
14231455
| test.rs:485:5:490:5 | enter fn test_infinite_loop | test.rs:485:5:490:5 | enter fn test_infinite_loop |
14241456
| test.rs:487:13:487:14 | TupleExpr | test.rs:487:13:487:14 | TupleExpr |
14251457
| test.rs:494:5:496:5 | enter fn say_hello | test.rs:494:5:496:5 | enter fn say_hello |
@@ -1682,7 +1714,8 @@ immediateDominator
16821714
| test.rs:369:26:369:28 | ret | test.rs:369:18:369:20 | ret |
16831715
| test.rs:370:13:370:16 | None | test.rs:367:5:373:5 | enter fn test_let_with_return |
16841716
| test.rs:394:9:397:9 | match st { ... } | test.rs:393:5:398:5 | enter fn struct_pattern |
1685-
| test.rs:395:34:395:34 | 0 | test.rs:393:5:398:5 | enter fn struct_pattern |
1717+
| test.rs:395:27:395:27 | 1 | test.rs:393:5:398:5 | enter fn struct_pattern |
1718+
| test.rs:395:34:395:34 | 0 | test.rs:395:27:395:27 | 1 |
16861719
| test.rs:396:13:396:26 | MyStruct {...} | test.rs:393:5:398:5 | enter fn struct_pattern |
16871720
| test.rs:401:9:406:9 | match 42 { ... } | test.rs:400:5:407:5 | enter fn range_pattern |
16881721
| test.rs:402:20:402:20 | 1 | test.rs:400:5:407:5 | enter fn range_pattern |
@@ -1725,7 +1758,13 @@ immediateDominator
17251758
| test.rs:456:13:456:13 | _ | test.rs:455:13:455:25 | [match(false)] 1 \| 2 |
17261759
| test.rs:461:9:464:9 | match pair { ... } | test.rs:460:5:465:5 | enter fn irrefutable_pattern_and_dead_code |
17271760
| test.rs:462:18:462:34 | MyStruct {...} | test.rs:460:5:465:5 | enter fn irrefutable_pattern_and_dead_code |
1761+
| test.rs:462:32:462:32 | _ | test.rs:462:18:462:34 | MyStruct {...} |
17281762
| test.rs:463:13:463:13 | _ | test.rs:460:5:465:5 | enter fn irrefutable_pattern_and_dead_code |
1763+
| test.rs:476:9:480:9 | match e { ... } | test.rs:475:5:481:5 | enter fn enum_pattern |
1764+
| test.rs:477:32:477:32 | _ | test.rs:475:5:481:5 | enter fn enum_pattern |
1765+
| test.rs:478:13:478:27 | TupleVariant(...) | test.rs:475:5:481:5 | enter fn enum_pattern |
1766+
| test.rs:478:26:478:26 | _ | test.rs:478:13:478:27 | TupleVariant(...) |
1767+
| test.rs:479:13:479:23 | UnitVariant | test.rs:478:13:478:27 | TupleVariant(...) |
17291768
| test.rs:487:13:487:14 | TupleExpr | test.rs:485:5:490:5 | enter fn test_infinite_loop |
17301769
| test.rs:511:28:516:9 | exit { ... } (normal) | test.rs:511:28:516:9 | enter { ... } |
17311770
| test.rs:512:13:514:13 | if b {...} | test.rs:511:28:516:9 | enter { ... } |
@@ -2236,6 +2275,8 @@ joinBlockPredecessor
22362275
| test.rs:370:13:370:16 | None | test.rs:369:18:369:20 | ret | 0 |
22372276
| test.rs:394:9:397:9 | match st { ... } | test.rs:395:34:395:34 | 0 | 0 |
22382277
| test.rs:394:9:397:9 | match st { ... } | test.rs:396:13:396:26 | MyStruct {...} | 1 |
2278+
| test.rs:396:13:396:26 | MyStruct {...} | test.rs:393:5:398:5 | enter fn struct_pattern | 1 |
2279+
| test.rs:396:13:396:26 | MyStruct {...} | test.rs:395:27:395:27 | 1 | 0 |
22392280
| test.rs:401:9:406:9 | match 42 { ... } | test.rs:402:20:402:20 | 1 | 0 |
22402281
| test.rs:401:9:406:9 | match 42 { ... } | test.rs:403:21:403:21 | 2 | 1 |
22412282
| test.rs:401:9:406:9 | match 42 { ... } | test.rs:404:20:404:20 | 3 | 2 |
@@ -2270,8 +2311,13 @@ joinBlockPredecessor
22702311
| test.rs:455:13:455:25 | [match(true)] 1 \| 2 | test.rs:455:13:455:25 | 2 | 0 |
22712312
| test.rs:455:13:455:25 | one_or_two!... | test.rs:455:13:455:25 | [match(false)] 1 \| 2 | 0 |
22722313
| test.rs:455:13:455:25 | one_or_two!... | test.rs:455:13:455:25 | [match(true)] 1 \| 2 | 1 |
2273-
| test.rs:461:9:464:9 | match pair { ... } | test.rs:462:18:462:34 | MyStruct {...} | 0 |
2314+
| test.rs:461:9:464:9 | match pair { ... } | test.rs:462:32:462:32 | _ | 0 |
22742315
| test.rs:461:9:464:9 | match pair { ... } | test.rs:463:13:463:13 | _ | 1 |
2316+
| test.rs:463:13:463:13 | _ | test.rs:460:5:465:5 | enter fn irrefutable_pattern_and_dead_code | 1 |
2317+
| test.rs:463:13:463:13 | _ | test.rs:462:18:462:34 | MyStruct {...} | 0 |
2318+
| test.rs:476:9:480:9 | match e { ... } | test.rs:477:32:477:32 | _ | 0 |
2319+
| test.rs:476:9:480:9 | match e { ... } | test.rs:478:26:478:26 | _ | 1 |
2320+
| test.rs:476:9:480:9 | match e { ... } | test.rs:479:13:479:23 | UnitVariant | 2 |
22752321
| test.rs:487:13:487:14 | TupleExpr | test.rs:485:5:490:5 | enter fn test_infinite_loop | 1 |
22762322
| test.rs:487:13:487:14 | TupleExpr | test.rs:487:13:487:14 | TupleExpr | 0 |
22772323
| test.rs:511:28:516:9 | exit { ... } (normal) | test.rs:512:13:514:13 | if b {...} | 1 |

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,7 @@ edges
969969
| test.rs:394:9:397:9 | match st { ... } | test.rs:393:44:398:5 | { ... } | |
970970
| test.rs:394:15:394:16 | st | test.rs:395:13:395:29 | MyStruct {...} | |
971971
| test.rs:395:13:395:29 | MyStruct {...} | test.rs:395:27:395:27 | 1 | match |
972+
| test.rs:395:13:395:29 | MyStruct {...} | test.rs:396:13:396:26 | MyStruct {...} | no-match |
972973
| test.rs:395:27:395:27 | 1 | test.rs:395:27:395:27 | 1 | |
973974
| test.rs:395:27:395:27 | 1 | test.rs:395:34:395:34 | 0 | match |
974975
| test.rs:395:27:395:27 | 1 | test.rs:396:13:396:26 | MyStruct {...} | no-match |
@@ -1159,6 +1160,7 @@ edges
11591160
| test.rs:462:15:462:15 | n | test.rs:462:18:462:34 | MyStruct {...} | match |
11601161
| test.rs:462:15:462:15 | n | test.rs:463:13:463:13 | _ | no-match |
11611162
| test.rs:462:18:462:34 | MyStruct {...} | test.rs:462:32:462:32 | _ | match |
1163+
| test.rs:462:18:462:34 | MyStruct {...} | test.rs:463:13:463:13 | _ | no-match |
11621164
| test.rs:462:32:462:32 | _ | test.rs:462:40:462:40 | n | match |
11631165
| test.rs:462:40:462:40 | n | test.rs:461:9:464:9 | match pair { ... } | |
11641166
| test.rs:463:13:463:13 | _ | test.rs:463:18:463:18 | 0 | match |
@@ -1172,8 +1174,16 @@ edges
11721174
| test.rs:476:9:480:9 | match e { ... } | test.rs:475:39:481:5 | { ... } | |
11731175
| test.rs:476:15:476:15 | e | test.rs:477:13:477:34 | StructVariant {...} | |
11741176
| test.rs:477:13:477:34 | StructVariant {...} | test.rs:477:32:477:32 | _ | match |
1177+
| test.rs:477:13:477:34 | StructVariant {...} | test.rs:478:13:478:27 | TupleVariant(...) | no-match |
11751178
| test.rs:477:32:477:32 | _ | test.rs:477:39:477:39 | 0 | match |
11761179
| test.rs:477:39:477:39 | 0 | test.rs:476:9:480:9 | match e { ... } | |
1180+
| test.rs:478:13:478:27 | TupleVariant(...) | test.rs:478:26:478:26 | _ | match |
1181+
| test.rs:478:13:478:27 | TupleVariant(...) | test.rs:479:13:479:23 | UnitVariant | no-match |
1182+
| test.rs:478:26:478:26 | _ | test.rs:478:32:478:32 | 1 | match |
1183+
| test.rs:478:32:478:32 | 1 | test.rs:476:9:480:9 | match e { ... } | |
1184+
| test.rs:479:13:479:23 | UnitVariant | test.rs:479:13:479:23 | UnitVariant | |
1185+
| test.rs:479:13:479:23 | UnitVariant | test.rs:479:28:479:28 | 2 | match |
1186+
| test.rs:479:28:479:28 | 2 | test.rs:476:9:480:9 | match e { ... } | |
11771187
| test.rs:485:5:490:5 | enter fn test_infinite_loop | test.rs:486:9:488:9 | ExprStmt | |
11781188
| test.rs:486:9:488:9 | ExprStmt | test.rs:487:13:487:14 | TupleExpr | |
11791189
| test.rs:486:14:488:9 | { ... } | test.rs:487:13:487:14 | TupleExpr | |

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ edges
401401
| main.rs:171:5:180:5 | match msg { ... } | main.rs:168:21:181:1 | { ... } | |
402402
| main.rs:171:11:171:13 | msg | main.rs:172:9:174:9 | ...::Hello {...} | |
403403
| main.rs:172:9:174:9 | ...::Hello {...} | main.rs:173:31:173:35 | RangePat | match |
404+
| main.rs:172:9:174:9 | ...::Hello {...} | main.rs:175:9:175:38 | ...::Hello {...} | no-match |
404405
| main.rs:173:17:173:27 | id_variable | main.rs:173:17:173:35 | id_variable @ ... | |
405406
| main.rs:173:17:173:35 | id_variable @ ... | main.rs:174:14:174:22 | print_i64 | match |
406407
| main.rs:173:31:173:31 | 3 | main.rs:173:31:173:31 | 3 | |
@@ -414,6 +415,7 @@ edges
414415
| main.rs:174:14:174:35 | print_i64(...) | main.rs:171:5:180:5 | match msg { ... } | |
415416
| main.rs:174:24:174:34 | id_variable | main.rs:174:14:174:35 | print_i64(...) | |
416417
| main.rs:175:9:175:38 | ...::Hello {...} | main.rs:175:30:175:36 | RangePat | match |
418+
| main.rs:175:9:175:38 | ...::Hello {...} | main.rs:178:9:178:29 | ...::Hello {...} | no-match |
417419
| main.rs:175:30:175:31 | 10 | main.rs:175:30:175:31 | 10 | |
418420
| main.rs:175:30:175:31 | 10 | main.rs:175:35:175:36 | 12 | match |
419421
| main.rs:175:30:175:31 | 10 | main.rs:178:9:178:29 | ...::Hello {...} | no-match |

0 commit comments

Comments
 (0)