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. |