Skip to content

Commit d7aa5f1

Browse files
authored
Merge pull request #17497 from geoffw0/unusedvar
Rust: Placeholder queries for unused variable, unused value
2 parents 1bffc2a + 2769bd6 commit d7aa5f1

File tree

13 files changed

+273
-0
lines changed

13 files changed

+273
-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 values that are assigned to variables but never used. Unused values should be removed to increase readability and avoid confusion.</p>
8+
</overview>
9+
10+
<recommendation>
11+
<p>Remove any unused values. Also remove any variables that only hold unused values.</p>
12+
</recommendation>
13+
14+
<example>
15+
<p>In the following example, there is a variable <code>average</code> that is initialized to <code>0</code>, but that value is never used:</p>
16+
<sample src="UnusedValueBad.rs" />
17+
<p>The problem can be fixed by removing the unused value:</p>
18+
<sample src="UnusedValueGood.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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @name Unused value
3+
* @description Unused values may be an indication that the code is incomplete or has a typo.
4+
* @kind problem
5+
* @problem.severity recommendation
6+
* @precision medium
7+
* @id rust/unused-value
8+
* @tags maintainability
9+
*/
10+
11+
import rust
12+
13+
from Locatable e
14+
where none() // TODO: implement query
15+
select e, "Variable is assigned a value that is never used."
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fn get_average(values:&[i32]) -> f64 {
2+
let mut sum = 0;
3+
let mut average = 0.0; // BAD: unused value
4+
5+
for v in values {
6+
sum += v;
7+
}
8+
9+
average = sum as f64 / values.len() as f64;
10+
return average;
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fn get_average(values:&[i32]) -> f64 {
2+
let mut sum = 0;
3+
let average;
4+
5+
for v in values {
6+
sum += v;
7+
}
8+
9+
average = sum as f64 / values.len() as f64;
10+
return average;
11+
}
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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @name Unused variable
3+
* @description Unused variables may be an indication that the code is incomplete or has a typo.
4+
* @kind problem
5+
* @problem.severity recommendation
6+
* @precision medium
7+
* @id rust/unused-variable
8+
* @tags maintainability
9+
*/
10+
11+
import rust
12+
13+
from Locatable e
14+
where none() // TODO: implement query
15+
select e, "Variable is not used."
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+
}

rust/ql/test/query-tests/unusedentities/UnusedValue.expected

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
queries/unusedentities/UnusedValue.ql

0 commit comments

Comments
 (0)