Skip to content

Commit e0839a3

Browse files
committed
Rust: Fix unused variable FPs due to unexpanded macro calls.
1 parent b2e3352 commit e0839a3

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,24 @@
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+
1428
from Variable v
1529
where
1630
isUnused(v) and
1731
not isAllowableUnused(v) and
18-
not v instanceof DiscardVariable
32+
not v instanceof DiscardVariable and
33+
not v.getEnclosingCfgScope() instanceof IncompleteCallable
1934
select v, "Variable '" + v + "' is not used."

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,4 @@
1919
| main.rs:431:26:431:28 | val | Variable 'val' is not used. |
2020
| main.rs:434:21:434:23 | acc | Variable 'acc' is not used. |
2121
| main.rs:455:9:455:14 | unused | Variable 'unused' is not used. |
22-
| main.rs:521:12:521:12 | n | Variable 'n' is not used. |
2322
| more.rs:24:9:24:11 | val | Variable 'val' is not used. |

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ fn macros4() {
518518

519519
fn macros5() {
520520
match std::env::args().nth(1).unwrap().parse::<u16>() {
521-
Ok(n) => { // $ SPURIOUS: Alert[rust/unused-variable]
521+
Ok(n) => {
522522
undefined_macro_call!(n);
523523
}
524524
_ => {}

0 commit comments

Comments
 (0)