Skip to content

Commit

Permalink
Merge pull request #17710 from geoffw0/unusedvar3
Browse files Browse the repository at this point in the history
Rust: More test cases for unused variables
  • Loading branch information
geoffw0 authored Oct 9, 2024
2 parents 6ffdf57 + f3d727f commit ed39c46
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 13 deletions.
23 changes: 17 additions & 6 deletions rust/ql/test/query-tests/unusedentities/UnusedVariable.expected
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,20 @@
| main.rs:164:9:164:9 | x | Variable is not used. |
| main.rs:169:9:169:9 | x | Variable is not used. |
| main.rs:174:9:174:9 | x | Variable is not used. |
| main.rs:195:17:195:17 | a | Variable is not used. |
| main.rs:203:20:203:22 | val | Variable is not used. |
| main.rs:216:14:216:16 | val | Variable is not used. |
| main.rs:218:9:218:12 | None | Variable is not used. |
| main.rs:227:9:227:12 | None | Variable is not used. |
| main.rs:233:24:233:26 | val | Variable is not used. |
| main.rs:202:17:202:17 | a | Variable is not used. |
| main.rs:210:20:210:22 | val | Variable is not used. |
| main.rs:223:14:223:16 | val | Variable is not used. |
| main.rs:225:9:225:12 | None | Variable is not used. |
| main.rs:234:9:234:12 | None | Variable is not used. |
| main.rs:240:22:240:24 | val | Variable is not used. |
| main.rs:248:24:248:26 | val | Variable is not used. |
| main.rs:257:13:257:15 | num | Variable is not used. |
| main.rs:268:9:268:11 | Yes | Variable is not used. |
| main.rs:269:9:269:10 | No | Variable is not used. |
| main.rs:272:12:272:12 | j | Variable is not used. |
| main.rs:282:12:282:14 | Yes | Variable is not used. |
| main.rs:294:25:294:25 | y | Variable is not used. |
| main.rs:298:28:298:28 | a | Variable is not used. |
| main.rs:302:9:302:9 | p | Variable is not used. |
| main.rs:309:13:309:13 | y | Variable is not used. |
| main.rs:317:21:317:21 | y | Variable is not used. |
95 changes: 88 additions & 7 deletions rust/ql/test/query-tests/unusedentities/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,12 @@ fn loops() {
for _ in 1..10 {}

for x // SPURIOUS: unused variable [macros not yet supported]
in 1..10 {
in 1..10 {
println!("x is {}", x);
}

for x // SPURIOUS: unused variable [macros not yet supported]
in 1..10 {
in 1..10 {
assert!(x != 11);
}
}
Expand All @@ -189,7 +189,14 @@ enum YesOrNo {
No,
}

fn if_lets() {
use YesOrNo::{Yes, No}; // allows `Yes`, `No` to be accessed without qualifiers.

struct MyPoint {
x: i64,
y: i64,
}

fn if_lets_matches() {
let mut total: i64 = 0;

if let Some(a) = Some(10) { // BAD: unused variable
Expand Down Expand Up @@ -228,18 +235,91 @@ fn if_lets() {
}
}

let e = MyOption::Some(80);
let e = Option::Some(80);
match e {
Option::Some(val) => { // BAD: unused variable
}
Option::None => {
}
}

let f = MyOption::Some(90);
match f {
MyOption::Some(val) => { // BAD: unused variable
}
MyOption::None => {}
}

let f = YesOrNo::Yes;
match f {
let g : Result<i64, i64> = Ok(100);
match g {
Ok(_) => {
}
Err(num) => {} // BAD: unused variable
}

let h = YesOrNo::Yes;
match h {
YesOrNo::Yes => {}
YesOrNo::No => {}
}

let i = Yes;
match i {
Yes => {} // SPURIOUS: unused variable 'Yes'
No => {} // SPURIOUS: unused variable 'No'
}

if let j = Yes { // BAD: unused variable
}

if let k = Yes {
match k {
_ => {}
}
}

let l = Yes;
if let Yes = l { // SPURIOUS: unused variable 'Yes'
}

match 1 {
1 => {}
_ => {}
}

let p1 = MyPoint { x: 1, y: 2 };
match p1 {
MyPoint { x: 0, y: 0 } => {
}
MyPoint { x: 1, y } => { // BAD: unused variable
}
MyPoint { x: 2, y: _ } => {
}
MyPoint { x: 3, y: a } => { // BAD: unused variable
}
MyPoint { x: 4, .. } => {
}
p => { // BAD: unused variable
}
}
}

fn shadowing() -> i32 {
let x = 1; // BAD: unused value [NOT DETECTED]
let mut y: i32; // BAD: unused variable

{
let x = 2;
let mut y: i32;

{
let x = 3; // BAD: unused value [NOT DETECTED]
let mut y: i32; // BAD: unused variable
}

y = x;
return y;
}
}

fn main() {
Expand All @@ -249,7 +329,8 @@ fn main() {
arrays();
statics();
loops();
if_lets();
if_lets_matches();
shadowing();

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

0 comments on commit ed39c46

Please sign in to comment.