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] 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; +}