Rollup merge of #108315 - clubby789:dead-code-in-closure, r=compiler-errors

Lint dead code in closures and generators

Fixes #108296

I think this might be a potentially breaking change, but restores the behaviour of pre-1.64.

`@rustbot` label +A-lint
This commit is contained in:
Matthias Krüger 2023-02-22 20:05:58 +01:00 committed by GitHub
commit 783617b5e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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