Skip to content

Commit

Permalink
Rust: Add qhelp and example for the unused variable query.
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffw0 committed Sep 17, 2024
1 parent 68f8e17 commit f93fd7c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
24 changes: 24 additions & 0 deletions rust/ql/src/queries/unusedentities/UnusedVariable.qhelp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>

<overview>
<p>This rule finds variables that are never accessed. Unused variables should be removed to increase readability and avoid confusion.</p>
</overview>

<recommendation>
<p>Remove any unused variables.</p>
</recommendation>

<example>
<p>In the following example, there is an unused variable <code>average</code> that is never used:</p>
<sample src="UnusedVariableBad.rs" />
<p>The problem can be fixed simply by removing the variable:</p>
<sample src="UnusedVariableGood.rs" />
</example>

<references>
<li>GeeksforGeeks: <a href="https://www.geeksforgeeks.org/how-to-avoid-unused-variable-warning-in-rust/">How to avoid unused Variable warning in Rust?</a></li>
</references>
</qhelp>
10 changes: 10 additions & 0 deletions rust/ql/src/queries/unusedentities/UnusedVariableBad.rs
Original file line number Diff line number Diff line change
@@ -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;
}
9 changes: 9 additions & 0 deletions rust/ql/src/queries/unusedentities/UnusedVariableGood.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fn get_sum(values:&[i32]) -> i32 {
let mut sum = 0;

for v in values {
sum += v;
}

return sum;
}

0 comments on commit f93fd7c

Please sign in to comment.