Skip to content

Commit 7a6eabc

Browse files
committed
Rust: Additional test cases.
1 parent 41218fb commit 7a6eabc

File tree

2 files changed

+77
-18
lines changed

2 files changed

+77
-18
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,14 @@
1010
| main.rs:223:14:223:16 | val | Variable is not used. |
1111
| main.rs:225:9:225:12 | None | Variable is not used. |
1212
| main.rs:234:9:234:12 | None | Variable is not used. |
13+
| main.rs:240:22:240:24 | val | Variable is not used. |
1314
| 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:290:25:290:25 | y | Variable is not used. |
20+
| main.rs:294:28:294:28 | a | Variable is not used. |
21+
| main.rs:298:9:298:9 | p | Variable is not used. |
22+
| main.rs:305:13:305:13 | y | Variable is not used. |
23+
| main.rs:313:21:313:21 | y | Variable is not used. |

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

Lines changed: 67 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,12 @@ enum YesOrNo {
189189
No,
190190
}
191191

192+
use YesOrNo::{Yes, No}; // allows `Yes`, `No` to be accessed without qualifiers.
192193

193-
194-
195-
196-
197-
194+
struct MyPoint {
195+
x: i64,
196+
y: i64,
197+
}
198198

199199
fn if_lets_matches() {
200200
let mut total: i64 = 0;
@@ -235,13 +235,13 @@ fn if_lets_matches() {
235235
}
236236
}
237237

238-
239-
240-
241-
242-
243-
244-
238+
let e = Option::Some(80);
239+
match e {
240+
Option::Some(val) => { // BAD: unused variable
241+
}
242+
Option::None => {
243+
}
244+
}
245245

246246
let f = MyOption::Some(90);
247247
match f {
@@ -250,24 +250,72 @@ fn if_lets_matches() {
250250
MyOption::None => {}
251251
}
252252

253-
254-
255-
256-
257-
258-
253+
let g : Result<i64, i64> = Ok(100);
254+
match g {
255+
Ok(_) => {
256+
}
257+
Err(num) => {} // BAD: unused variable
258+
}
259259

260260
let h = YesOrNo::Yes;
261261
match h {
262262
YesOrNo::Yes => {}
263263
YesOrNo::No => {}
264264
}
265265

266+
let i = Yes;
267+
match i {
268+
Yes => {} // SPURIOUS: unused variable 'None'
269+
No => {} // SPURIOUS: unused variable 'None'
270+
}
266271

272+
if let j = Yes { // BAD: unused variable
273+
}
267274

275+
if let k = Yes {
276+
match k {
277+
_ => {}
278+
}
279+
}
268280

281+
match 1 {
282+
1 => {}
283+
_ => {}
284+
}
269285

286+
let p1 = MyPoint { x: 1, y: 2 };
287+
match p1 {
288+
MyPoint { x: 0, y: 0 } => {
289+
}
290+
MyPoint { x: 1, y } => { // BAD: unused variable
291+
}
292+
MyPoint { x: 2, y: _ } => {
293+
}
294+
MyPoint { x: 3, y: a } => { // BAD: unused variable
295+
}
296+
MyPoint { x: 4, .. } => {
297+
}
298+
p => { // BAD: unused variable
299+
}
300+
}
301+
}
302+
303+
fn shadowing() -> i32 {
304+
let x = 1; // BAD: unused value [NOT DETECTED]
305+
let mut y: i32; // BAD: unused variable
270306

307+
{
308+
let x = 2;
309+
let mut y: i32;
310+
311+
{
312+
let x = 3; // BAD: unused value [NOT DETECTED]
313+
let mut y: i32; // BAD: unused variable
314+
}
315+
316+
y = x;
317+
return y;
318+
}
271319
}
272320

273321
fn main() {
@@ -278,6 +326,7 @@ fn main() {
278326
statics();
279327
loops();
280328
if_lets_matches();
329+
shadowing();
281330

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

0 commit comments

Comments
 (0)