From ab60a7df6446bf175b9717ff4e96288287393c02 Mon Sep 17 00:00:00 2001 From: est31 Date: Fri, 29 Dec 2023 02:52:11 +0100 Subject: [PATCH] Also walk bindings created by if-let guards --- compiler/rustc_passes/src/liveness.rs | 3 +++ tests/ui/lint/unused/issue-119383.rs | 9 +++++++++ tests/ui/lint/unused/issue-119383.stderr | 14 ++++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 tests/ui/lint/unused/issue-119383.rs create mode 100644 tests/ui/lint/unused/issue-119383.stderr diff --git a/compiler/rustc_passes/src/liveness.rs b/compiler/rustc_passes/src/liveness.rs index 6be01e0d88fca..832c0c6b3174f 100644 --- a/compiler/rustc_passes/src/liveness.rs +++ b/compiler/rustc_passes/src/liveness.rs @@ -1351,6 +1351,9 @@ impl<'a, 'tcx> Visitor<'tcx> for Liveness<'a, 'tcx> { fn visit_arm(&mut self, arm: &'tcx hir::Arm<'tcx>) { self.check_unused_vars_in_pat(arm.pat, None, None, |_, _, _, _| {}); + if let Some(hir::Guard::IfLet(let_expr)) = arm.guard { + self.check_unused_vars_in_pat(let_expr.pat, None, None, |_, _, _, _| {}); + } intravisit::walk_arm(self, arm); } } diff --git a/tests/ui/lint/unused/issue-119383.rs b/tests/ui/lint/unused/issue-119383.rs new file mode 100644 index 0000000000000..71197444f45c3 --- /dev/null +++ b/tests/ui/lint/unused/issue-119383.rs @@ -0,0 +1,9 @@ +#![feature(if_let_guard)] +#![deny(unused_variables)] + +fn main() { + match () { + () if let Some(b) = Some(()) => {} //~ ERROR unused variable: `b` + _ => {} + } +} diff --git a/tests/ui/lint/unused/issue-119383.stderr b/tests/ui/lint/unused/issue-119383.stderr new file mode 100644 index 0000000000000..6b4d14b95d55a --- /dev/null +++ b/tests/ui/lint/unused/issue-119383.stderr @@ -0,0 +1,14 @@ +error: unused variable: `b` + --> $DIR/issue-119383.rs:6:24 + | +LL | () if let Some(b) = Some(()) => {} + | ^ help: if this is intentional, prefix it with an underscore: `_b` + | +note: the lint level is defined here + --> $DIR/issue-119383.rs:2:9 + | +LL | #![deny(unused_variables)] + | ^^^^^^^^^^^^^^^^ + +error: aborting due to 1 previous error +