Skip to content

Fix #3112 #3113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from Sep 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion clippy_lints/src/eval_order_dependence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion clippy_lints/src/shadow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 9 additions & 3 deletions clippy_lints/src/utils/author.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
20 changes: 18 additions & 2 deletions clippy_lints/src/utils/hir_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
},
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
}
}
}
13 changes: 12 additions & 1 deletion clippy_lints/src/utils/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
}