Skip to content

Commit 25b3d76

Browse files
authored
Merge pull request github#17718 from geoffw0/unusedvar4
Rust: Restrict variables to lowercase (for now).
2 parents 09c2f90 + 5ac9c2d commit 25b3d76

File tree

4 files changed

+11
-12
lines changed

4 files changed

+11
-12
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,12 @@ module Impl {
7878
not exists(getOutermostEnclosingOrPat(p)) and
7979
definingNode = p.getName()
8080
) and
81-
name = p.getName().getText()
81+
name = p.getName().getText() and
82+
// exclude for now anything starting with an uppercase character, which may be a reference to
83+
// an enum constant (e.g. `None`). This excludes static and constant variables (UPPERCASE),
84+
// which we don't appear to recognize yet anyway. This also assumes programmers follow the
85+
// naming guidelines, which they generally do, but they're not enforced.
86+
not name.charAt(0).isUppercase()
8287
}
8388

8489
/** A variable. */

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ variable
2424
| variables.rs:100:9:100:10 | x6 |
2525
| variables.rs:101:9:101:10 | y1 |
2626
| variables.rs:105:14:105:15 | y1 |
27-
| variables.rs:110:9:110:12 | None |
2827
| variables.rs:117:9:117:15 | numbers |
2928
| variables.rs:121:13:121:17 | first |
3029
| variables.rs:122:13:122:17 | third |

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)