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
12
12
import UnusedVariable
13
13
14
14
from Variable v
15
- where isUnused ( v )
15
+ where
16
+ isUnused ( v ) and
17
+ not isAllowableUnused ( v ) and
18
+ not v instanceof DiscardVariable
16
19
select v , "Variable '" + v + "' is not used."
Original file line number Diff line number Diff line change 1
1
import rust
2
2
3
- /** A deliberately unused variable. */
3
+ /**
4
+ * A deliberately unused variable, for example `_` or `_x`.
5
+ */
4
6
class DiscardVariable extends Variable {
5
7
DiscardVariable ( ) { this .getName ( ) .charAt ( 0 ) = "_" }
6
8
}
7
9
8
- /** Holds if variable `v` is unused. */
10
+ /**
11
+ * Holds if variable `v` is unused.
12
+ */
9
13
predicate isUnused ( Variable v ) {
10
14
// variable is accessed or initialized
11
15
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 ( ) )
18
28
}
Original file line number Diff line number Diff line change 15
15
| main.rs:373:9:373:9 | x | Variable $@ is assigned a value that is never used. | main.rs:373:9:373:9 | x | x |
16
16
| main.rs:381:17:381:17 | x | Variable $@ is assigned a value that is never used. | main.rs:381:17:381:17 | x | x |
17
17
| 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 |
22
18
| 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 |
23
19
| 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 |
24
20
| 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() {
491
491
492
492
pub struct my_declaration {
493
493
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 ,
497
497
}
498
498
499
- type MyType = fn ( x : i32 ) -> i32 ; // $ SPURIOUS: Alert[rust/unused-value]
499
+ type MyType = fn ( x : i32 ) -> i32 ;
500
500
501
501
trait MyTrait {
502
502
fn my_func2 ( & self , x : i32 ) -> i32 ;
You can’t perform that action at this time.
0 commit comments