Skip to content

Commit 131c8f8

Browse files
authored
Merge pull request #3113 from mikerite/fix-3112
Fix #3112
2 parents c81d70e + 19157c0 commit 131c8f8

File tree

5 files changed

+45
-8
lines changed

5 files changed

+45
-8
lines changed

clippy_lints/src/eval_order_dependence.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ impl<'a, 'tcx> DivergenceVisitor<'a, 'tcx> {
109109
self.visit_expr(e);
110110
for arm in arms {
111111
if let Some(ref guard) = arm.guard {
112-
self.visit_expr(guard);
112+
match guard {
113+
Guard::If(if_expr) => self.visit_expr(if_expr),
114+
}
113115
}
114116
// make sure top level arm expressions aren't linted
115117
self.maybe_walk_expr(&*arm.body);

clippy_lints/src/shadow.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,9 @@ fn check_expr<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, bindings:
339339
check_pat(cx, pat, Some(&**init), pat.span, bindings);
340340
// This is ugly, but needed to get the right type
341341
if let Some(ref guard) = arm.guard {
342-
check_expr(cx, guard, bindings);
342+
match guard {
343+
Guard::If(if_expr) => check_expr(cx, if_expr, bindings),
344+
}
343345
}
344346
check_expr(cx, &arm.body, bindings);
345347
bindings.truncate(len);

clippy_lints/src/utils/author.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,15 @@ impl<'tcx> Visitor<'tcx> for PrintVisitor {
345345
self.visit_expr(&arm.body);
346346
if let Some(ref guard) = arm.guard {
347347
let guard_pat = self.next("guard");
348-
println!(" if let Some(ref {}) = {}[{}].guard", guard_pat, arms_pat, i);
349-
self.current = guard_pat;
350-
self.visit_expr(guard);
348+
println!(" if let Some(ref {}) = {}[{}].guard;", guard_pat, arms_pat, i);
349+
match guard {
350+
hir::Guard::If(ref if_expr) => {
351+
let if_expr_pat = self.next("expr");
352+
println!(" if let Guard::If(ref {}) = {};", if_expr_pat, guard_pat);
353+
self.current = if_expr_pat;
354+
self.visit_expr(if_expr);
355+
}
356+
}
351357
}
352358
println!(" if {}[{}].pats.len() == {};", arms_pat, i, arm.pats.len());
353359
for (j, pat) in arm.pats.iter().enumerate() {

clippy_lints/src/utils/hir_utils.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
113113
},
114114
(&ExprKind::Match(ref le, ref la, ref ls), &ExprKind::Match(ref re, ref ra, ref rs)) => {
115115
ls == rs && self.eq_expr(le, re) && over(la, ra, |l, r| {
116-
self.eq_expr(&l.body, &r.body) && both(&l.guard, &r.guard, |l, r| self.eq_expr(l, r))
116+
self.eq_expr(&l.body, &r.body) && both(&l.guard, &r.guard, |l, r| self.eq_guard(l, r))
117117
&& over(&l.pats, &r.pats, |l, r| self.eq_pat(l, r))
118118
})
119119
},
@@ -152,6 +152,12 @@ impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
152152
left.ident.name == right.ident.name && self.eq_expr(&left.expr, &right.expr)
153153
}
154154

155+
fn eq_guard(&mut self, left: &Guard, right: &Guard) -> bool {
156+
match (left, right) {
157+
(Guard::If(l), Guard::If(r)) => self.eq_expr(l, r),
158+
}
159+
}
160+
155161
fn eq_generic_arg(&mut self, left: &GenericArg, right: &GenericArg) -> bool {
156162
match (left, right) {
157163
(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> {
497503
for arm in arms {
498504
// TODO: arm.pat?
499505
if let Some(ref e) = arm.guard {
500-
self.hash_expr(e);
506+
self.hash_guard(e);
501507
}
502508
self.hash_expr(&arm.body);
503509
}
@@ -637,4 +643,14 @@ impl<'a, 'tcx: 'a> SpanlessHash<'a, 'tcx> {
637643
},
638644
}
639645
}
646+
647+
pub fn hash_guard(&mut self, g: &Guard) {
648+
match g {
649+
Guard::If(ref expr) => {
650+
let c: fn(_) -> _ = Guard::If;
651+
c.hash(&mut self.s);
652+
self.hash_expr(expr);
653+
}
654+
}
655+
}
640656
}

clippy_lints/src/utils/inspector.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
113113
}
114114
if let Some(ref guard) = arm.guard {
115115
println!("guard:");
116-
print_expr(cx, guard, 1);
116+
print_guard(cx, guard, 1);
117117
}
118118
println!("body:");
119119
print_expr(cx, &arm.body, 1);
@@ -515,3 +515,14 @@ fn print_pat(cx: &LateContext<'_, '_>, pat: &hir::Pat, indent: usize) {
515515
},
516516
}
517517
}
518+
519+
fn print_guard(cx: &LateContext<'_, '_>, guard: &hir::Guard, indent: usize) {
520+
let ind = " ".repeat(indent);
521+
println!("{}+", ind);
522+
match guard {
523+
hir::Guard::If(expr) => {
524+
println!("{}If", ind);
525+
print_expr(cx, expr, indent + 1);
526+
}
527+
}
528+
}

0 commit comments

Comments
 (0)