Skip to content

Commit 93836a5

Browse files
committed
Rust: Clean up code / clarify responsibilities and fix the issue in rust/unused-value as well.
1 parent 278760c commit 93836a5

File tree

4 files changed

+26
-17
lines changed

4 files changed

+26
-17
lines changed

rust/ql/src/queries/unusedentities/UnusedVariable.ql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@ import rust
1212
import UnusedVariable
1313

1414
from Variable v
15-
where isUnused(v)
15+
where
16+
isUnused(v) and
17+
not isAllowableUnused(v) and
18+
not v instanceof DiscardVariable
1619
select v, "Variable '" + v + "' is not used."
Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
import rust
22

3-
/** A deliberately unused variable. */
3+
/**
4+
* A deliberately unused variable, for example `_` or `_x`.
5+
*/
46
class DiscardVariable extends Variable {
57
DiscardVariable() { this.getName().charAt(0) = "_" }
68
}
79

8-
/** Holds if variable `v` is unused. */
10+
/**
11+
* Holds if variable `v` is unused.
12+
*/
913
predicate isUnused(Variable v) {
1014
// variable is accessed or initialized
1115
not exists(v.getAnAccess()) and
12-
not exists(v.getInitializer()) and
13-
// variable is intentionally unused
14-
not v instanceof DiscardVariable and
15-
// variable is in a context where is may not have a use
16-
not v.getPat().isInMacroExpansion() and
17-
not exists(FnPtrType fp | fp.getParamList().getParam(_).getPat() = v.getPat())
16+
not exists(v.getInitializer())
17+
}
18+
19+
/**
20+
* Holds if variable `v` is in a context where we may not find a use for it,
21+
* but that's expected and should not be considered a problem.
22+
*/
23+
predicate isAllowableUnused(Variable v) {
24+
// in a macro expansion
25+
v.getPat().isInMacroExpansion() or
26+
// function pointer parameters
27+
exists(FnPtrType fp | fp.getParamList().getParam(_).getPat() = v.getPat())
1828
}

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
| main.rs:373:9:373:9 | x | Variable $@ is assigned a value that is never used. | main.rs:373:9:373:9 | x | x |
1616
| main.rs:381:17:381:17 | x | Variable $@ is assigned a value that is never used. | main.rs:381:17:381:17 | x | x |
1717
| main.rs:482:9:482:9 | c | Variable $@ is assigned a value that is never used. | main.rs:482:9:482:9 | c | c |
18-
| main.rs:494:16:494:16 | x | Variable $@ is assigned a value that is never used. | main.rs:494:16:494:16 | x | x |
19-
| main.rs:495:16:495:16 | y | Variable $@ is assigned a value that is never used. | main.rs:495:16:495:16 | y | y |
20-
| main.rs:496:12:496:12 | z | Variable $@ is assigned a value that is never used. | main.rs:496:12:496:12 | z | z |
21-
| main.rs:499:18:499:18 | x | Variable $@ is assigned a value that is never used. | main.rs:499:18:499:18 | x | x |
2218
| more.rs:44:9:44:14 | a_ptr4 | Variable $@ is assigned a value that is never used. | more.rs:44:9:44:14 | a_ptr4 | a_ptr4 |
2319
| more.rs:59:9:59:13 | d_ptr | Variable $@ is assigned a value that is never used. | more.rs:59:9:59:13 | d_ptr | d_ptr |
2420
| more.rs:65:9:65:17 | f_ptr | Variable $@ is assigned a value that is never used. | more.rs:65:13:65:17 | f_ptr | f_ptr |

rust/ql/test/query-tests/unusedentities/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -491,12 +491,12 @@ fn references() {
491491

492492
pub struct my_declaration {
493493
field1: fn(i32) -> i32,
494-
field2: fn(x: i32) -> i32, // $ SPURIOUS: Alert[rust/unused-value]
495-
field3: fn(y: // $ SPURIOUS: Alert[rust/unused-value]
496-
fn(z: i32) -> i32) -> i32, // $ SPURIOUS: Alert[rust/unused-value]
494+
field2: fn(x: i32) -> i32,
495+
field3: fn(y:
496+
fn(z: i32) -> i32) -> i32,
497497
}
498498

499-
type MyType = fn(x: i32) -> i32; // $ SPURIOUS: Alert[rust/unused-value]
499+
type MyType = fn(x: i32) -> i32;
500500

501501
trait MyTrait {
502502
fn my_func2(&self, x: i32) -> i32;

0 commit comments

Comments
 (0)