Skip to content

Commit 0304aa8

Browse files
committed
Rust: Add more tests for uused variables.
1 parent 8213987 commit 0304aa8

File tree

2 files changed

+114
-2
lines changed

2 files changed

+114
-2
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,13 @@
22
| main.rs:90:13:90:13 | d | Variable is not used. |
33
| main.rs:114:9:114:9 | k | Variable is not used. |
44
| main.rs:141:5:141:5 | y | Variable is not used. |
5+
| main.rs:164:9:164:9 | x | Variable is not used. |
6+
| main.rs:170:9:170:9 | x | Variable is not used. |
7+
| 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. |

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

Lines changed: 104 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn locals_1() {
88
let c = 1;
99
let d = String::from("a"); // BAD: unused value [NOT DETECTED]
1010
let e = String::from("b");
11-
11+
let f = 1;
1212
let _ = 1; // (deliberately unused)
1313

1414
println!("use {}", b);
@@ -18,7 +18,7 @@ fn locals_1() {
1818
}
1919

2020
println!("use {}", e);
21-
21+
assert!(f == 1);
2222
}
2323

2424
fn locals_2() {
@@ -144,11 +144,113 @@ fn parameters(
144144
return x;
145145
}
146146

147+
// --- loops ---
148+
149+
fn loops() {
150+
let mut a: i64 = 10;
151+
let b: i64 = 20;
152+
let c: i64 = 30;
153+
let d: i64 = 40;
154+
let mut e: i64 = 50;
155+
156+
while a < b {
157+
a += 1;
158+
}
159+
160+
for x in c..d {
161+
e += x;
162+
}
163+
164+
for x in 1..10 { // BAD: unused variable
165+
}
166+
167+
for _ in 1..10 {
168+
}
169+
170+
for x in 1..10 { // SPURIOUS: unused variable [macros not yet supported]
171+
println!("x is {}", x);
172+
}
173+
174+
for x in 1..10 { // SPURIOUS: unused variable [macros not yet supported]
175+
assert!(x != 11);
176+
}
177+
}
178+
179+
// --- lets ---
180+
181+
enum MyOption<T> {
182+
None,
183+
Some(T),
184+
}
185+
186+
enum YesOrNo {
187+
Yes,
188+
No,
189+
}
190+
191+
fn if_lets() {
192+
let mut total: i64 = 0;
193+
194+
if let Some(a) = Some(10) { // BAD: unused variable
195+
}
196+
197+
if let Some(b) = Some(20) {
198+
total += b;
199+
}
200+
201+
let mut next = Some(30);
202+
while let Some(val) = next { // BAD: unused variable
203+
next = None;
204+
}
205+
206+
let mut next2 = Some(40);
207+
while let Some(val) = next2 { // SPURIOUS: unused variable 'val'
208+
total += val;
209+
next2 = None;
210+
}
211+
212+
let c = Some(60);
213+
match c {
214+
Some(val) => { // BAD: unused variable
215+
},
216+
None => { // SPURIOUS: unused variable 'None'
217+
}
218+
}
219+
220+
let d = Some(70);
221+
match d {
222+
Some(val) => {
223+
total += val;
224+
},
225+
None => { // SPURIOUS: unused variable 'None'
226+
}
227+
}
228+
229+
let e = MyOption::Some(80);
230+
match e {
231+
MyOption::Some(val) => { // BAD: unused variable
232+
},
233+
MyOption::None => {
234+
}
235+
}
236+
237+
let f = YesOrNo::Yes;
238+
match f {
239+
YesOrNo::Yes => {
240+
},
241+
YesOrNo::No => {
242+
},
243+
}
244+
}
245+
147246
fn main() {
148247
locals_1();
149248
locals_2();
150249
structs();
151250
arrays();
152251
statics();
252+
loops();
253+
if_lets();
254+
153255
println!("lets use result {}", parameters(1, 2, 3));
154256
}

0 commit comments

Comments
 (0)