]> git.lizzy.rs Git - rust.git/commitdiff
Fix false positive in `MATCH_SAME_ARMS` and guards
authormcarton <cartonmartin+git@gmail.com>
Mon, 28 Mar 2016 23:39:35 +0000 (01:39 +0200)
committermcarton <cartonmartin+git@gmail.com>
Mon, 28 Mar 2016 23:39:35 +0000 (01:39 +0200)
src/copies.rs
tests/compile-fail/copies.rs

index 04f8aaa37e7bedfa087aa5a843f45669254d5157..2de034f83c2703cb95078a06934e1507b85c3df3 100644 (file)
@@ -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,
index 66457e77f476422104b577461ae664150f7fa099..68756a57cc75ee21c5dd4e88555d87bb5dab7253 100644 (file)
@@ -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