diff --git a/src/copies.rs b/src/copies.rs index 04f8aaa37e7..2de034f83c2 100644 --- a/src/copies.rs +++ b/src/copies.rs @@ -132,13 +132,15 @@ fn lint_match_arms(cx: &LateContext, expr: &Expr) { }; let eq = |lhs: &Arm, rhs: &Arm| -> bool { - SpanlessEq::new(cx).eq_expr(&lhs.body, &rhs.body) && + // Arms with a guard are ignored, those can’t always be merged together + lhs.guard.is_none() && rhs.guard.is_none() && + SpanlessEq::new(cx).eq_expr(&lhs.body, &rhs.body) && // all patterns should have the same bindings bindings(cx, &lhs.pats[0]) == bindings(cx, &rhs.pats[0]) }; if let ExprMatch(_, ref arms, MatchSource::Normal) = expr.node { - if let Some((i, j)) = search_same(&**arms, hash, eq) { + if let Some((i, j)) = search_same(&arms, hash, eq) { span_note_and_lint(cx, MATCH_SAME_ARMS, j.body.span, diff --git a/tests/compile-fail/copies.rs b/tests/compile-fail/copies.rs index 66457e77f47..68756a57cc7 100644 --- a/tests/compile-fail/copies.rs +++ b/tests/compile-fail/copies.rs @@ -142,12 +142,23 @@ fn if_same_then_else() -> Result<&'static str, ()> { _ => true, }; + let _ = match Some(42) { + Some(_) => 24, + None => 24, + }; + let _ = match Some(42) { Some(42) => 24, Some(a) => 24, // bindings are different None => 0, }; + let _ = match Some(42) { + Some(a) if a > 0 => 24, + Some(a) => 24, // one arm has a guard + None => 0, + }; + match (Some(42), Some(42)) { (Some(a), None) => bar(a), (None, Some(a)) => bar(a), //~ERROR this `match` has identical arm bodies