Rollup merge of #151990 - eggyal:unused-in-match-with-guard, r=nnethercote
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. Fixes rust-lang/rust#151983 r? compiler
This commit is contained in:
commit
b73b8223d8
4 changed files with 32 additions and 4 deletions
|
|
@ -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) =>
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
10
tests/ui/lint/unused/match_with_guard.rs
Normal file
10
tests/ui/lint/unused/match_with_guard.rs
Normal 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`
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
15
tests/ui/lint/unused/match_with_guard.stderr
Normal file
15
tests/ui/lint/unused/match_with_guard.stderr
Normal 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
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue