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