File tree Expand file tree Collapse file tree 7 files changed +27
-18
lines changed
lib/codeql/rust/elements/internal
src/queries/unusedentities
test/query-tests/unusedentities Expand file tree Collapse file tree 7 files changed +27
-18
lines changed Original file line number Diff line number Diff line change @@ -26,21 +26,29 @@ module Impl {
26
26
result = getImmediateParent ( e )
27
27
}
28
28
29
- /** Gets the nearest enclosing parent of `ast` that is an `AstNode`. */
30
- private AstNode getParentOfAst ( AstNode ast ) {
31
- result = getParentOfAstStep * ( getImmediateParent ( ast ) )
32
- }
33
-
34
29
class AstNode extends Generated:: AstNode {
30
+ /**
31
+ * Gets the nearest enclosing parent of this node, which is also an `AstNode`,
32
+ * if any.
33
+ */
34
+ AstNode getParentNode ( ) { result = getParentOfAstStep * ( getImmediateParent ( this ) ) }
35
+
35
36
/** Gets the immediately enclosing callable of this node, if any. */
36
37
cached
37
38
Callable getEnclosingCallable ( ) {
38
- exists ( AstNode p | p = getParentOfAst ( this ) |
39
+ exists ( AstNode p | p = this . getParentNode ( ) |
39
40
result = p
40
41
or
41
42
not p instanceof Callable and
42
43
result = p .getEnclosingCallable ( )
43
44
)
44
45
}
46
+
47
+ /** Holds if this node is inside a macro expansion. */
48
+ predicate isInMacroExpansion ( ) {
49
+ this = any ( MacroCall mc ) .getExpanded ( )
50
+ or
51
+ this .getParentNode ( ) .isInMacroExpansion ( )
52
+ }
45
53
}
46
54
}
Original file line number Diff line number Diff line change @@ -29,7 +29,10 @@ predicate hiddenNode(AstNode n) {
29
29
not succ ( n , _) and
30
30
not succ ( _, n )
31
31
or
32
- n instanceof ControlFlowGraphImpl:: PostOrderTree // location is counter-intuitive
32
+ n instanceof ControlFlowGraphImpl:: PostOrderTree and // location is counter-intuitive
33
+ not n instanceof MacroExpr
34
+ or
35
+ n .isInMacroExpansion ( )
33
36
}
34
37
35
38
/**
Original file line number Diff line number Diff line change 20
20
not write = any ( Ssa:: WriteDefinition def ) .getWriteAccess ( ) .getAstNode ( ) and
21
21
// avoid overlap with the unused variable query
22
22
not isUnused ( v ) and
23
- not v instanceof DiscardVariable
23
+ not v instanceof DiscardVariable and
24
+ not write .isInMacroExpansion ( )
24
25
select write , "Variable '" + v + "' is assigned a value that is never used."
Original file line number Diff line number Diff line change @@ -10,5 +10,6 @@ predicate isUnused(Variable v) {
10
10
not exists ( v .getAnAccess ( ) ) and
11
11
not exists ( v .getInitializer ( ) ) and
12
12
not v instanceof DiscardVariable and
13
+ not v .getPat ( ) .isInMacroExpansion ( ) and
13
14
exists ( File f | f .getBaseName ( ) = "main.rs" | v .getLocation ( ) .getFile ( ) = f ) // temporarily severely limit results
14
15
}
Original file line number Diff line number Diff line change 3
3
| unreachable.rs:31:9:31:23 | ExprStmt | This code is never reached. |
4
4
| unreachable.rs:38:9:38:23 | ExprStmt | This code is never reached. |
5
5
| unreachable.rs:59:5:59:19 | ExprStmt | This code is never reached. |
6
- | unreachable.rs:106:13:106:20 | ExprStmt | This code is never reached. |
7
- | unreachable.rs:115:13:115:20 | ExprStmt | This code is never reached. |
6
+ | unreachable.rs:106:13:106:20 | MacroExpr | This code is never reached. |
7
+ | unreachable.rs:115:13:115:20 | MacroExpr | This code is never reached. |
8
8
| unreachable.rs:141:5:141:19 | ExprStmt | This code is never reached. |
9
9
| unreachable.rs:148:9:148:23 | ExprStmt | This code is never reached. |
10
10
| unreachable.rs:157:13:157:27 | ExprStmt | This code is never reached. |
Original file line number Diff line number Diff line change 11
11
| main.rs:87:9:87:9 | a | Variable 'a' is assigned a value that is never used. |
12
12
| main.rs:108:9:108:10 | is | Variable 'is' is assigned a value that is never used. |
13
13
| main.rs:131:13:131:17 | total | Variable 'total' is assigned a value that is never used. |
14
- | main.rs:194:13:194:31 | res | Variable 'res' is assigned a value that is never used. |
15
- | main.rs:206:9:206:24 | kind | Variable 'kind' is assigned a value that is never used. |
16
- | main.rs:210:9:210:32 | kind | Variable 'kind' is assigned a value that is never used. |
17
14
| main.rs:266:13:266:17 | total | Variable 'total' is assigned a value that is never used. |
18
- | main.rs:334:5:334:39 | kind | Variable 'kind' is assigned a value that is never used. |
19
15
| main.rs:359:9:359:9 | x | Variable 'x' is assigned a value that is never used. |
20
16
| main.rs:367:17:367:17 | x | Variable 'x' is assigned a value that is never used. |
21
17
| more.rs:24:9:24:11 | val | Variable 'val' is assigned a value that is never used. |
Original file line number Diff line number Diff line change @@ -191,7 +191,7 @@ fn loops() {
191
191
}
192
192
193
193
for x in 1 ..10 {
194
- _ = format ! ( "x is {x}" ) ; // $ SPURIOUS: Alert[rust/unused-value]
194
+ _ = format ! ( "x is {x}" ) ;
195
195
}
196
196
197
197
for x in 1 ..10 {
@@ -203,11 +203,11 @@ fn loops() {
203
203
}
204
204
205
205
for x in 1 ..10 {
206
- assert_eq ! ( x, 1 ) ; // $ SPURIOUS: Alert[rust/unused-value]
206
+ assert_eq ! ( x, 1 ) ;
207
207
}
208
208
209
209
for x in 1 ..10 {
210
- assert_eq ! ( id( x) , id( 1 ) ) ; // $ SPURIOUS: Alert[rust/unused-value]
210
+ assert_eq ! ( id( x) , id( 1 ) ) ;
211
211
}
212
212
}
213
213
@@ -331,7 +331,7 @@ fn if_lets_matches() {
331
331
}
332
332
333
333
let duration1 = std:: time:: Duration :: new ( 10 , 0 ) ; // ten seconds
334
- assert_eq ! ( duration1. as_secs( ) , 10 ) ; // $ SPURIOUS: Alert[rust/unused-value]
334
+ assert_eq ! ( duration1. as_secs( ) , 10 ) ;
335
335
336
336
let duration2: Result < std:: time:: Duration , String > = Ok ( std:: time:: Duration :: new ( 10 , 0 ) ) ;
337
337
match duration2 {
You can’t perform that action at this time.
0 commit comments