Skip to content

Commit 6df9649

Browse files
committed
Rust: Assume in the CFG that lowercase identifiers are in fact identifiers
1 parent 72346cc commit 6df9649

2 files changed

Lines changed: 5 additions & 14 deletions

File tree

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,11 @@ private predicate cannotCauseMatchFailure(Pat pat) {
126126
pat instanceof RangePat or
127127
// Identifier patterns that are in fact path patterns can cause failures. For
128128
// 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
129+
// it's an actual identifier pattern. Additionaly, if the identifier starts
130+
// with a lower case letter then we guess and assume that it's an identifier.
131+
// This heuristic works for code that follows the naming convention for enums
132+
// and constants.
133+
pat = any(IdentPat p | p.hasPat() or p.getName().getText().charAt(0).isLowercase()) or
131134
pat instanceof WildcardPat or
132135
pat instanceof RestPat or
133136
pat instanceof RefPat or

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ edges
203203
| test.rs:99:15:99:39 | let ... = ... | test.rs:99:29:99:32 | iter | |
204204
| test.rs:99:19:99:25 | Some(...) | test.rs:99:9:103:9 | while ... { ... } | no-match |
205205
| test.rs:99:19:99:25 | Some(...) | test.rs:99:24:99:24 | x | match |
206-
| test.rs:99:24:99:24 | x | test.rs:99:9:103:9 | while ... { ... } | no-match |
207206
| test.rs:99:24:99:24 | x | test.rs:99:24:99:24 | x | |
208207
| test.rs:99:24:99:24 | x | test.rs:100:17:100:17 | x | match |
209208
| test.rs:99:29:99:32 | iter | test.rs:99:29:99:39 | iter.next(...) | |
@@ -317,7 +316,6 @@ edges
317316
| test.rs:146:16:146:22 | Some(...) | test.rs:149:13:149:13 | 0 | no-match |
318317
| test.rs:146:21:146:21 | n | test.rs:146:21:146:21 | n | |
319318
| test.rs:146:21:146:21 | n | test.rs:147:13:147:13 | n | match |
320-
| test.rs:146:21:146:21 | n | test.rs:149:13:149:13 | 0 | no-match |
321319
| test.rs:146:26:146:26 | a | test.rs:146:16:146:22 | Some(...) | |
322320
| test.rs:146:28:148:9 | { ... } | test.rs:146:9:150:9 | if ... {...} else {...} | |
323321
| test.rs:147:13:147:13 | n | test.rs:146:28:148:9 | { ... } | |
@@ -334,7 +332,6 @@ edges
334332
| test.rs:154:12:154:26 | let ... = a | test.rs:154:26:154:26 | a | |
335333
| test.rs:154:16:154:22 | Some(...) | test.rs:154:9:156:9 | if ... {...} | no-match |
336334
| test.rs:154:16:154:22 | Some(...) | test.rs:154:21:154:21 | n | match |
337-
| test.rs:154:21:154:21 | n | test.rs:154:9:156:9 | if ... {...} | no-match |
338335
| test.rs:154:21:154:21 | n | test.rs:154:21:154:21 | n | |
339336
| test.rs:154:21:154:21 | n | test.rs:155:13:155:21 | ExprStmt | match |
340337
| test.rs:154:26:154:26 | a | test.rs:154:16:154:22 | Some(...) | |
@@ -802,7 +799,6 @@ edges
802799
| test.rs:328:13:328:27 | ...::Some(...) | test.rs:329:13:329:27 | ...::Some(...) | no-match |
803800
| test.rs:328:26:328:26 | x | test.rs:328:26:328:26 | x | |
804801
| test.rs:328:26:328:26 | x | test.rs:328:32:328:32 | x | match |
805-
| test.rs:328:26:328:26 | x | test.rs:329:13:329:27 | ...::Some(...) | no-match |
806802
| test.rs:328:32:328:32 | x | test.rs:328:36:328:37 | 10 | |
807803
| test.rs:328:32:328:37 | ... < ... | test.rs:328:42:328:42 | x | true |
808804
| test.rs:328:32:328:37 | ... < ... | test.rs:329:13:329:27 | ...::Some(...) | false |
@@ -814,7 +810,6 @@ edges
814810
| test.rs:329:13:329:27 | ...::Some(...) | test.rs:330:13:330:24 | ...::None | no-match |
815811
| test.rs:329:26:329:26 | x | test.rs:329:26:329:26 | x | |
816812
| test.rs:329:26:329:26 | x | test.rs:329:32:329:32 | x | match |
817-
| test.rs:329:26:329:26 | x | test.rs:330:13:330:24 | ...::None | no-match |
818813
| test.rs:329:32:329:32 | x | test.rs:327:9:331:9 | match maybe_digit { ... } | |
819814
| test.rs:330:13:330:24 | ...::None | test.rs:330:29:330:29 | 5 | match |
820815
| test.rs:330:29:330:29 | 5 | test.rs:327:9:331:9 | match maybe_digit { ... } | |
@@ -841,7 +836,6 @@ edges
841836
| test.rs:340:13:340:27 | ...::Some(...) | test.rs:341:13:341:24 | ...::None | no-match |
842837
| test.rs:340:26:340:26 | x | test.rs:340:26:340:26 | x | |
843838
| test.rs:340:26:340:26 | x | test.rs:340:32:340:32 | x | match |
844-
| test.rs:340:26:340:26 | x | test.rs:341:13:341:24 | ...::None | no-match |
845839
| test.rs:340:32:340:32 | x | test.rs:340:36:340:36 | 5 | |
846840
| test.rs:340:32:340:36 | ... + ... | test.rs:335:9:342:9 | match ... { ... } | |
847841
| test.rs:340:36:340:36 | 5 | test.rs:340:32:340:36 | ... + ... | |
@@ -864,7 +858,6 @@ edges
864858
| test.rs:347:13:347:19 | Some(...) | test.rs:348:13:348:13 | _ | no-match |
865859
| test.rs:347:18:347:18 | a | test.rs:347:18:347:18 | a | |
866860
| test.rs:347:18:347:18 | a | test.rs:347:24:347:24 | a | match |
867-
| test.rs:347:18:347:18 | a | test.rs:348:13:348:13 | _ | no-match |
868861
| test.rs:347:24:347:24 | a | test.rs:346:10:349:9 | [boolean(false)] match r { ... } | false |
869862
| test.rs:347:24:347:24 | a | test.rs:346:10:349:9 | [boolean(true)] match r { ... } | true |
870863
| test.rs:348:13:348:13 | _ | test.rs:348:18:348:22 | false | match |
@@ -882,7 +875,6 @@ edges
882875
| test.rs:354:13:354:21 | Ok(...) | test.rs:355:13:355:22 | Err(...) | no-match |
883876
| test.rs:354:16:354:20 | value | test.rs:354:16:354:20 | value | |
884877
| test.rs:354:16:354:20 | value | test.rs:354:26:354:30 | value | match |
885-
| test.rs:354:16:354:20 | value | test.rs:355:13:355:22 | Err(...) | no-match |
886878
| test.rs:354:26:354:30 | value | test.rs:353:9:356:9 | match r { ... } | |
887879
| test.rs:355:13:355:22 | Err(...) | test.rs:355:17:355:21 | never | match |
888880
| test.rs:355:17:355:21 | never | test.rs:355:17:355:21 | never | |
@@ -899,7 +891,6 @@ edges
899891
| test.rs:363:13:363:19 | Some(...) | test.rs:363:18:363:18 | n | match |
900892
| test.rs:363:13:363:19 | Some(...) | test.rs:363:39:363:53 | MacroStmts | no-match |
901893
| test.rs:363:18:363:18 | n | test.rs:363:18:363:18 | n | |
902-
| test.rs:363:18:363:18 | n | test.rs:363:39:363:53 | MacroStmts | no-match |
903894
| test.rs:363:18:363:18 | n | test.rs:364:9:364:9 | n | match |
904895
| test.rs:363:23:363:23 | a | test.rs:363:13:363:19 | Some(...) | |
905896
| test.rs:363:32:363:54 | ...::panic_fmt | test.rs:363:39:363:53 | "Expected some" | |
@@ -932,7 +923,6 @@ edges
932923
| test.rs:369:13:369:21 | Some(...) | test.rs:370:13:370:16 | None | no-match |
933924
| test.rs:369:18:369:20 | ret | test.rs:369:18:369:20 | ret | |
934925
| test.rs:369:18:369:20 | ret | test.rs:369:26:369:28 | ret | match |
935-
| test.rs:369:18:369:20 | ret | test.rs:370:13:370:16 | None | no-match |
936926
| test.rs:369:26:369:28 | ret | test.rs:368:19:371:9 | match m { ... } | |
937927
| test.rs:370:13:370:16 | None | test.rs:370:13:370:16 | None | |
938928
| test.rs:370:13:370:16 | None | test.rs:370:28:370:32 | false | match |
@@ -1158,7 +1148,6 @@ edges
11581148
| test.rs:462:14:462:35 | TuplePat | test.rs:462:15:462:15 | n | match |
11591149
| test.rs:462:15:462:15 | n | test.rs:462:15:462:15 | n | |
11601150
| test.rs:462:15:462:15 | n | test.rs:462:18:462:34 | MyStruct {...} | match |
1161-
| test.rs:462:15:462:15 | n | test.rs:463:13:463:13 | _ | no-match |
11621151
| test.rs:462:18:462:34 | MyStruct {...} | test.rs:462:32:462:32 | _ | match |
11631152
| test.rs:462:18:462:34 | MyStruct {...} | test.rs:463:13:463:13 | _ | no-match |
11641153
| test.rs:462:32:462:32 | _ | test.rs:462:40:462:40 | n | match |
@@ -1409,7 +1398,6 @@ edges
14091398
| test.rs:587:13:587:19 | Some(...) | test.rs:587:18:587:18 | y | match |
14101399
| test.rs:587:13:587:19 | Some(...) | test.rs:588:13:588:27 | ExprStmt | no-match |
14111400
| test.rs:587:18:587:18 | y | test.rs:587:18:587:18 | y | |
1412-
| test.rs:587:18:587:18 | y | test.rs:588:13:588:27 | ExprStmt | no-match |
14131401
| test.rs:587:18:587:18 | y | test.rs:590:9:590:9 | 0 | match |
14141402
| test.rs:587:23:587:23 | x | test.rs:587:13:587:19 | Some(...) | |
14151403
| test.rs:588:13:588:26 | break ''block 1 | test.rs:585:18:591:5 | 'block: { ... } | break |

0 commit comments

Comments
 (0)