Fix missing unused_variables lint when using a match guard

Within a binding pattern match guard, only real reads of a bound local
impact its liveness analysis - not the fake read that is injected.
This commit is contained in:
Alan Egerton 2026-02-02 09:38:07 +00:00
parent a60d12cbcc
commit 5aba6b1635
No known key found for this signature in database
GPG key ID: 3D7EA7527916B438
4 changed files with 32 additions and 4 deletions

View file

@ -1243,9 +1243,12 @@ struct TransferFunction<'a, 'tcx> {
impl<'tcx> Visitor<'tcx> for TransferFunction<'_, 'tcx> {
fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
match statement.kind {
// `ForLet(None)` fake read erroneously marks the just-assigned local as live.
// This defeats the purpose of the analysis for `let` bindings.
StatementKind::FakeRead(box (FakeReadCause::ForLet(None), _)) => return,
// `ForLet(None)` and `ForGuardBinding` fake reads erroneously mark the just-assigned
// locals as live. This defeats the purpose of the analysis for such bindings.
StatementKind::FakeRead(box (
FakeReadCause::ForLet(None) | FakeReadCause::ForGuardBinding,
_,
)) => return,
// Handle self-assignment by restricting the read/write they do.
StatementKind::Assign(box (ref dest, ref rvalue))
if self.self_assignment.contains(&location) =>

View file

@ -85,7 +85,7 @@ impl LateLintPass<'_> for UncheckedTimeSubtraction {
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ Expr<'_>) {
let (lhs, rhs) = match expr.kind {
ExprKind::Binary(op, lhs, rhs) if matches!(op.node, BinOpKind::Sub,) => (lhs, rhs),
ExprKind::MethodCall(fn_name, lhs, [rhs], _) if cx.ty_based_def(expr).is_diag_item(cx, sym::sub) => {
ExprKind::MethodCall(_, lhs, [rhs], _) if cx.ty_based_def(expr).is_diag_item(cx, sym::sub) => {
(lhs, rhs)
},
_ => return,

View file

@ -0,0 +1,10 @@
//! The mere presence of a match guard should not deem bound variables "used".
//! Regression test for https://github.com/rust-lang/rust/issues/151983
//@ check-pass
#![warn(unused)]
fn main() {
match Some(42) {
Some(unused) if true => (), //~WARN unused variable: `unused`
_ => (),
}
}

View file

@ -0,0 +1,15 @@
warning: unused variable: `unused`
--> $DIR/match_with_guard.rs:7:14
|
LL | Some(unused) if true => (),
| ^^^^^^ help: if this is intentional, prefix it with an underscore: `_unused`
|
note: the lint level is defined here
--> $DIR/match_with_guard.rs:4:9
|
LL | #![warn(unused)]
| ^^^^^^
= note: `#[warn(unused_variables)]` implied by `#[warn(unused)]`
warning: 1 warning emitted