]> git.lizzy.rs Git - rust.git/commitdiff
Fix single-match-else in the presence of macros expressions expanding to blocks
authorOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Fri, 3 Mar 2017 15:56:16 +0000 (16:56 +0100)
committerOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Fri, 3 Mar 2017 15:56:16 +0000 (16:56 +0100)
clippy_lints/src/matches.rs
tests/run-pass/single-match-else.rs [new file with mode: 0644]

index e2bdf3b80b80fedab58d9a0e1943c5db328ad1da..8729a39733ac5597bcf11137f85db8c9f6eb0bbe 100644 (file)
@@ -10,7 +10,7 @@
 use syntax::codemap::Span;
 use utils::paths;
 use utils::{match_type, snippet, span_note_and_lint, span_lint_and_then, in_external_macro, expr_block, walk_ptrs_ty,
-            is_expn_of};
+            is_expn_of, remove_blocks};
 use utils::sugg::Sugg;
 
 /// **What it does:** Checks for matches with a single arm where an `if let`
@@ -179,11 +179,12 @@ fn check_single_match(cx: &LateContext, ex: &Expr, arms: &[Arm], expr: &Expr) {
     if arms.len() == 2 &&
       arms[0].pats.len() == 1 && arms[0].guard.is_none() &&
       arms[1].pats.len() == 1 && arms[1].guard.is_none() {
-        let els = if is_unit_expr(&arms[1].body) {
+        let els = remove_blocks(&arms[1].body);
+        let els = if is_unit_expr(els) {
             None
-        } else if let ExprBlock(_) = arms[1].body.node {
+        } else if let ExprBlock(_) = els.node {
             // matches with blocks that contain statements are prettier as `if let + else`
-            Some(&*arms[1].body)
+            Some(els)
         } else {
             // allow match arms with just expressions
             return;
diff --git a/tests/run-pass/single-match-else.rs b/tests/run-pass/single-match-else.rs
new file mode 100644 (file)
index 0000000..fe3cf1c
--- /dev/null
@@ -0,0 +1,11 @@
+#![feature(plugin)]
+#![plugin(clippy)]
+#![warn(single_match_else)]
+
+fn main() {
+    let n = match (42, 43) {
+        (42, n) => n,
+        _ => panic!("typeck error"),
+    };
+    assert_eq!(n, 43);
+}