diff --git a/clippy_lints/src/eval_order_dependence.rs b/clippy_lints/src/eval_order_dependence.rs index 72abbddf1..e3ae56076 100644 --- a/clippy_lints/src/eval_order_dependence.rs +++ b/clippy_lints/src/eval_order_dependence.rs @@ -109,7 +109,9 @@ impl<'a, 'tcx> DivergenceVisitor<'a, 'tcx> { self.visit_expr(e); for arm in arms { if let Some(ref guard) = arm.guard { - self.visit_expr(guard); + match guard { + Guard::If(if_expr) => self.visit_expr(if_expr), + } } // make sure top level arm expressions aren't linted self.maybe_walk_expr(&*arm.body); diff --git a/clippy_lints/src/shadow.rs b/clippy_lints/src/shadow.rs index d552c0167..1191723ba 100644 --- a/clippy_lints/src/shadow.rs +++ b/clippy_lints/src/shadow.rs @@ -339,7 +339,9 @@ fn check_expr<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, bindings: check_pat(cx, pat, Some(&**init), pat.span, bindings); // This is ugly, but needed to get the right type if let Some(ref guard) = arm.guard { - check_expr(cx, guard, bindings); + match guard { + Guard::If(if_expr) => check_expr(cx, if_expr, bindings), + } } check_expr(cx, &arm.body, bindings); bindings.truncate(len); diff --git a/clippy_lints/src/utils/author.rs b/clippy_lints/src/utils/author.rs index fb131b908..e2c809c0c 100644 --- a/clippy_lints/src/utils/author.rs +++ b/clippy_lints/src/utils/author.rs @@ -345,9 +345,15 @@ impl<'tcx> Visitor<'tcx> for PrintVisitor { self.visit_expr(&arm.body); if let Some(ref guard) = arm.guard { let guard_pat = self.next("guard"); - println!(" if let Some(ref {}) = {}[{}].guard", guard_pat, arms_pat, i); - self.current = guard_pat; - self.visit_expr(guard); + println!(" if let Some(ref {}) = {}[{}].guard;", guard_pat, arms_pat, i); + match guard { + hir::Guard::If(ref if_expr) => { + let if_expr_pat = self.next("expr"); + println!(" if let Guard::If(ref {}) = {};", if_expr_pat, guard_pat); + self.current = if_expr_pat; + self.visit_expr(if_expr); + } + } } println!(" if {}[{}].pats.len() == {};", arms_pat, i, arm.pats.len()); for (j, pat) in arm.pats.iter().enumerate() { diff --git a/clippy_lints/src/utils/hir_utils.rs b/clippy_lints/src/utils/hir_utils.rs index 93d73ca70..939b4f595 100644 --- a/clippy_lints/src/utils/hir_utils.rs +++ b/clippy_lints/src/utils/hir_utils.rs @@ -113,7 +113,7 @@ impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> { }, (&ExprKind::Match(ref le, ref la, ref ls), &ExprKind::Match(ref re, ref ra, ref rs)) => { ls == rs && self.eq_expr(le, re) && over(la, ra, |l, r| { - self.eq_expr(&l.body, &r.body) && both(&l.guard, &r.guard, |l, r| self.eq_expr(l, r)) + self.eq_expr(&l.body, &r.body) && both(&l.guard, &r.guard, |l, r| self.eq_guard(l, r)) && over(&l.pats, &r.pats, |l, r| self.eq_pat(l, r)) }) }, @@ -152,6 +152,12 @@ impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> { left.ident.name == right.ident.name && self.eq_expr(&left.expr, &right.expr) } + fn eq_guard(&mut self, left: &Guard, right: &Guard) -> bool { + match (left, right) { + (Guard::If(l), Guard::If(r)) => self.eq_expr(l, r), + } + } + fn eq_generic_arg(&mut self, left: &GenericArg, right: &GenericArg) -> bool { match (left, right) { (GenericArg::Lifetime(l_lt), GenericArg::Lifetime(r_lt)) => self.eq_lifetime(l_lt, r_lt), @@ -497,7 +503,7 @@ impl<'a, 'tcx: 'a> SpanlessHash<'a, 'tcx> { for arm in arms { // TODO: arm.pat? if let Some(ref e) = arm.guard { - self.hash_expr(e); + self.hash_guard(e); } self.hash_expr(&arm.body); } @@ -637,4 +643,14 @@ impl<'a, 'tcx: 'a> SpanlessHash<'a, 'tcx> { }, } } + + pub fn hash_guard(&mut self, g: &Guard) { + match g { + Guard::If(ref expr) => { + let c: fn(_) -> _ = Guard::If; + c.hash(&mut self.s); + self.hash_expr(expr); + } + } + } } diff --git a/clippy_lints/src/utils/inspector.rs b/clippy_lints/src/utils/inspector.rs index b9a0435eb..56b76fdc7 100644 --- a/clippy_lints/src/utils/inspector.rs +++ b/clippy_lints/src/utils/inspector.rs @@ -113,7 +113,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass { } if let Some(ref guard) = arm.guard { println!("guard:"); - print_expr(cx, guard, 1); + print_guard(cx, guard, 1); } println!("body:"); print_expr(cx, &arm.body, 1); @@ -515,3 +515,14 @@ fn print_pat(cx: &LateContext<'_, '_>, pat: &hir::Pat, indent: usize) { }, } } + +fn print_guard(cx: &LateContext<'_, '_>, guard: &hir::Guard, indent: usize) { + let ind = " ".repeat(indent); + println!("{}+", ind); + match guard { + hir::Guard::If(expr) => { + println!("{}If", ind); + print_expr(cx, expr, indent + 1); + } + } +}