Lint dead code in closures

This commit is contained in:
clubby789 2023-02-21 16:11:48 +00:00
parent bda32a4023
commit c7a4f387fd
3 changed files with 39 additions and 0 deletions

View File

@ -395,6 +395,9 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> {
self.mark_as_used_if_union(*adt, fields);
}
}
hir::ExprKind::Closure(cls) => {
self.insert_def_id(cls.def_id.to_def_id());
}
_ => (),
}

View File

@ -0,0 +1,16 @@
// edition: 2021
#![deny(dead_code)]
pub fn foo() {
let closure = || {
fn a() {} //~ ERROR function `a` is never used
};
closure()
}
pub async fn async_foo() {
const A: usize = 1; //~ ERROR constant `A` is never used
}
fn main() {}

View File

@ -0,0 +1,20 @@
error: function `a` is never used
--> $DIR/in-closure.rs:7:12
|
LL | fn a() {}
| ^
|
note: the lint level is defined here
--> $DIR/in-closure.rs:3:9
|
LL | #![deny(dead_code)]
| ^^^^^^^^^
error: constant `A` is never used
--> $DIR/in-closure.rs:13:11
|
LL | const A: usize = 1;
| ^
error: aborting due to 2 previous errors