Skip to content

Commit f93fd7c

Browse files
committed
Rust: Add qhelp and example for the unused variable query.
1 parent 68f8e17 commit f93fd7c

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
6+
<overview>
7+
<p>This rule finds variables that are never accessed. Unused variables should be removed to increase readability and avoid confusion.</p>
8+
</overview>
9+
10+
<recommendation>
11+
<p>Remove any unused variables.</p>
12+
</recommendation>
13+
14+
<example>
15+
<p>In the following example, there is an unused variable <code>average</code> that is never used:</p>
16+
<sample src="UnusedVariableBad.rs" />
17+
<p>The problem can be fixed simply by removing the variable:</p>
18+
<sample src="UnusedVariableGood.rs" />
19+
</example>
20+
21+
<references>
22+
<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>
23+
</references>
24+
</qhelp>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
fn get_sum(values:&[i32]) -> i32 {
2+
let mut sum = 0;
3+
let mut average; // BAD: unused variable
4+
5+
for v in values {
6+
sum += v;
7+
}
8+
9+
return sum;
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn get_sum(values:&[i32]) -> i32 {
2+
let mut sum = 0;
3+
4+
for v in values {
5+
sum += v;
6+
}
7+
8+
return sum;
9+
}

0 commit comments

Comments
 (0)