Skip to content

Commit e5885f6

Browse files
committed
Rust: Add more test cases for unused variables and unreachable code.
1 parent 528641c commit e5885f6

File tree

3 files changed

+104
-98
lines changed

3 files changed

+104
-98
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
| main.rs:90:13:90:13 | d | Variable is not used. |
33
| main.rs:141:5:141:5 | y | Variable is not used. |
44
| main.rs:168:9:168:9 | x | Variable is not used. |
5+
| main.rs:196:9:196:9 | x | Variable is not used. |
6+
| main.rs:201:9:201:9 | x | Variable is not used. |
57
| main.rs:250:17:250:17 | a | Variable is not used. |
68
| main.rs:258:20:258:22 | val | Variable is not used. |
79
| main.rs:271:14:271:16 | val | Variable is not used. |
@@ -12,5 +14,9 @@
1214
| main.rs:342:25:342:25 | y | Variable is not used. |
1315
| main.rs:346:28:346:28 | a | Variable is not used. |
1416
| main.rs:350:9:350:9 | p | Variable is not used. |
17+
| main.rs:365:9:365:13 | right | Variable is not used. |
18+
| main.rs:371:9:371:14 | right2 | Variable is not used. |
1519
| main.rs:378:13:378:13 | y | Variable is not used. |
1620
| main.rs:386:21:386:21 | y | Variable is not used. |
21+
| main.rs:434:27:434:29 | val | Variable is not used. |
22+
| main.rs:437:22:437:24 | acc | Variable is not used. |

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

Lines changed: 93 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ fn parameters(
146146

147147
// --- loops ---
148148

149-
150-
151-
149+
fn id(v: i32) -> i32 {
150+
return v;
151+
}
152152

153153
fn loops() {
154154
let mut a: i64 = 10;
@@ -175,53 +175,53 @@ fn loops() {
175175
println!("x is {}", x);
176176
}
177177

178+
for x
179+
in 1..10 {
180+
println!("x is {:?}", x);
181+
}
178182

183+
for x
184+
in 1..10 {
185+
println!("x + 1 is {}", x + 1);
186+
}
179187

188+
for x
189+
in 1..10 {
190+
for y
191+
in 1..x {
192+
println!("y is {}", y);
193+
}
194+
}
180195

196+
for x // SPURIOUS: unused variable
197+
in 1..10 {
198+
println!("x is {x}");
199+
}
181200

201+
for x // SPURIOUS: unused variable
202+
in 1..10 {
203+
_ = format!("x is {x}");
204+
}
182205

183-
184-
185-
186-
187-
188-
189-
190-
191-
192-
193-
194-
195-
196-
197-
198-
199-
200-
201-
202-
203-
204-
205-
206-
207-
208-
209-
210-
206+
for x
207+
in 1..10 {
208+
println!("x is {val}", val = x);
209+
}
211210

212211
for x
213212
in 1..10 {
214213
assert!(x != 11);
215214
}
216215

216+
for x
217+
in 1..10 {
218+
assert_eq!(x, 1);
219+
}
217220

218-
219-
220-
221-
222-
223-
224-
221+
for x
222+
in 1..10 {
223+
assert_eq!(id(x), id(1));
224+
}
225225

226226
}
227227

@@ -351,26 +351,26 @@ fn if_lets_matches() {
351351
}
352352
}
353353

354+
let duration1 = std::time::Duration::new(10, 0); // ten seconds
355+
assert_eq!(duration1.as_secs(), 10);
354356

357+
let duration2:Result<std::time::Duration, String> =
358+
Ok(std::time::Duration::new(10, 0));
359+
match (duration2) {
360+
Ok(n) => { println!("duration was {} seconds", n.as_secs()); }
361+
Err(_) => { println!("failed"); }
362+
}
355363

364+
let(left,
365+
right) = // BAD: unused value [NOT DETECTED] SPURIOUS: unused variable
366+
(1, 2);
367+
_ = left;
356368

357-
358-
359-
360-
361-
362-
363-
364-
365-
366-
367-
368-
369-
370-
371-
372-
373-
369+
let pair = (1, 2);
370+
let(left2,
371+
right2) = // BAD: unused value [NOT DETECTED] SPURIOUS: unused variable
372+
pair;
373+
_ = left2;
374374
}
375375

376376
fn shadowing() -> i32 {
@@ -391,55 +391,55 @@ fn shadowing() -> i32 {
391391
}
392392
}
393393

394+
// --- function pointers ---
394395

396+
type FuncPtr = fn(i32) -> i32;
395397

398+
fn increment(x: i32) -> i32 {
399+
return x + 1;
400+
}
396401

402+
fn func_ptrs() {
403+
let MyFunc: FuncPtr = increment;
397404

405+
for x
406+
in 1..10 {
407+
_ = x + 1;
408+
}
398409

410+
for x
411+
in 1..10 {
412+
_ = increment(x);
413+
}
399414

415+
for x
416+
in 1..10 {
417+
_ = MyFunc(x);
418+
}
419+
}
400420

421+
// --- folds and closures ---
401422

423+
fn folds_and_closures() {
424+
let a1 = 1..10;
425+
_ = a1.sum::<i32>();
402426

427+
let a2 = 1..10;
428+
_ = a2.fold(0, | acc: i32, val: i32 | -> i32 { acc + val } );
403429

430+
let a3 = 1..10;
431+
_ = a3.fold(0, | acc, val | acc + val);
404432

433+
let a4 = 1..10;
434+
_ = a4.fold(0, | acc, val | acc); // BAD: unused variable
405435

436+
let a5 = 1..10;
437+
_ = a5.fold(0, | acc, val | val); // BAD: unused variable
406438

407-
408-
409-
410-
411-
412-
413-
414-
415-
416-
417-
418-
419-
420-
421-
422-
423-
424-
425-
426-
427-
428-
429-
430-
431-
432-
433-
434-
435-
436-
437-
438-
439-
440-
441-
442-
439+
let i6 = 1;
440+
let a6 = 1..10;
441+
_ = a6.fold(0, | acc, val | acc + val + i6);
442+
}
443443

444444
// --- main ---
445445

@@ -453,8 +453,8 @@ fn main() {
453453
loops();
454454
if_lets_matches();
455455
shadowing();
456-
457-
456+
func_ptrs();
457+
folds_and_closures();
458458

459459
unreachable_if();
460460
unreachable_panic();

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ fn unreachable_panic() {
7979
do_something(); // BAD: unreachable code [NOT DETECTED]
8080
}
8181

82-
83-
84-
85-
86-
82+
if cond() {
83+
do_something();
84+
unreachable!();
85+
do_something(); // BAD: unreachable code [NOT DETECTED]
86+
}
8787

8888
if cond() {
8989
let mut maybe;

0 commit comments

Comments
 (0)