Rust: Fix unused variable FPs due to unexpanded macro calls.

This commit is contained in:
Geoffrey White
2025-03-07 14:17:36 +00:00
parent b2e3352aa8
commit e0839a369c
3 changed files with 17 additions and 3 deletions

View File

@@ -11,9 +11,24 @@
import rust
import UnusedVariable
/**
* A callable for which we have incomplete information, for example because an unexpanded
* macro call is present. These callables are prone to false positive results from unused
* entities queries, unless they are excluded from results.
*/
class IncompleteCallable extends Callable {
IncompleteCallable() {
exists(MacroExpr me |
me.getEnclosingCallable() = this and
not me.getMacroCall().hasExpanded()
)
}
}
from Variable v
where
isUnused(v) and
not isAllowableUnused(v) and
not v instanceof DiscardVariable
not v instanceof DiscardVariable and
not v.getEnclosingCfgScope() instanceof IncompleteCallable
select v, "Variable '" + v + "' is not used."

View File

@@ -19,5 +19,4 @@
| main.rs:431:26:431:28 | val | Variable 'val' is not used. |
| main.rs:434:21:434:23 | acc | Variable 'acc' is not used. |
| main.rs:455:9:455:14 | unused | Variable 'unused' is not used. |
| main.rs:521:12:521:12 | n | Variable 'n' is not used. |
| more.rs:24:9:24:11 | val | Variable 'val' is not used. |

View File

@@ -518,7 +518,7 @@ fn macros4() {
fn macros5() {
match std::env::args().nth(1).unwrap().parse::<u16>() {
Ok(n) => { // $ SPURIOUS: Alert[rust/unused-variable]
Ok(n) => {
undefined_macro_call!(n);
}
_ => {}