Skip to content

Commit cc902a6

Browse files
committed
Rust: Fix unused value FPs due to unexpanded macro calls as well.
1 parent e0839a3 commit cc902a6

File tree

5 files changed

+21
-19
lines changed

5 files changed

+21
-19
lines changed

rust/ql/src/queries/unusedentities/UnusedValue.ql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ where
2121
// avoid overlap with the unused variable query
2222
not isUnused(v) and
2323
not v instanceof DiscardVariable and
24-
not write.isInMacroExpansion()
24+
not write.isInMacroExpansion() and
25+
not isAllowableUnused(v)
2526
select write, "Variable $@ is assigned a value that is never used.", v, v.getText()

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

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,9 @@
1111
import rust
1212
import UnusedVariable
1313

14-
/**
15-
* A callable for which we have incomplete information, for example because an unexpanded
16-
* macro call is present. These callables are prone to false positive results from unused
17-
* entities queries, unless they are excluded from results.
18-
*/
19-
class IncompleteCallable extends Callable {
20-
IncompleteCallable() {
21-
exists(MacroExpr me |
22-
me.getEnclosingCallable() = this and
23-
not me.getMacroCall().hasExpanded()
24-
)
25-
}
26-
}
27-
2814
from Variable v
2915
where
3016
isUnused(v) and
3117
not isAllowableUnused(v) and
32-
not v instanceof DiscardVariable and
33-
not v.getEnclosingCfgScope() instanceof IncompleteCallable
18+
not v instanceof DiscardVariable
3419
select v, "Variable '" + v + "' is not used."

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ predicate isUnused(Variable v) {
1616
not exists(v.getInitializer())
1717
}
1818

19+
/**
20+
* A callable for which we have incomplete information, for example because an unexpanded
21+
* macro call is present. These callables are prone to false positive results from unused
22+
* entities queries, unless they are excluded from results.
23+
*/
24+
class IncompleteCallable extends Callable {
25+
IncompleteCallable() {
26+
exists(MacroExpr me |
27+
me.getEnclosingCallable() = this and
28+
not me.getMacroCall().hasExpanded()
29+
)
30+
}
31+
}
32+
1933
/**
2034
* Holds if variable `v` is in a context where we may not find a use for it,
2135
* but that's expected and should not be considered a problem.
@@ -24,6 +38,9 @@ predicate isAllowableUnused(Variable v) {
2438
// in a macro expansion
2539
v.getPat().isInMacroExpansion()
2640
or
41+
// declared in an incomplete callable
42+
v.getEnclosingCfgScope() instanceof IncompleteCallable
43+
or
2744
// a 'self' variable
2845
v.getText() = "self"
2946
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
| main.rs:284:13:284:17 | total | Variable $@ is assigned a value that is never used. | main.rs:252:13:252:17 | total | total |
1515
| main.rs:377:9:377:9 | x | Variable $@ is assigned a value that is never used. | main.rs:377:9:377:9 | x | x |
1616
| main.rs:385:17:385:17 | x | Variable $@ is assigned a value that is never used. | main.rs:385:17:385:17 | x | x |
17-
| main.rs:510:9:510:9 | d | Variable $@ is assigned a value that is never used. | main.rs:510:9:510:9 | d | d |
1817
| main.rs:550:9:550:20 | var_in_macro | Variable $@ is assigned a value that is never used. | main.rs:550:9:550:20 | var_in_macro | var_in_macro |
1918
| main.rs:559:9:559:9 | c | Variable $@ is assigned a value that is never used. | main.rs:559:9:559:9 | c | c |
2019
| 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 |

rust/ql/test/query-tests/unusedentities/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ fn macros2() {
507507
}
508508

509509
fn macros3() {
510-
let d: u16 = 4; // $ SPURIOUS: Alert[rust/unused-value]
510+
let d: u16 = 4;
511511

512512
undefined_macro_call!(d);
513513
}

0 commit comments

Comments
 (0)