Skip to content

Commit 6c51bd1

Browse files
authored
Merge pull request #22398 from hvitved/rust/unused-variable-todo
Rust: Exclude 'unused variable' results for functions containing `todo!()` or `unimplemented!()`
2 parents 3290afc + bbff992 commit 6c51bd1

3 files changed

Lines changed: 19 additions & 2 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The `rust/unused-variable` query no longer reports variables in functions containing the standard `todo!()` or `unimplemented!()` macros.

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import rust
2+
private import codeql.rust.internal.PathResolution
23

34
/**
45
* A deliberately unused variable, for example `_` or `_x`.
@@ -23,9 +24,13 @@ predicate isUnused(Variable v) {
2324
*/
2425
class IncompleteCallable extends Callable {
2526
IncompleteCallable() {
26-
exists(MacroExpr me |
27-
me.getEnclosingCallable() = this and
27+
exists(MacroExpr me | me.getEnclosingCallable() = this |
2828
not me.getMacroCall().hasMacroCallExpansion()
29+
or
30+
exists(ItemNode i |
31+
i.getCanonicalPath(_) = ["core::macros::unimplemented", "core::macros::todo"] and
32+
me.getMacroCall().resolveMacro() = i
33+
)
2934
)
3035
}
3136
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,14 @@ trait MyTrait {
558558
fn my_func2(&self, x: i32) -> i32;
559559
}
560560

561+
fn unimplemented(x : i32) {
562+
unimplemented!()
563+
}
564+
565+
fn todo(x : i32) {
566+
todo!()
567+
}
568+
561569
// --- main ---
562570

563571
fn main() {

0 commit comments

Comments
 (0)