Skip to content

Rust: Fix rust/unused-variable FPs #17913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Nov 8, 2024
Merged
6 changes: 5 additions & 1 deletion rust/ql/src/queries/unusedentities/UnusedVariable.qll
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ class DiscardVariable extends Variable {

/** Holds if variable `v` is unused. */
predicate isUnused(Variable v) {
// variable is accessed or initialized
not exists(v.getAnAccess()) and
not exists(v.getInitializer()) and
// variable is intentionally unused
not v instanceof DiscardVariable and
not v.getPat().isInMacroExpansion()
// variable is in a context where is may not have a use
not v.getPat().isInMacroExpansion() and
not exists(FnPtrType fp | fp.getParamList().getParam(_).getPat() = v.getPat())
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
| main.rs:373:9:373:9 | x | Variable $@ is assigned a value that is never used. | main.rs:373:9:373:9 | x | x |
| main.rs:381:17:381:17 | x | Variable $@ is assigned a value that is never used. | main.rs:381:17:381:17 | x | x |
| main.rs:482:9:482:9 | c | Variable $@ is assigned a value that is never used. | main.rs:482:9:482:9 | c | c |
| main.rs:494:16:494:16 | x | Variable $@ is assigned a value that is never used. | main.rs:494:16:494:16 | x | x |
| main.rs:495:16:495:16 | y | Variable $@ is assigned a value that is never used. | main.rs:495:16:495:16 | y | y |
| main.rs:496:12:496:12 | z | Variable $@ is assigned a value that is never used. | main.rs:496:12:496:12 | z | z |
| main.rs:499:18:499:18 | x | Variable $@ is assigned a value that is never used. | main.rs:499:18:499:18 | x | x |
| 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 |
| 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 |
| 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 |
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,4 @@
| main.rs:427:26:427:28 | val | Variable 'val' is not used. |
| main.rs:430:21:430:23 | acc | Variable 'acc' is not used. |
| main.rs:451:9:451:14 | unused | Variable 'unused' is not used. |
| main.rs:494:16:494:16 | x | Variable 'x' is not used. |
| main.rs:495:16:495:16 | y | Variable 'y' is not used. |
| main.rs:496:12:496:12 | z | Variable 'z' is not used. |
| main.rs:499:18:499:18 | x | Variable 'x' is not used. |
| more.rs:24:9:24:11 | val | Variable 'val' is not used. |
8 changes: 4 additions & 4 deletions rust/ql/test/query-tests/unusedentities/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,12 +491,12 @@ fn references() {

pub struct my_declaration {
field1: fn(i32) -> i32,
field2: fn(x: i32) -> i32, // $ SPURIOUS: Alert[rust/unused-variable]
field3: fn(y: // $ SPURIOUS: Alert[rust/unused-variable]
fn(z: i32) -> i32) -> i32, // $ SPURIOUS: Alert[rust/unused-variable]
field2: fn(x: i32) -> i32, // $ SPURIOUS: Alert[rust/unused-value]
field3: fn(y: // $ SPURIOUS: Alert[rust/unused-value]
fn(z: i32) -> i32) -> i32, // $ SPURIOUS: Alert[rust/unused-value]
}

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

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