Skip to content

Commit 61e69bc

Browse files
Handle Guard::IfLet in clippy
1 parent f3d4aa6 commit 61e69bc

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

src/tools/clippy/clippy_lints/src/shadow.rs

+4
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,10 @@ fn check_expr<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, bindings: &mut
342342
if let Some(ref guard) = arm.guard {
343343
match guard {
344344
Guard::If(if_expr) => check_expr(cx, if_expr, bindings),
345+
Guard::IfLet(guard_pat, guard_expr) => {
346+
check_pat(cx, guard_pat, Some(*guard_expr), guard_pat.span, bindings);
347+
check_expr(cx, guard_expr, bindings);
348+
},
345349
}
346350
}
347351
check_expr(cx, &arm.body, bindings);

src/tools/clippy/clippy_lints/src/utils/author.rs

+12
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,18 @@ impl<'tcx> Visitor<'tcx> for PrintVisitor {
372372
self.current = if_expr_pat;
373373
self.visit_expr(if_expr);
374374
},
375+
hir::Guard::IfLet(ref if_let_pat, ref if_let_expr) => {
376+
let if_let_pat_pat = self.next("pat");
377+
let if_let_expr_pat = self.next("expr");
378+
println!(
379+
" if let Guard::IfLet(ref {}, ref {}) = {};",
380+
if_let_pat_pat, if_let_expr_pat, guard_pat
381+
);
382+
self.current = if_let_expr_pat;
383+
self.visit_expr(if_let_expr);
384+
self.current = if_let_pat_pat;
385+
self.visit_pat(if_let_pat);
386+
},
375387
}
376388
}
377389
self.current = format!("{}[{}].pat", arms_pat, i);

src/tools/clippy/clippy_lints/src/utils/hir_utils.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ impl<'a, 'tcx> SpanlessEq<'a, 'tcx> {
169169
fn eq_guard(&mut self, left: &Guard<'_>, right: &Guard<'_>) -> bool {
170170
match (left, right) {
171171
(Guard::If(l), Guard::If(r)) => self.eq_expr(l, r),
172+
(Guard::IfLet(lp, le), Guard::IfLet(rp, re)) => self.eq_pat(lp, rp) && self.eq_expr(le, re),
173+
_ => false,
172174
}
173175
}
174176

@@ -643,7 +645,7 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
643645

644646
pub fn hash_guard(&mut self, g: &Guard<'_>) {
645647
match g {
646-
Guard::If(ref expr) => {
648+
Guard::If(ref expr) | Guard::IfLet(_, ref expr) => {
647649
self.hash_expr(expr);
648650
},
649651
}

src/tools/clippy/clippy_lints/src/utils/inspector.rs

+5
Original file line numberDiff line numberDiff line change
@@ -560,5 +560,10 @@ fn print_guard(cx: &LateContext<'_>, guard: &hir::Guard<'_>, indent: usize) {
560560
println!("{}If", ind);
561561
print_expr(cx, expr, indent + 1);
562562
},
563+
hir::Guard::IfLet(pat, expr) => {
564+
println!("{}IfLet", ind);
565+
print_pat(cx, pat, indent + 1);
566+
print_expr(cx, expr, indent + 1);
567+
},
563568
}
564569
}

0 commit comments

Comments
 (0)