Skip to content

Commit c7a4f38

Browse files
committed
Lint dead code in closures
1 parent bda32a4 commit c7a4f38

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

compiler/rustc_passes/src/dead.rs

+3
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,9 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> {
395395
self.mark_as_used_if_union(*adt, fields);
396396
}
397397
}
398+
hir::ExprKind::Closure(cls) => {
399+
self.insert_def_id(cls.def_id.to_def_id());
400+
}
398401
_ => (),
399402
}
400403

tests/ui/lint/dead-code/in-closure.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// edition: 2021
2+
3+
#![deny(dead_code)]
4+
5+
pub fn foo() {
6+
let closure = || {
7+
fn a() {} //~ ERROR function `a` is never used
8+
};
9+
closure()
10+
}
11+
12+
pub async fn async_foo() {
13+
const A: usize = 1; //~ ERROR constant `A` is never used
14+
}
15+
16+
fn main() {}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: function `a` is never used
2+
--> $DIR/in-closure.rs:7:12
3+
|
4+
LL | fn a() {}
5+
| ^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/in-closure.rs:3:9
9+
|
10+
LL | #![deny(dead_code)]
11+
| ^^^^^^^^^
12+
13+
error: constant `A` is never used
14+
--> $DIR/in-closure.rs:13:11
15+
|
16+
LL | const A: usize = 1;
17+
| ^
18+
19+
error: aborting due to 2 previous errors
20+

0 commit comments

Comments
 (0)