Skip to content

Commit a66f31d

Browse files
committed
Rust: Restrict variables to lowercase (for now).
1 parent 79871aa commit a66f31d

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,13 @@ module Impl {
8585
private AstNode definingNode;
8686
private string name;
8787

88-
Variable() { this = MkVariable(definingNode, name) }
88+
Variable() {
89+
this = MkVariable(definingNode, name) and
90+
// exclude for now anything starting with an uppercase character, which may be an enum constant (e.g. `None`). This excludes
91+
// static and constant variables (UPPERCASE), which we don't appear to recognize yet anyway. This also assumes programmers
92+
// follow the naming guidelines, which they generally do, but they're not enforced.
93+
not name.charAt(0).isUppercase()
94+
}
8995

9096
/** Gets the name of this variable. */
9197
string getName() { result = name }

rust/ql/test/query-tests/unusedentities/UnusedVariable.expected

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,10 @@
88
| main.rs:202:17:202:17 | a | Variable is not used. |
99
| main.rs:210:20:210:22 | val | Variable is not used. |
1010
| main.rs:223:14:223:16 | val | Variable is not used. |
11-
| main.rs:225:9:225:12 | None | Variable is not used. |
12-
| main.rs:234:9:234:12 | None | Variable is not used. |
1311
| main.rs:240:22:240:24 | val | Variable is not used. |
1412
| main.rs:248:24:248:26 | val | Variable is not used. |
1513
| main.rs:257:13:257:15 | num | Variable is not used. |
16-
| main.rs:268:9:268:11 | Yes | Variable is not used. |
17-
| main.rs:269:9:269:10 | No | Variable is not used. |
1814
| main.rs:272:12:272:12 | j | Variable is not used. |
19-
| main.rs:282:12:282:14 | Yes | Variable is not used. |
2015
| main.rs:294:25:294:25 | y | Variable is not used. |
2116
| main.rs:298:28:298:28 | a | Variable is not used. |
2217
| main.rs:302:9:302:9 | p | Variable is not used. |

rust/ql/test/query-tests/unusedentities/main.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ fn if_lets_matches() {
222222
match c {
223223
Some(val) => { // BAD: unused variable
224224
}
225-
None => { // SPURIOUS: unused variable 'None'
225+
None => {
226226
}
227227
}
228228

@@ -231,7 +231,7 @@ fn if_lets_matches() {
231231
Some(val) => {
232232
total += val;
233233
}
234-
None => { // SPURIOUS: unused variable 'None'
234+
None => {
235235
}
236236
}
237237

@@ -265,8 +265,8 @@ fn if_lets_matches() {
265265

266266
let i = Yes;
267267
match i {
268-
Yes => {} // SPURIOUS: unused variable 'Yes'
269-
No => {} // SPURIOUS: unused variable 'No'
268+
Yes => {}
269+
No => {}
270270
}
271271

272272
if let j = Yes { // BAD: unused variable
@@ -279,7 +279,7 @@ fn if_lets_matches() {
279279
}
280280

281281
let l = Yes;
282-
if let Yes = l { // SPURIOUS: unused variable 'Yes'
282+
if let Yes = l {
283283
}
284284

285285
match 1 {

0 commit comments

Comments
 (0)