Skip to content

Commit ed39c46

Browse files
authored
Merge pull request #17710 from geoffw0/unusedvar3
Rust: More test cases for unused variables
2 parents 6ffdf57 + f3d727f commit ed39c46

File tree

2 files changed

+105
-13
lines changed

2 files changed

+105
-13
lines changed

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,20 @@
55
| main.rs:164:9:164:9 | x | Variable is not used. |
66
| 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: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. |
8+
| main.rs:202:17:202:17 | a | Variable is not used. |
9+
| main.rs:210:20:210:22 | val | Variable is not used. |
10+
| 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. |
13+
| main.rs:240:22:240:24 | val | Variable is not used. |
14+
| main.rs:248:24:248:26 | val | Variable is not used. |
15+
| 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. |
18+
| main.rs:272:12:272:12 | j | Variable is not used. |
19+
| main.rs:282:12:282:14 | Yes | Variable is not used. |
20+
| main.rs:294:25:294:25 | y | Variable is not used. |
21+
| main.rs:298:28:298:28 | a | Variable is not used. |
22+
| main.rs:302:9:302:9 | p | Variable is not used. |
23+
| main.rs:309:13:309:13 | y | Variable is not used. |
24+
| main.rs:317:21:317:21 | y | Variable is not used. |

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

Lines changed: 88 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,12 @@ fn loops() {
167167
for _ in 1..10 {}
168168

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

174174
for x // SPURIOUS: unused variable [macros not yet supported]
175-
in 1..10 {
175+
in 1..10 {
176176
assert!(x != 11);
177177
}
178178
}
@@ -189,7 +189,14 @@ enum YesOrNo {
189189
No,
190190
}
191191

192-
fn if_lets() {
192+
use YesOrNo::{Yes, No}; // allows `Yes`, `No` to be accessed without qualifiers.
193+
194+
struct MyPoint {
195+
x: i64,
196+
y: i64,
197+
}
198+
199+
fn if_lets_matches() {
193200
let mut total: i64 = 0;
194201

195202
if let Some(a) = Some(10) { // BAD: unused variable
@@ -228,18 +235,91 @@ fn if_lets() {
228235
}
229236
}
230237

231-
let e = MyOption::Some(80);
238+
let e = Option::Some(80);
232239
match e {
240+
Option::Some(val) => { // BAD: unused variable
241+
}
242+
Option::None => {
243+
}
244+
}
245+
246+
let f = MyOption::Some(90);
247+
match f {
233248
MyOption::Some(val) => { // BAD: unused variable
234249
}
235250
MyOption::None => {}
236251
}
237252

238-
let f = YesOrNo::Yes;
239-
match f {
253+
let g : Result<i64, i64> = Ok(100);
254+
match g {
255+
Ok(_) => {
256+
}
257+
Err(num) => {} // BAD: unused variable
258+
}
259+
260+
let h = YesOrNo::Yes;
261+
match h {
240262
YesOrNo::Yes => {}
241263
YesOrNo::No => {}
242264
}
265+
266+
let i = Yes;
267+
match i {
268+
Yes => {} // SPURIOUS: unused variable 'Yes'
269+
No => {} // SPURIOUS: unused variable 'No'
270+
}
271+
272+
if let j = Yes { // BAD: unused variable
273+
}
274+
275+
if let k = Yes {
276+
match k {
277+
_ => {}
278+
}
279+
}
280+
281+
let l = Yes;
282+
if let Yes = l { // SPURIOUS: unused variable 'Yes'
283+
}
284+
285+
match 1 {
286+
1 => {}
287+
_ => {}
288+
}
289+
290+
let p1 = MyPoint { x: 1, y: 2 };
291+
match p1 {
292+
MyPoint { x: 0, y: 0 } => {
293+
}
294+
MyPoint { x: 1, y } => { // BAD: unused variable
295+
}
296+
MyPoint { x: 2, y: _ } => {
297+
}
298+
MyPoint { x: 3, y: a } => { // BAD: unused variable
299+
}
300+
MyPoint { x: 4, .. } => {
301+
}
302+
p => { // BAD: unused variable
303+
}
304+
}
305+
}
306+
307+
fn shadowing() -> i32 {
308+
let x = 1; // BAD: unused value [NOT DETECTED]
309+
let mut y: i32; // BAD: unused variable
310+
311+
{
312+
let x = 2;
313+
let mut y: i32;
314+
315+
{
316+
let x = 3; // BAD: unused value [NOT DETECTED]
317+
let mut y: i32; // BAD: unused variable
318+
}
319+
320+
y = x;
321+
return y;
322+
}
243323
}
244324

245325
fn main() {
@@ -249,7 +329,8 @@ fn main() {
249329
arrays();
250330
statics();
251331
loops();
252-
if_lets();
332+
if_lets_matches();
333+
shadowing();
253334

254335
println!("lets use result {}", parameters(1, 2, 3));
255336
}

0 commit comments

Comments
 (0)