Skip to content

Commit cd04500

Browse files
committed
Rust: Account for variables bound in while let expressions
1 parent 0304aa8 commit cd04500

File tree

6 files changed

+676
-636
lines changed

6 files changed

+676
-636
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,13 @@ module Impl {
218218
scope = ce.getBody() and
219219
scope.getLocation().hasLocationInfo(_, line, column, _, _)
220220
)
221+
or
222+
exists(WhileExpr we, LetExpr let |
223+
let.getPat() = pat and
224+
we.getCondition() = let and
225+
scope = we.getLoopBody() and
226+
scope.getLocation().hasLocationInfo(_, line, column, _, _)
227+
)
221228
)
222229
}
223230

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

Lines changed: 452 additions & 433 deletions
Large diffs are not rendered by default.

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

Lines changed: 188 additions & 181 deletions
Large diffs are not rendered by default.

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ fn let_pattern4() {
8585
print_str(x5); // $ access=x5
8686
}
8787

88+
fn let_pattern5() {
89+
let s1 = Some(String::from("Hello!")); // s1
90+
91+
while let Some(ref s2) // s2
92+
= s1 { // $ access=s1
93+
print_str(s2); // $ access=s2
94+
}
95+
}
96+
8897
fn match_pattern1() {
8998
let x6 = Some(5); // x6
9099
let y1 = 10; // y1_1

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
| main.rs:114:9:114:9 | k | Variable is not used. |
44
| main.rs:141:5:141:5 | y | Variable is not used. |
55
| main.rs:164:9:164:9 | x | Variable is not used. |
6-
| main.rs:170:9:170:9 | x | Variable is not used. |
6+
| main.rs:169:9:169:9 | x | Variable is not used. |
77
| main.rs:174:9:174:9 | x | Variable is not used. |
8-
| main.rs:194:17:194:17 | a | Variable is not used. |
9-
| main.rs:202:20:202:22 | val | Variable is not used. |
10-
| main.rs:207:20:207:22 | val | Variable is not used. |
11-
| main.rs:214:14:214:16 | val | Variable is not used. |
12-
| main.rs:216:9:216:12 | None | Variable is not used. |
13-
| main.rs:225:9:225:12 | None | Variable is not used. |
14-
| main.rs:231:24:231:26 | val | Variable is not used. |
8+
| main.rs:195:17:195:17 | a | Variable is not used. |
9+
| main.rs:203:20:203:22 | val | Variable is not used. |
10+
| main.rs:216:14:216:16 | val | Variable is not used. |
11+
| main.rs:218:9:218:12 | None | Variable is not used. |
12+
| main.rs:227:9:227:12 | None | Variable is not used. |
13+
| main.rs:233:24:233:26 | val | Variable is not used. |

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,15 @@ fn loops() {
164164
for x in 1..10 { // BAD: unused variable
165165
}
166166

167-
for _ in 1..10 {
168-
}
167+
for _ in 1..10 {}
169168

170-
for x in 1..10 { // SPURIOUS: unused variable [macros not yet supported]
169+
for x // SPURIOUS: unused variable [macros not yet supported]
170+
in 1..10 {
171171
println!("x is {}", x);
172172
}
173173

174-
for x in 1..10 { // SPURIOUS: unused variable [macros not yet supported]
174+
for x // SPURIOUS: unused variable [macros not yet supported]
175+
in 1..10 {
175176
assert!(x != 11);
176177
}
177178
}
@@ -199,20 +200,21 @@ fn if_lets() {
199200
}
200201

201202
let mut next = Some(30);
202-
while let Some(val) = next { // BAD: unused variable
203+
while let Some(val) = next // BAD: unused variable
204+
{
203205
next = None;
204206
}
205207

206208
let mut next2 = Some(40);
207-
while let Some(val) = next2 { // SPURIOUS: unused variable 'val'
209+
while let Some(val) = next2 {
208210
total += val;
209211
next2 = None;
210212
}
211213

212214
let c = Some(60);
213215
match c {
214216
Some(val) => { // BAD: unused variable
215-
},
217+
}
216218
None => { // SPURIOUS: unused variable 'None'
217219
}
218220
}
@@ -221,25 +223,22 @@ fn if_lets() {
221223
match d {
222224
Some(val) => {
223225
total += val;
224-
},
226+
}
225227
None => { // SPURIOUS: unused variable 'None'
226228
}
227229
}
228230

229231
let e = MyOption::Some(80);
230232
match e {
231233
MyOption::Some(val) => { // BAD: unused variable
232-
},
233-
MyOption::None => {
234234
}
235+
MyOption::None => {}
235236
}
236237

237238
let f = YesOrNo::Yes;
238239
match f {
239-
YesOrNo::Yes => {
240-
},
241-
YesOrNo::No => {
242-
},
240+
YesOrNo::Yes => {}
241+
YesOrNo::No => {}
243242
}
244243
}
245244

0 commit comments

Comments
 (0)