Skip to content

Commit 35ffd0c

Browse files
committed
Rust: Add more test cases for unused variables (inspired by real world FPs).
1 parent 3091083 commit 35ffd0c

File tree

3 files changed

+61
-20
lines changed

3 files changed

+61
-20
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
| main.rs:91:9:91:9 | a | Variable $@ is assigned a value that is never used. | main.rs:91:9:91:9 | a | a |
1212
| main.rs:112:9:112:10 | is | Variable $@ is assigned a value that is never used. | main.rs:112:9:112:10 | is | is |
1313
| main.rs:135:13:135:17 | total | Variable $@ is assigned a value that is never used. | main.rs:135:13:135:17 | total | total |
14-
| main.rs:272:13:272:17 | total | Variable $@ is assigned a value that is never used. | main.rs:240:13:240:17 | total | total |
15-
| main.rs:365:9:365:9 | x | Variable $@ is assigned a value that is never used. | main.rs:365:9:365:9 | x | x |
16-
| main.rs:373:17:373:17 | x | Variable $@ is assigned a value that is never used. | main.rs:373:17:373:17 | x | x |
14+
| main.rs:280:13:280:17 | total | Variable $@ is assigned a value that is never used. | main.rs:248:13:248:17 | total | total |
15+
| main.rs:373:9:373:9 | x | Variable $@ is assigned a value that is never used. | main.rs:373:9:373:9 | x | x |
16+
| main.rs:381:17:381:17 | x | Variable $@ is assigned a value that is never used. | main.rs:381:17:381:17 | x | x |
17+
| main.rs:482:9:482:9 | c | Variable $@ is assigned a value that is never used. | main.rs:482:9:482:9 | c | c |
1718
| more.rs:44:9:44:14 | a_ptr4 | Variable $@ is assigned a value that is never used. | more.rs:44:9:44:14 | a_ptr4 | a_ptr4 |
1819
| more.rs:59:9:59:13 | d_ptr | Variable $@ is assigned a value that is never used. | more.rs:59:9:59:13 | d_ptr | d_ptr |
1920
| more.rs:65:9:65:17 | f_ptr | Variable $@ is assigned a value that is never used. | more.rs:65:13:65:17 | f_ptr | f_ptr |

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

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,25 @@
22
| main.rs:94:13:94:13 | d | Variable 'd' is not used. |
33
| main.rs:143:5:143:5 | y | Variable 'y' is not used. |
44
| main.rs:170:9:170:9 | x | Variable 'x' is not used. |
5-
| main.rs:242:17:242:17 | a | Variable 'a' is not used. |
6-
| main.rs:250:20:250:22 | val | Variable 'val' is not used. |
7-
| main.rs:264:14:264:16 | val | Variable 'val' is not used. |
8-
| main.rs:279:22:279:24 | val | Variable 'val' is not used. |
9-
| main.rs:286:24:286:26 | val | Variable 'val' is not used. |
10-
| main.rs:294:13:294:15 | num | Variable 'num' is not used. |
11-
| main.rs:309:12:309:12 | j | Variable 'j' is not used. |
12-
| main.rs:329:25:329:25 | y | Variable 'y' is not used. |
13-
| main.rs:332:28:332:28 | a | Variable 'a' is not used. |
14-
| main.rs:335:9:335:9 | p | Variable 'p' is not used. |
15-
| main.rs:353:9:353:13 | right | Variable 'right' is not used. |
16-
| main.rs:359:9:359:14 | right2 | Variable 'right2' is not used. |
17-
| main.rs:366:13:366:13 | y | Variable 'y' is not used. |
18-
| main.rs:374:21:374:21 | y | Variable 'y' is not used. |
19-
| main.rs:419:26:419:28 | val | Variable 'val' is not used. |
20-
| main.rs:422:21:422:23 | acc | Variable 'acc' is not used. |
21-
| main.rs:443:9:443:14 | unused | Variable 'unused' is not used. |
5+
| main.rs:250:17:250:17 | a | Variable 'a' is not used. |
6+
| main.rs:258:20:258:22 | val | Variable 'val' is not used. |
7+
| main.rs:272:14:272:16 | val | Variable 'val' is not used. |
8+
| main.rs:287:22:287:24 | val | Variable 'val' is not used. |
9+
| main.rs:294:24:294:26 | val | Variable 'val' is not used. |
10+
| main.rs:302:13:302:15 | num | Variable 'num' is not used. |
11+
| main.rs:317:12:317:12 | j | Variable 'j' is not used. |
12+
| main.rs:337:25:337:25 | y | Variable 'y' is not used. |
13+
| main.rs:340:28:340:28 | a | Variable 'a' is not used. |
14+
| main.rs:343:9:343:9 | p | Variable 'p' is not used. |
15+
| main.rs:361:9:361:13 | right | Variable 'right' is not used. |
16+
| main.rs:367:9:367:14 | right2 | Variable 'right2' is not used. |
17+
| main.rs:374:13:374:13 | y | Variable 'y' is not used. |
18+
| main.rs:382:21:382:21 | y | Variable 'y' is not used. |
19+
| main.rs:427:26:427:28 | val | Variable 'val' is not used. |
20+
| main.rs:430:21:430:23 | acc | Variable 'acc' is not used. |
21+
| main.rs:451:9:451:14 | unused | Variable 'unused' is not used. |
22+
| main.rs:494:16:494:16 | x | Variable 'x' is not used. |
23+
| main.rs:495:16:495:16 | y | Variable 'y' is not used. |
24+
| main.rs:496:12:496:12 | z | Variable 'z' is not used. |
25+
| main.rs:499:18:499:18 | x | Variable 'x' is not used. |
2226
| more.rs:24:9:24:11 | val | Variable 'val' is not used. |

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,14 @@ fn loops() {
198198
_ = format!("x is {x}");
199199
}
200200

201+
for x in 1..10 {
202+
_ = format!("x is {x:?}");
203+
}
204+
205+
[1, 2, 3].iter().for_each(|x| {
206+
_ = format!("x is {x}");
207+
});
208+
201209
for x in 1..10 {
202210
println!("x is {val}", val = x);
203211
}
@@ -466,6 +474,33 @@ fn macros() {
466474
})
467475
)
468476
}
477+
// --- references ---
478+
479+
fn references() {
480+
let a = 1;
481+
let b = &a;
482+
let c = *b; // $ Alert[rust/unused-value]
483+
let d = 2;
484+
let e = 3;
485+
let f = &&e;
486+
487+
assert!(&d != *f);
488+
}
489+
490+
// --- declarations in types ---
491+
492+
pub struct my_declaration {
493+
field1: fn(i32) -> i32,
494+
field2: fn(x: i32) -> i32, // $ SPURIOUS: Alert[rust/unused-variable]
495+
field3: fn(y: // $ SPURIOUS: Alert[rust/unused-variable]
496+
fn(z: i32) -> i32) -> i32, // $ SPURIOUS: Alert[rust/unused-variable]
497+
}
498+
499+
type MyType = fn(x: i32) -> i32; // $ SPURIOUS: Alert[rust/unused-variable]
500+
501+
trait MyTrait {
502+
fn my_func2(&self, x: i32) -> i32;
503+
}
469504

470505
// --- main ---
471506

@@ -482,6 +517,7 @@ fn main() {
482517
func_ptrs();
483518
folds_and_closures();
484519
macros();
520+
references();
485521

486522
generics();
487523
pointers();

0 commit comments

Comments
 (0)