From 1fc6a0e103b2047aca6ae3742e74540f3555569c Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Thu, 12 Sep 2024 18:51:16 +0100 Subject: [PATCH 01/10] Rust: Add placeholder query + tests for unused values query. --- .../src/queries/unusedentities/UnusedValue.ql | 12 ++ .../unusedentities/UnusedValue.expected | 1 + .../unusedentities/UnusedValue.qlref | 1 + .../test/query-tests/unusedentities/main.rs | 152 ++++++++++++++++++ 4 files changed, 166 insertions(+) create mode 100644 rust/ql/src/queries/unusedentities/UnusedValue.ql create mode 100644 rust/ql/test/query-tests/unusedentities/UnusedValue.expected create mode 100644 rust/ql/test/query-tests/unusedentities/UnusedValue.qlref create mode 100644 rust/ql/test/query-tests/unusedentities/main.rs diff --git a/rust/ql/src/queries/unusedentities/UnusedValue.ql b/rust/ql/src/queries/unusedentities/UnusedValue.ql new file mode 100644 index 000000000000..aab3760ddb7d --- /dev/null +++ b/rust/ql/src/queries/unusedentities/UnusedValue.ql @@ -0,0 +1,12 @@ + +/** + * @name Unused value + * @description Unused values may be an indication that the code is incomplete or has a typo. + * @kind problem + * @problem.severity recommendation + * @precision high + * @id rust/unused-value + * @tags maintainability + */ + +select 1 diff --git a/rust/ql/test/query-tests/unusedentities/UnusedValue.expected b/rust/ql/test/query-tests/unusedentities/UnusedValue.expected new file mode 100644 index 000000000000..2a4f078a25fc --- /dev/null +++ b/rust/ql/test/query-tests/unusedentities/UnusedValue.expected @@ -0,0 +1 @@ +| 1 | diff --git a/rust/ql/test/query-tests/unusedentities/UnusedValue.qlref b/rust/ql/test/query-tests/unusedentities/UnusedValue.qlref new file mode 100644 index 000000000000..d5ee4e655157 --- /dev/null +++ b/rust/ql/test/query-tests/unusedentities/UnusedValue.qlref @@ -0,0 +1 @@ +queries/unusedentities/UnusedValue.ql \ No newline at end of file diff --git a/rust/ql/test/query-tests/unusedentities/main.rs b/rust/ql/test/query-tests/unusedentities/main.rs new file mode 100644 index 000000000000..d2101b221090 --- /dev/null +++ b/rust/ql/test/query-tests/unusedentities/main.rs @@ -0,0 +1,152 @@ + +//fn cond() -> bool; + +// --- locals --- + +fn locals_1() { + let a = 1; // BAD: unused value + let b = 1; + let c = 1; + let d = String::from("a"); // BAD: unused value + let e = String::from("b"); + let _ = 1; // (deliberately unused) + + println!("use {}", b); + + if cond() { + println!("use {}", c); + } + + println!("use {}", e); +} + +fn locals_2() { + let a: i32; + let b: i32; // BAD: unused variable + let mut c: i32; + let mut d: i32; + let mut e: i32; + let mut f: i32; + let g: i32; + let h: i32; + let i: i32; + + b = 1; // BAD: unused value + + c = 1; // BAD: unused value + c = 2; + println!("use {}", c); + c = 3; // BAD: unused value + + d = 1; + if cond() { + d = 2; // BAD: unused value + d = 3; + } else { + } + println!("use {}", d); + + e = 1; // BAD: unused value + if cond() { + e = 2; + } else { + e = 3; + } + println!("use {}", e); + + f = 1; + f += 1; + println!("use {}", f); + f += 1; // BAD: unused value + f = 1; + f += 1; // BAD: unused value + + g = if cond() { 1 } else { 2 }; // BAD: unused value (x2) + h = if cond() { 3 } else { 4 }; + i = if cond() { h } else { 5 }; + println!("use {}", i); + + _ = 1; // (deliberately unused) +} + +// --- structs --- + +#[derive(Debug)] +struct MyStruct { + val: i64 +} + +impl MyStruct { + fn my_get(&mut self) -> i64 { + return self.val; + } +} + +fn structs() { + let a = MyStruct {val : 1 }; // BAD: unused value + let b = MyStruct {val : 2 }; + let c = MyStruct {val : 3 }; + let mut d : MyStruct; // BAD: unused variable + let mut e : MyStruct; + let mut f : MyStruct; + + println!("lets use {:?} and {}", b, c.val); + + e = MyStruct {val : 4 }; + println!("lets use {}", e.my_get()); + e.val = 5; + println!("lets use {}", e.my_get()); + + f = MyStruct {val : 6 }; // BAD: unused value + f.val = 7; // BAD: unused value +} + +// --- arrays --- + +fn arrays() { + let is = [1, 2, 3]; // BAD: unused values (x3) + let js = [1, 2, 3]; + let ks = [1, 2, 3]; + + println!("lets use {:?}", js); + + for k in ks { + println!("lets use {}", k); + } +} + +// --- constants and statics --- + +const CON1: i32 = 1; +const CON2: i32 = 2; // BAD: unused value +static mut STAT1: i32 = 1; +static mut STAT2: i32 = 2; // BAD: unused value + +fn statics() { + static mut STAT3: i32 = 0; + static mut STAT4: i32 = 0; // BAD: unused value + + unsafe + { + let total = CON1 + STAT1 + STAT3; + } +} + +// --- parameters --- + +fn parameters( + x: i32, + y: i32, // BAD: unused variable + _z: i32 // (`_` is asking the compiler, and by extension us, to not warn that this is unused) + ) -> i32 { + return x; +} + +fn main() { + locals_1(); + locals_2(); + structs(); + arrays(); + statics(); + println!("lets use result {}", parameters(1, 2, 3)); +} From b7ad331b753807c23ed5df7178d753c2ba005bf7 Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Tue, 17 Sep 2024 15:14:08 +0100 Subject: [PATCH 02/10] Rust: Add qhelp and example for the unused values query. --- .../queries/unusedentities/UnusedValue.qhelp | 24 +++++++++++++++++++ .../queries/unusedentities/UnusedValueBad.rs | 10 ++++++++ .../queries/unusedentities/UnusedValueGood.rs | 9 +++++++ 3 files changed, 43 insertions(+) create mode 100644 rust/ql/src/queries/unusedentities/UnusedValue.qhelp create mode 100644 rust/ql/src/queries/unusedentities/UnusedValueBad.rs create mode 100644 rust/ql/src/queries/unusedentities/UnusedValueGood.rs diff --git a/rust/ql/src/queries/unusedentities/UnusedValue.qhelp b/rust/ql/src/queries/unusedentities/UnusedValue.qhelp new file mode 100644 index 000000000000..8bfab5f17344 --- /dev/null +++ b/rust/ql/src/queries/unusedentities/UnusedValue.qhelp @@ -0,0 +1,24 @@ + + + + +

This rule finds values that are assigned to variables but never used. Unused values should be removed to increase readability and avoid confusion.

+
+ + +

Remove any unused values. Also remove any variables that only hold unused values.

+
+ + +

In the following example, there is a variable average that is initialized to 0, but that value is never used:

+ +

The problem can be fixed by removing the value and the variable that contains it:

+ +
+ + +
  • GeeksforGeeks: How to avoid unused Variable warning in Rust?
  • +
    +
    diff --git a/rust/ql/src/queries/unusedentities/UnusedValueBad.rs b/rust/ql/src/queries/unusedentities/UnusedValueBad.rs new file mode 100644 index 000000000000..9cc5729b5c24 --- /dev/null +++ b/rust/ql/src/queries/unusedentities/UnusedValueBad.rs @@ -0,0 +1,10 @@ +fn get_sum(values:&[i32]) -> i32 { + let mut sum = 0; + let mut average = 0; // BAD: unused value + + for v in values { + sum += v; + } + + return sum; +} diff --git a/rust/ql/src/queries/unusedentities/UnusedValueGood.rs b/rust/ql/src/queries/unusedentities/UnusedValueGood.rs new file mode 100644 index 000000000000..1b502ea29156 --- /dev/null +++ b/rust/ql/src/queries/unusedentities/UnusedValueGood.rs @@ -0,0 +1,9 @@ +fn get_sum(values:&[i32]) -> i32 { + let mut sum = 0; + + for v in values { + sum += v; + } + + return sum; +} From 68f8e171861b65308b1c098be0c4b89c85157bdf Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Tue, 17 Sep 2024 15:34:30 +0100 Subject: [PATCH 03/10] Rust: Add a placeholder query + tests for unused variables query. --- rust/ql/src/queries/unusedentities/UnusedVariable.ql | 12 ++++++++++++ .../unusedentities/UnusedVariable.expected | 1 + .../query-tests/unusedentities/UnusedVariable.qlref | 1 + 3 files changed, 14 insertions(+) create mode 100644 rust/ql/src/queries/unusedentities/UnusedVariable.ql create mode 100644 rust/ql/test/query-tests/unusedentities/UnusedVariable.expected create mode 100644 rust/ql/test/query-tests/unusedentities/UnusedVariable.qlref diff --git a/rust/ql/src/queries/unusedentities/UnusedVariable.ql b/rust/ql/src/queries/unusedentities/UnusedVariable.ql new file mode 100644 index 000000000000..9dfd43b1a761 --- /dev/null +++ b/rust/ql/src/queries/unusedentities/UnusedVariable.ql @@ -0,0 +1,12 @@ + +/** + * @name Unused variable + * @description Unused variables may be an indication that the code is incomplete or has a typo. + * @kind problem + * @problem.severity recommendation + * @precision high + * @id rust/unused-variable + * @tags maintainability + */ + +select 1 diff --git a/rust/ql/test/query-tests/unusedentities/UnusedVariable.expected b/rust/ql/test/query-tests/unusedentities/UnusedVariable.expected new file mode 100644 index 000000000000..2a4f078a25fc --- /dev/null +++ b/rust/ql/test/query-tests/unusedentities/UnusedVariable.expected @@ -0,0 +1 @@ +| 1 | diff --git a/rust/ql/test/query-tests/unusedentities/UnusedVariable.qlref b/rust/ql/test/query-tests/unusedentities/UnusedVariable.qlref new file mode 100644 index 000000000000..a1cd7d0a904b --- /dev/null +++ b/rust/ql/test/query-tests/unusedentities/UnusedVariable.qlref @@ -0,0 +1 @@ +queries/unusedentities/UnusedVariable.ql \ No newline at end of file From f93fd7cd6b8ddc68d8773123c6b173cde66938a1 Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Tue, 17 Sep 2024 15:40:11 +0100 Subject: [PATCH 04/10] Rust: Add qhelp and example for the unused variable query. --- .../unusedentities/UnusedVariable.qhelp | 24 +++++++++++++++++++ .../unusedentities/UnusedVariableBad.rs | 10 ++++++++ .../unusedentities/UnusedVariableGood.rs | 9 +++++++ 3 files changed, 43 insertions(+) create mode 100644 rust/ql/src/queries/unusedentities/UnusedVariable.qhelp create mode 100644 rust/ql/src/queries/unusedentities/UnusedVariableBad.rs create mode 100644 rust/ql/src/queries/unusedentities/UnusedVariableGood.rs diff --git a/rust/ql/src/queries/unusedentities/UnusedVariable.qhelp b/rust/ql/src/queries/unusedentities/UnusedVariable.qhelp new file mode 100644 index 000000000000..c34283962f62 --- /dev/null +++ b/rust/ql/src/queries/unusedentities/UnusedVariable.qhelp @@ -0,0 +1,24 @@ + + + + +

    This rule finds variables that are never accessed. Unused variables should be removed to increase readability and avoid confusion.

    +
    + + +

    Remove any unused variables.

    +
    + + +

    In the following example, there is an unused variable average that is never used:

    + +

    The problem can be fixed simply by removing the variable:

    + +
    + + +
  • GeeksforGeeks: How to avoid unused Variable warning in Rust?
  • +
    +
    diff --git a/rust/ql/src/queries/unusedentities/UnusedVariableBad.rs b/rust/ql/src/queries/unusedentities/UnusedVariableBad.rs new file mode 100644 index 000000000000..e968ea6d80e7 --- /dev/null +++ b/rust/ql/src/queries/unusedentities/UnusedVariableBad.rs @@ -0,0 +1,10 @@ +fn get_sum(values:&[i32]) -> i32 { + let mut sum = 0; + let mut average; // BAD: unused variable + + for v in values { + sum += v; + } + + return sum; +} diff --git a/rust/ql/src/queries/unusedentities/UnusedVariableGood.rs b/rust/ql/src/queries/unusedentities/UnusedVariableGood.rs new file mode 100644 index 000000000000..1b502ea29156 --- /dev/null +++ b/rust/ql/src/queries/unusedentities/UnusedVariableGood.rs @@ -0,0 +1,9 @@ +fn get_sum(values:&[i32]) -> i32 { + let mut sum = 0; + + for v in values { + sum += v; + } + + return sum; +} From 1c7d5217a04f8f4b5704b02c20c9051ba0f0fb06 Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Tue, 17 Sep 2024 16:24:28 +0100 Subject: [PATCH 05/10] Rust: Autoformat. --- rust/ql/src/queries/unusedentities/UnusedValue.ql | 1 - rust/ql/src/queries/unusedentities/UnusedVariable.ql | 1 - 2 files changed, 2 deletions(-) diff --git a/rust/ql/src/queries/unusedentities/UnusedValue.ql b/rust/ql/src/queries/unusedentities/UnusedValue.ql index aab3760ddb7d..3a599b31b1ab 100644 --- a/rust/ql/src/queries/unusedentities/UnusedValue.ql +++ b/rust/ql/src/queries/unusedentities/UnusedValue.ql @@ -1,4 +1,3 @@ - /** * @name Unused value * @description Unused values may be an indication that the code is incomplete or has a typo. diff --git a/rust/ql/src/queries/unusedentities/UnusedVariable.ql b/rust/ql/src/queries/unusedentities/UnusedVariable.ql index 9dfd43b1a761..955ba1d7e3b3 100644 --- a/rust/ql/src/queries/unusedentities/UnusedVariable.ql +++ b/rust/ql/src/queries/unusedentities/UnusedVariable.ql @@ -1,4 +1,3 @@ - /** * @name Unused variable * @description Unused variables may be an indication that the code is incomplete or has a typo. From a2bf2c7edb5d6940886915d80f61e85d7a0ff9fa Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Tue, 17 Sep 2024 16:53:02 +0100 Subject: [PATCH 06/10] Rust: Fix query result columns. --- rust/ql/src/queries/unusedentities/UnusedValue.ql | 2 +- rust/ql/src/queries/unusedentities/UnusedVariable.ql | 2 +- rust/ql/test/query-tests/unusedentities/UnusedValue.expected | 2 +- rust/ql/test/query-tests/unusedentities/UnusedVariable.expected | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/rust/ql/src/queries/unusedentities/UnusedValue.ql b/rust/ql/src/queries/unusedentities/UnusedValue.ql index 3a599b31b1ab..b6bd2d7730a6 100644 --- a/rust/ql/src/queries/unusedentities/UnusedValue.ql +++ b/rust/ql/src/queries/unusedentities/UnusedValue.ql @@ -8,4 +8,4 @@ * @tags maintainability */ -select 1 +select 1, "Variable is assigned a value that is never used." diff --git a/rust/ql/src/queries/unusedentities/UnusedVariable.ql b/rust/ql/src/queries/unusedentities/UnusedVariable.ql index 955ba1d7e3b3..78ed6573b750 100644 --- a/rust/ql/src/queries/unusedentities/UnusedVariable.ql +++ b/rust/ql/src/queries/unusedentities/UnusedVariable.ql @@ -8,4 +8,4 @@ * @tags maintainability */ -select 1 +select 1, "Variable is not used." diff --git a/rust/ql/test/query-tests/unusedentities/UnusedValue.expected b/rust/ql/test/query-tests/unusedentities/UnusedValue.expected index 2a4f078a25fc..d7571f7a745f 100644 --- a/rust/ql/test/query-tests/unusedentities/UnusedValue.expected +++ b/rust/ql/test/query-tests/unusedentities/UnusedValue.expected @@ -1 +1 @@ -| 1 | +| 1 | Variable is assigned a value that is never used. | diff --git a/rust/ql/test/query-tests/unusedentities/UnusedVariable.expected b/rust/ql/test/query-tests/unusedentities/UnusedVariable.expected index 2a4f078a25fc..7a7841c6f51e 100644 --- a/rust/ql/test/query-tests/unusedentities/UnusedVariable.expected +++ b/rust/ql/test/query-tests/unusedentities/UnusedVariable.expected @@ -1 +1 @@ -| 1 | +| 1 | Variable is not used. | From 463a1254d32daa98ab4d19c2dbbffc3f81521480 Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Wed, 18 Sep 2024 12:55:11 +0100 Subject: [PATCH 07/10] Update rust/ql/src/queries/unusedentities/UnusedValueBad.rs Co-authored-by: Paolo Tranquilli --- rust/ql/src/queries/unusedentities/UnusedValueBad.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/rust/ql/src/queries/unusedentities/UnusedValueBad.rs b/rust/ql/src/queries/unusedentities/UnusedValueBad.rs index 9cc5729b5c24..7eb2cc0a2012 100644 --- a/rust/ql/src/queries/unusedentities/UnusedValueBad.rs +++ b/rust/ql/src/queries/unusedentities/UnusedValueBad.rs @@ -1,4 +1,4 @@ -fn get_sum(values:&[i32]) -> i32 { +fn get_average(values:&[i32]) -> i32 { let mut sum = 0; let mut average = 0; // BAD: unused value @@ -6,5 +6,6 @@ fn get_sum(values:&[i32]) -> i32 { sum += v; } - return sum; + average = sum / values.len(); + return average; } From d79aa073ea75dc09d08ff193a87670f5feddf682 Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Wed, 18 Sep 2024 13:03:09 +0100 Subject: [PATCH 08/10] Rust: Clean up the UnusedValue examples. --- rust/ql/src/queries/unusedentities/UnusedValue.qhelp | 2 +- rust/ql/src/queries/unusedentities/UnusedValueBad.rs | 6 +++--- rust/ql/src/queries/unusedentities/UnusedValueGood.rs | 6 ++++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/rust/ql/src/queries/unusedentities/UnusedValue.qhelp b/rust/ql/src/queries/unusedentities/UnusedValue.qhelp index 8bfab5f17344..bfcdd0e08771 100644 --- a/rust/ql/src/queries/unusedentities/UnusedValue.qhelp +++ b/rust/ql/src/queries/unusedentities/UnusedValue.qhelp @@ -14,7 +14,7 @@

    In the following example, there is a variable average that is initialized to 0, but that value is never used:

    -

    The problem can be fixed by removing the value and the variable that contains it:

    +

    The problem can be fixed by removing the unused value:

    diff --git a/rust/ql/src/queries/unusedentities/UnusedValueBad.rs b/rust/ql/src/queries/unusedentities/UnusedValueBad.rs index 7eb2cc0a2012..1256a8d95a4c 100644 --- a/rust/ql/src/queries/unusedentities/UnusedValueBad.rs +++ b/rust/ql/src/queries/unusedentities/UnusedValueBad.rs @@ -1,11 +1,11 @@ -fn get_average(values:&[i32]) -> i32 { +fn get_average(values:&[i32]) -> f64 { let mut sum = 0; - let mut average = 0; // BAD: unused value + let mut average = 0.0; // BAD: unused value for v in values { sum += v; } - average = sum / values.len(); + average = sum as f64 / values.len() as f64; return average; } diff --git a/rust/ql/src/queries/unusedentities/UnusedValueGood.rs b/rust/ql/src/queries/unusedentities/UnusedValueGood.rs index 1b502ea29156..0b3c69302031 100644 --- a/rust/ql/src/queries/unusedentities/UnusedValueGood.rs +++ b/rust/ql/src/queries/unusedentities/UnusedValueGood.rs @@ -1,9 +1,11 @@ -fn get_sum(values:&[i32]) -> i32 { +fn get_average(values:&[i32]) -> f64 { let mut sum = 0; + let average; for v in values { sum += v; } - return sum; + average = sum as f64 / values.len() as f64; + return average; } From 3632a76eafd0888872e288feb3dfce76404a3535 Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Wed, 18 Sep 2024 13:05:06 +0100 Subject: [PATCH 09/10] Rust: Add test annotations. --- .../test/query-tests/unusedentities/main.rs | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/rust/ql/test/query-tests/unusedentities/main.rs b/rust/ql/test/query-tests/unusedentities/main.rs index d2101b221090..8066f7c783e0 100644 --- a/rust/ql/test/query-tests/unusedentities/main.rs +++ b/rust/ql/test/query-tests/unusedentities/main.rs @@ -4,10 +4,10 @@ // --- locals --- fn locals_1() { - let a = 1; // BAD: unused value + let a = 1; // BAD: unused value [NOT DETECTED] let b = 1; let c = 1; - let d = String::from("a"); // BAD: unused value + let d = String::from("a"); // BAD: unused value [NOT DETECTED] let e = String::from("b"); let _ = 1; // (deliberately unused) @@ -22,7 +22,7 @@ fn locals_1() { fn locals_2() { let a: i32; - let b: i32; // BAD: unused variable + let b: i32; // BAD: unused variable [NOT DETECTED] let mut c: i32; let mut d: i32; let mut e: i32; @@ -31,22 +31,22 @@ fn locals_2() { let h: i32; let i: i32; - b = 1; // BAD: unused value + b = 1; // BAD: unused value [NOT DETECTED] - c = 1; // BAD: unused value + c = 1; // BAD: unused value [NOT DETECTED] c = 2; println!("use {}", c); - c = 3; // BAD: unused value + c = 3; // BAD: unused value [NOT DETECTED] d = 1; if cond() { - d = 2; // BAD: unused value + d = 2; // BAD: unused value [NOT DETECTED] d = 3; } else { } println!("use {}", d); - e = 1; // BAD: unused value + e = 1; // BAD: unused value [NOT DETECTED] if cond() { e = 2; } else { @@ -57,16 +57,16 @@ fn locals_2() { f = 1; f += 1; println!("use {}", f); - f += 1; // BAD: unused value + f += 1; // BAD: unused value [NOT DETECTED] f = 1; - f += 1; // BAD: unused value + f += 1; // BAD: unused value [NOT DETECTED] - g = if cond() { 1 } else { 2 }; // BAD: unused value (x2) + g = if cond() { 1 } else { 2 }; // BAD: unused value (x2) [NOT DETECTED] h = if cond() { 3 } else { 4 }; i = if cond() { h } else { 5 }; println!("use {}", i); - _ = 1; // (deliberately unused) + _ = 1; // (deliberately unused) [NOT DETECTED] } // --- structs --- @@ -83,10 +83,10 @@ impl MyStruct { } fn structs() { - let a = MyStruct {val : 1 }; // BAD: unused value + let a = MyStruct {val : 1 }; // BAD: unused value [NOT DETECTED] let b = MyStruct {val : 2 }; let c = MyStruct {val : 3 }; - let mut d : MyStruct; // BAD: unused variable + let mut d : MyStruct; // BAD: unused variable [NOT DETECTED] let mut e : MyStruct; let mut f : MyStruct; @@ -97,14 +97,14 @@ fn structs() { e.val = 5; println!("lets use {}", e.my_get()); - f = MyStruct {val : 6 }; // BAD: unused value - f.val = 7; // BAD: unused value + f = MyStruct {val : 6 }; // BAD: unused value [NOT DETECTED] + f.val = 7; // BAD: unused value [NOT DETECTED] } // --- arrays --- fn arrays() { - let is = [1, 2, 3]; // BAD: unused values (x3) + let is = [1, 2, 3]; // BAD: unused values (x3) [NOT DETECTED] let js = [1, 2, 3]; let ks = [1, 2, 3]; @@ -118,13 +118,13 @@ fn arrays() { // --- constants and statics --- const CON1: i32 = 1; -const CON2: i32 = 2; // BAD: unused value +const CON2: i32 = 2; // BAD: unused value [NOT DETECTED] static mut STAT1: i32 = 1; -static mut STAT2: i32 = 2; // BAD: unused value +static mut STAT2: i32 = 2; // BAD: unused value [NOT DETECTED] fn statics() { static mut STAT3: i32 = 0; - static mut STAT4: i32 = 0; // BAD: unused value + static mut STAT4: i32 = 0; // BAD: unused value [NOT DETECTED] unsafe { @@ -136,7 +136,7 @@ fn statics() { fn parameters( x: i32, - y: i32, // BAD: unused variable + y: i32, // BAD: unused variable [NOT DETECTED] _z: i32 // (`_` is asking the compiler, and by extension us, to not warn that this is unused) ) -> i32 { return x; From 2769bd6f35b661dbee57c1d82fa94ce639890b11 Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Wed, 18 Sep 2024 13:07:27 +0100 Subject: [PATCH 10/10] Rust: Make the placeholder queries produce no results, to avoid confusion. --- rust/ql/src/queries/unusedentities/UnusedValue.ql | 8 ++++++-- rust/ql/src/queries/unusedentities/UnusedVariable.ql | 8 ++++++-- .../test/query-tests/unusedentities/UnusedValue.expected | 1 - .../query-tests/unusedentities/UnusedVariable.expected | 1 - 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/rust/ql/src/queries/unusedentities/UnusedValue.ql b/rust/ql/src/queries/unusedentities/UnusedValue.ql index b6bd2d7730a6..a22413696822 100644 --- a/rust/ql/src/queries/unusedentities/UnusedValue.ql +++ b/rust/ql/src/queries/unusedentities/UnusedValue.ql @@ -3,9 +3,13 @@ * @description Unused values may be an indication that the code is incomplete or has a typo. * @kind problem * @problem.severity recommendation - * @precision high + * @precision medium * @id rust/unused-value * @tags maintainability */ -select 1, "Variable is assigned a value that is never used." +import rust + +from Locatable e +where none() // TODO: implement query +select e, "Variable is assigned a value that is never used." diff --git a/rust/ql/src/queries/unusedentities/UnusedVariable.ql b/rust/ql/src/queries/unusedentities/UnusedVariable.ql index 78ed6573b750..a057db7d1734 100644 --- a/rust/ql/src/queries/unusedentities/UnusedVariable.ql +++ b/rust/ql/src/queries/unusedentities/UnusedVariable.ql @@ -3,9 +3,13 @@ * @description Unused variables may be an indication that the code is incomplete or has a typo. * @kind problem * @problem.severity recommendation - * @precision high + * @precision medium * @id rust/unused-variable * @tags maintainability */ -select 1, "Variable is not used." +import rust + +from Locatable e +where none() // TODO: implement query +select e, "Variable is not used." diff --git a/rust/ql/test/query-tests/unusedentities/UnusedValue.expected b/rust/ql/test/query-tests/unusedentities/UnusedValue.expected index d7571f7a745f..e69de29bb2d1 100644 --- a/rust/ql/test/query-tests/unusedentities/UnusedValue.expected +++ b/rust/ql/test/query-tests/unusedentities/UnusedValue.expected @@ -1 +0,0 @@ -| 1 | Variable is assigned a value that is never used. | diff --git a/rust/ql/test/query-tests/unusedentities/UnusedVariable.expected b/rust/ql/test/query-tests/unusedentities/UnusedVariable.expected index 7a7841c6f51e..e69de29bb2d1 100644 --- a/rust/ql/test/query-tests/unusedentities/UnusedVariable.expected +++ b/rust/ql/test/query-tests/unusedentities/UnusedVariable.expected @@ -1 +0,0 @@ -| 1 | Variable is not used. |