From 41218fb89fb510e8b7e614a809bd28b1138808f4 Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Tue, 8 Oct 2024 17:33:21 +0100 Subject: [PATCH 1/4] Rust: Test spacing / variable names. --- .../unusedentities/UnusedVariable.expected | 12 ++--- .../test/query-tests/unusedentities/main.rs | 44 +++++++++++++++---- 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/rust/ql/test/query-tests/unusedentities/UnusedVariable.expected b/rust/ql/test/query-tests/unusedentities/UnusedVariable.expected index fd4b8d3d2b46..ccf6117f2966 100644 --- a/rust/ql/test/query-tests/unusedentities/UnusedVariable.expected +++ b/rust/ql/test/query-tests/unusedentities/UnusedVariable.expected @@ -5,9 +5,9 @@ | 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:248:24:248:26 | val | Variable is not used. | diff --git a/rust/ql/test/query-tests/unusedentities/main.rs b/rust/ql/test/query-tests/unusedentities/main.rs index a34219f8a7ab..be9fdaed2e4f 100644 --- a/rust/ql/test/query-tests/unusedentities/main.rs +++ b/rust/ql/test/query-tests/unusedentities/main.rs @@ -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); } } @@ -189,7 +189,14 @@ enum YesOrNo { No, } -fn if_lets() { + + + + + + + +fn if_lets_matches() { let mut total: i64 = 0; if let Some(a) = Some(10) { // BAD: unused variable @@ -228,18 +235,39 @@ fn if_lets() { } } - let e = MyOption::Some(80); - match e { + + + + + + + + + let f = MyOption::Some(90); + match f { MyOption::Some(val) => { // BAD: unused variable } MyOption::None => {} } - let f = YesOrNo::Yes; - match f { + + + + + + + + let h = YesOrNo::Yes; + match h { YesOrNo::Yes => {} YesOrNo::No => {} } + + + + + + } fn main() { @@ -249,7 +277,7 @@ fn main() { arrays(); statics(); loops(); - if_lets(); + if_lets_matches(); println!("lets use result {}", parameters(1, 2, 3)); } From 7a6eabc8a3ec0d1450c7e94658700b9f00d79bba Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Tue, 8 Oct 2024 17:38:44 +0100 Subject: [PATCH 2/4] Rust: Additional test cases. --- .../unusedentities/UnusedVariable.expected | 10 +++ .../test/query-tests/unusedentities/main.rs | 85 +++++++++++++++---- 2 files changed, 77 insertions(+), 18 deletions(-) diff --git a/rust/ql/test/query-tests/unusedentities/UnusedVariable.expected b/rust/ql/test/query-tests/unusedentities/UnusedVariable.expected index ccf6117f2966..a69c63ec424e 100644 --- a/rust/ql/test/query-tests/unusedentities/UnusedVariable.expected +++ b/rust/ql/test/query-tests/unusedentities/UnusedVariable.expected @@ -10,4 +10,14 @@ | 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:290:25:290:25 | y | Variable is not used. | +| main.rs:294:28:294:28 | a | Variable is not used. | +| main.rs:298:9:298:9 | p | Variable is not used. | +| main.rs:305:13:305:13 | y | Variable is not used. | +| main.rs:313:21:313:21 | y | Variable is not used. | diff --git a/rust/ql/test/query-tests/unusedentities/main.rs b/rust/ql/test/query-tests/unusedentities/main.rs index be9fdaed2e4f..1d989d2f10c4 100644 --- a/rust/ql/test/query-tests/unusedentities/main.rs +++ b/rust/ql/test/query-tests/unusedentities/main.rs @@ -189,12 +189,12 @@ enum YesOrNo { No, } +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; @@ -235,13 +235,13 @@ fn if_lets_matches() { } } - - - - - - - + let e = Option::Some(80); + match e { + Option::Some(val) => { // BAD: unused variable + } + Option::None => { + } + } let f = MyOption::Some(90); match f { @@ -250,12 +250,12 @@ fn if_lets_matches() { MyOption::None => {} } - - - - - - + let g : Result = Ok(100); + match g { + Ok(_) => { + } + Err(num) => {} // BAD: unused variable + } let h = YesOrNo::Yes; match h { @@ -263,11 +263,59 @@ fn if_lets_matches() { YesOrNo::No => {} } + let i = Yes; + match i { + Yes => {} // SPURIOUS: unused variable 'None' + No => {} // SPURIOUS: unused variable 'None' + } + if let j = Yes { // BAD: unused variable + } + if let k = Yes { + match k { + _ => {} + } + } + 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() { @@ -278,6 +326,7 @@ fn main() { statics(); loops(); if_lets_matches(); + shadowing(); println!("lets use result {}", parameters(1, 2, 3)); } From fa1c92dbe7b9b111a6476eea6a03fd82704360a6 Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Wed, 9 Oct 2024 11:50:29 +0100 Subject: [PATCH 3/4] Rust: Another case worth having. --- .../unusedentities/UnusedVariable.expected | 11 ++++++----- rust/ql/test/query-tests/unusedentities/main.rs | 4 ++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/rust/ql/test/query-tests/unusedentities/UnusedVariable.expected b/rust/ql/test/query-tests/unusedentities/UnusedVariable.expected index a69c63ec424e..66b5ac84ab33 100644 --- a/rust/ql/test/query-tests/unusedentities/UnusedVariable.expected +++ b/rust/ql/test/query-tests/unusedentities/UnusedVariable.expected @@ -16,8 +16,9 @@ | 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:290:25:290:25 | y | Variable is not used. | -| main.rs:294:28:294:28 | a | Variable is not used. | -| main.rs:298:9:298:9 | p | Variable is not used. | -| main.rs:305:13:305:13 | y | Variable is not used. | -| main.rs:313:21:313:21 | y | 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. | diff --git a/rust/ql/test/query-tests/unusedentities/main.rs b/rust/ql/test/query-tests/unusedentities/main.rs index 1d989d2f10c4..6d883cece8c5 100644 --- a/rust/ql/test/query-tests/unusedentities/main.rs +++ b/rust/ql/test/query-tests/unusedentities/main.rs @@ -278,6 +278,10 @@ fn if_lets_matches() { } } + let l = Yes; + if let Yes = l { // SPURIOUS: unused variable 'Yes' + } + match 1 { 1 => {} _ => {} From f3d727fadc9fa4800954c1ee0ff2c4d8e7564a62 Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Wed, 9 Oct 2024 11:52:24 +0100 Subject: [PATCH 4/4] Rust: Correct spurious annotations. --- rust/ql/test/query-tests/unusedentities/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rust/ql/test/query-tests/unusedentities/main.rs b/rust/ql/test/query-tests/unusedentities/main.rs index 6d883cece8c5..d7c08f05b62a 100644 --- a/rust/ql/test/query-tests/unusedentities/main.rs +++ b/rust/ql/test/query-tests/unusedentities/main.rs @@ -265,8 +265,8 @@ fn if_lets_matches() { let i = Yes; match i { - Yes => {} // SPURIOUS: unused variable 'None' - No => {} // SPURIOUS: unused variable 'None' + Yes => {} // SPURIOUS: unused variable 'Yes' + No => {} // SPURIOUS: unused variable 'No' } if let j = Yes { // BAD: unused variable