Skip to content

Commit 01421e0

Browse files
committed
Auto merge of rust-lang#96863 - SparrowLii:let, r=michaelwoerister
use `hir::Let` in `hir::Guard::IfLet` This PR fixes the FIXME about using `hir::Let` in `hir::Guard::IfLet`
2 parents acc822a + 47e9afa commit 01421e0

File tree

5 files changed

+14
-12
lines changed

5 files changed

+14
-12
lines changed

clippy_lints/src/collapsible_match.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use clippy_utils::{is_lang_ctor, is_unit_expr, path_to_local, peel_blocks_with_s
55
use if_chain::if_chain;
66
use rustc_errors::MultiSpan;
77
use rustc_hir::LangItem::OptionNone;
8-
use rustc_hir::{Arm, Expr, Guard, HirId, Pat, PatKind};
8+
use rustc_hir::{Arm, Expr, Guard, HirId, Let, Pat, PatKind};
99
use rustc_lint::{LateContext, LateLintPass};
1010
use rustc_session::{declare_lint_pass, declare_tool_lint};
1111
use rustc_span::Span;
@@ -109,7 +109,7 @@ fn check_arm<'tcx>(
109109
(Some(a), Some(b)) => SpanlessEq::new(cx).eq_expr(a, b),
110110
};
111111
// the binding must not be used in the if guard
112-
if outer_guard.map_or(true, |(Guard::If(e) | Guard::IfLet(_, e))| !is_local_used(cx, *e, binding_id));
112+
if outer_guard.map_or(true, |(Guard::If(e) | Guard::IfLet(Let { init: e, .. }))| !is_local_used(cx, *e, binding_id));
113113
// ...or anywhere in the inner expression
114114
if match inner {
115115
IfLetOrMatch::IfLet(_, _, body, els) => {

clippy_lints/src/entry.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_errors::Applicability;
1111
use rustc_hir::{
1212
hir_id::HirIdSet,
1313
intravisit::{walk_expr, Visitor},
14-
Block, Expr, ExprKind, Guard, HirId, Pat, Stmt, StmtKind, UnOp,
14+
Block, Expr, ExprKind, Guard, HirId, Let, Pat, Stmt, StmtKind, UnOp,
1515
};
1616
use rustc_lint::{LateContext, LateLintPass};
1717
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -478,7 +478,7 @@ impl<'tcx> Visitor<'tcx> for InsertSearcher<'_, 'tcx> {
478478
let mut is_map_used = self.is_map_used;
479479
for arm in arms {
480480
self.visit_pat(arm.pat);
481-
if let Some(Guard::If(guard) | Guard::IfLet(_, guard)) = arm.guard {
481+
if let Some(Guard::If(guard) | Guard::IfLet(&Let { init: guard, .. })) = arm.guard {
482482
self.visit_non_tail_expr(guard);
483483
}
484484
is_map_used |= self.visit_cond_arm(arm.body);

clippy_lints/src/only_used_in_recursion.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ impl<'tcx> SideEffectVisit<'tcx> {
596596
let mut vars = std::mem::take(&mut self.ret_vars);
597597
let _ = arm.guard.as_ref().map(|guard| {
598598
self.visit_expr(match guard {
599-
Guard::If(expr) | Guard::IfLet(_, expr) => expr,
599+
Guard::If(expr) | Guard::IfLet(Let { init: expr, .. }) => expr,
600600
});
601601
vars.append(&mut self.ret_vars);
602602
});

clippy_lints/src/utils/author.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -315,11 +315,11 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
315315
out!("if let Some(Guard::If({expr})) = {arm}.guard;");
316316
self.expr(expr);
317317
},
318-
Some(hir::Guard::IfLet(pat, expr)) => {
319-
bind!(self, pat, expr);
320-
out!("if let Some(Guard::IfLet({pat}, {expr}) = {arm}.guard;");
321-
self.pat(pat);
322-
self.expr(expr);
318+
Some(hir::Guard::IfLet(let_expr)) => {
319+
bind!(self, let_expr);
320+
out!("if let Some(Guard::IfLet({let_expr}) = {arm}.guard;");
321+
self.pat(field!(let_expr.pat));
322+
self.expr(field!(let_expr.init));
323323
},
324324
}
325325
self.expr(field!(arm.body));

clippy_utils/src/hir_utils.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,9 @@ impl HirEqInterExpr<'_, '_, '_> {
301301
fn eq_guard(&mut self, left: &Guard<'_>, right: &Guard<'_>) -> bool {
302302
match (left, right) {
303303
(Guard::If(l), Guard::If(r)) => self.eq_expr(l, r),
304-
(Guard::IfLet(lp, le), Guard::IfLet(rp, re)) => self.eq_pat(lp, rp) && self.eq_expr(le, re),
304+
(Guard::IfLet(l), Guard::IfLet(r)) => {
305+
self.eq_pat(l.pat, r.pat) && both(&l.ty, &r.ty, |l, r| self.eq_ty(l, r)) && self.eq_expr(l.init, r.init)
306+
},
305307
_ => false,
306308
}
307309
}
@@ -894,7 +896,7 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
894896

895897
pub fn hash_guard(&mut self, g: &Guard<'_>) {
896898
match g {
897-
Guard::If(expr) | Guard::IfLet(_, expr) => {
899+
Guard::If(expr) | Guard::IfLet(Let { init: expr, .. }) => {
898900
self.hash_expr(expr);
899901
},
900902
}

0 commit comments

Comments
 (0)