Skip to content

Commit 278760c

Browse files
committed
Rust: Add another exception to rust/unused-variable.
1 parent 35ffd0c commit 278760c

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ class DiscardVariable extends Variable {
77

88
/** Holds if variable `v` is unused. */
99
predicate isUnused(Variable v) {
10+
// variable is accessed or initialized
1011
not exists(v.getAnAccess()) and
1112
not exists(v.getInitializer()) and
13+
// variable is intentionally unused
1214
not v instanceof DiscardVariable and
13-
not v.getPat().isInMacroExpansion()
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())
1418
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
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 |
1822
| 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 |
1923
| 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 |
2024
| 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/UnusedVariable.expected

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,4 @@
1919
| main.rs:427:26:427:28 | val | Variable 'val' is not used. |
2020
| main.rs:430:21:430:23 | acc | Variable 'acc' is not used. |
2121
| main.rs:451:9:451:14 | unused | Variable 'unused' is not used. |
22-
| main.rs:494:16:494:16 | x | Variable 'x' is not used. |
23-
| main.rs:495:16:495:16 | y | Variable 'y' is not used. |
24-
| main.rs:496:12:496:12 | z | Variable 'z' is not used. |
25-
| main.rs:499:18:499:18 | x | Variable 'x' is not used. |
2622
| more.rs:24:9:24:11 | val | Variable 'val' is not used. |

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-variable]
495-
field3: fn(y: // $ SPURIOUS: Alert[rust/unused-variable]
496-
fn(z: i32) -> i32) -> i32, // $ SPURIOUS: Alert[rust/unused-variable]
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]
497497
}
498498

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

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

0 commit comments

Comments
 (0)