File tree Expand file tree Collapse file tree 4 files changed +26
-17
lines changed
src/queries/unusedentities
test/query-tests/unusedentities Expand file tree Collapse file tree 4 files changed +26
-17
lines changed Original file line number Diff line number Diff line change @@ -12,5 +12,8 @@ import rust
1212import UnusedVariable
1313
1414from Variable v
15- where isUnused ( v )
15+ where
16+ isUnused ( v ) and
17+ not isAllowableUnused ( v ) and
18+ not v instanceof DiscardVariable
1619select v , "Variable '" + v + "' is not used."
Original file line number Diff line number Diff line change 11import rust
22
3- /** A deliberately unused variable. */
3+ /**
4+ * A deliberately unused variable, for example `_` or `_x`.
5+ */
46class 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+ */
913predicate 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}
Original file line number Diff line number Diff line change 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 |
Original file line number Diff line number Diff line change @@ -491,12 +491,12 @@ fn references() {
491491
492492pub 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
501501trait MyTrait {
502502 fn my_func2 ( & self , x : i32 ) -> i32 ;
You can’t perform that action at this time.
0 commit comments