Skip to content

Commit

Permalink
Lint dead code in closures
Browse files Browse the repository at this point in the history
  • Loading branch information
clubby789 committed Feb 22, 2023
1 parent bda32a4 commit c7a4f38
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions compiler/rustc_passes/src/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
_ => (),
}

Expand Down
16 changes: 16 additions & 0 deletions tests/ui/lint/dead-code/in-closure.rs
Original file line number Diff line number Diff line change
@@ -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() {}
20 changes: 20 additions & 0 deletions tests/ui/lint/dead-code/in-closure.stderr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c7a4f38

Please sign in to comment.