Skip to content

Commit 17d6cb6

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

File tree

5 files changed

+34
-170
lines changed

5 files changed

+34
-170
lines changed

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. As a heuristic, if the identifier starts
130+
// with a lower case letter, then we assume that it's an identifier. This
131+
// works for code that follows the Rust naming convention for enums and
132+
// 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-unstable/Cfg.expected

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ edges
1919
| test.rs:6:17:6:31 | let ... = b | test.rs:6:31:6:31 | b | |
2020
| test.rs:6:21:6:27 | Some(...) | test.rs:6:12:6:31 | [boolean(false)] ... && ... | no-match |
2121
| test.rs:6:21:6:27 | Some(...) | test.rs:6:26:6:26 | d | match |
22-
| test.rs:6:26:6:26 | d | test.rs:6:12:6:31 | [boolean(false)] ... && ... | no-match |
2322
| test.rs:6:26:6:26 | d | test.rs:6:12:6:31 | [boolean(true)] ... && ... | match |
2423
| test.rs:6:26:6:26 | d | test.rs:6:26:6:26 | d | |
2524
| test.rs:6:31:6:31 | b | test.rs:6:21:6:27 | Some(...) | |
@@ -47,7 +46,6 @@ edges
4746
| test.rs:14:12:15:16 | [boolean(false)] ... && ... | test.rs:19:13:19:17 | false | false |
4847
| test.rs:14:12:15:16 | [boolean(true)] ... && ... | test.rs:17:13:17:13 | d | true |
4948
| test.rs:14:17:14:25 | let ... = b | test.rs:14:25:14:25 | b | |
50-
| test.rs:14:21:14:21 | d | test.rs:14:12:14:25 | [boolean(false)] ... && ... | no-match |
5149
| test.rs:14:21:14:21 | d | test.rs:14:12:14:25 | [boolean(true)] ... && ... | match |
5250
| test.rs:14:21:14:21 | d | test.rs:14:21:14:21 | d | |
5351
| test.rs:14:25:14:25 | b | test.rs:14:21:14:21 | d | |

0 commit comments

Comments
 (0)