]> git.lizzy.rs Git - rust.git/commitdiff
Don't expand macro in single_match suggestion
authorPhilipp Hansch <dev@phansch.net>
Sat, 27 Oct 2018 12:45:02 +0000 (14:45 +0200)
committerPhilipp Hansch <dev@phansch.net>
Sat, 27 Oct 2018 13:47:56 +0000 (15:47 +0200)
clippy_lints/src/matches.rs
clippy_lints/src/utils/mod.rs
tests/ui/matches.stderr
tests/ui/single_match.rs
tests/ui/single_match.stderr

index e46615f4da2f5abca324e49abeba0d4e47993c65..4a704c3d52ece894c0e4a8991cd28d7f02d5f841 100644 (file)
@@ -19,7 +19,8 @@
 use crate::syntax::source_map::Span;
 use crate::utils::paths;
 use crate::utils::{expr_block, is_allowed, is_expn_of, match_qpath, match_type, multispan_sugg,
-            remove_blocks, snippet, span_lint_and_sugg, span_lint_and_then, span_note_and_lint, walk_ptrs_ty};
+            remove_blocks, snippet, span_lint_and_sugg, span_lint_and_then,
+            span_note_and_lint, walk_ptrs_ty};
 use crate::utils::sugg::Sugg;
 use crate::consts::{constant, Constant};
 use crate::rustc_errors::Applicability;
index 5ff246630e038ec4a8e87f6b684708e693900cb7..72a6bda26c36b41676dadab8d4a12cdafaf18bb8 100644 (file)
@@ -406,7 +406,10 @@ pub fn expr_block<'a, 'b, T: LintContext<'b>>(
 ) -> Cow<'a, str> {
     let code = snippet_block(cx, expr.span, default);
     let string = option.unwrap_or_default();
-    if let ExprKind::Block(_, _) = expr.node {
+    if in_macro(expr.span) {
+        Cow::Owned(format!("{{ {} }}", snippet_with_macro_callsite(cx, expr.span, default)))
+    }
+    else if let ExprKind::Block(_, _) = expr.node {
         Cow::Owned(format!("{}{}", code, string))
     } else if string.is_empty() {
         Cow::Owned(format!("{{ {} }}", code))
index bed903faf1a12062bc6d369d8642f78c4565a74e..b5f1f2ab0e731dcbf82f48c836847f5d9dadb06c 100644 (file)
@@ -33,7 +33,7 @@ error: you seem to be trying to use match for destructuring a single pattern. Co
 51 | |         &(v, 1) => println!("{}", v),
 52 | |         _ => println!("none"),
 53 | |     }
-   | |_____^ help: try this: `if let &(v, 1) = tup { $ crate :: io :: _print ( format_args_nl ! ( $ ( $ arg ) * ) ) ; } else { $ crate :: io :: _print ( format_args_nl ! ( $ ( $ arg ) * ) ) ; }`
+   | |_____^ help: try this: `if let &(v, 1) = tup { println!("{}", v) } else { println!("none") }`
 
 error: you don't need to add `&` to all patterns
   --> $DIR/matches.rs:50:5
index 5c7cae249b4f26c79b16c4b3f2651178360e96b9..dca68e179e7425ac06a0baa0e30733e74d78b4dc 100644 (file)
@@ -23,6 +23,15 @@ fn single_match(){
         _ => ()
     };
 
+    let x = Some(1u8);
+    match x {
+        // Note the missing block braces.
+        // We suggest `if let Some(y) = x { .. }` because the macro
+        // is expanded before we can do anything.
+        Some(y) => println!("{:?}", y),
+        _ => ()
+    }
+
     let z = (1u8,1u8);
     match z {
         (2...3, 7...9) => dummy(),
index 74448391ca5277e76163962f95375c857e0f75a3..df614ad201d1d3642163c18eec00ea398ff6a97d 100644 (file)
@@ -12,38 +12,50 @@ error: you seem to be trying to use match for destructuring a single pattern. Co
 error: you seem to be trying to use match for destructuring a single pattern. Consider using `if let`
   --> $DIR/single_match.rs:27:5
    |
-27 | /     match z {
-28 | |         (2...3, 7...9) => dummy(),
-29 | |         _ => {}
-30 | |     };
+27 | /     match x {
+28 | |         // Note the missing block braces.
+29 | |         // We suggest `if let Some(y) = x { .. }` because the macro
+30 | |         // is expanded before we can do anything.
+31 | |         Some(y) => println!("{:?}", y),
+32 | |         _ => ()
+33 | |     }
+   | |_____^ help: try this: `if let Some(y) = x { println!("{:?}", y) }`
+
+error: you seem to be trying to use match for destructuring a single pattern. Consider using `if let`
+  --> $DIR/single_match.rs:36:5
+   |
+36 | /     match z {
+37 | |         (2...3, 7...9) => dummy(),
+38 | |         _ => {}
+39 | |     };
    | |_____^ help: try this: `if let (2...3, 7...9) = z { dummy() }`
 
 error: you seem to be trying to use match for destructuring a single pattern. Consider using `if let`
-  --> $DIR/single_match.rs:53:5
+  --> $DIR/single_match.rs:62:5
    |
-53 | /     match x {
-54 | |         Some(y) => dummy(),
-55 | |         None => ()
-56 | |     };
+62 | /     match x {
+63 | |         Some(y) => dummy(),
+64 | |         None => ()
+65 | |     };
    | |_____^ help: try this: `if let Some(y) = x { dummy() }`
 
 error: you seem to be trying to use match for destructuring a single pattern. Consider using `if let`
-  --> $DIR/single_match.rs:58:5
+  --> $DIR/single_match.rs:67:5
    |
-58 | /     match y {
-59 | |         Ok(y) => dummy(),
-60 | |         Err(..) => ()
-61 | |     };
+67 | /     match y {
+68 | |         Ok(y) => dummy(),
+69 | |         Err(..) => ()
+70 | |     };
    | |_____^ help: try this: `if let Ok(y) = y { dummy() }`
 
 error: you seem to be trying to use match for destructuring a single pattern. Consider using `if let`
-  --> $DIR/single_match.rs:65:5
+  --> $DIR/single_match.rs:74:5
    |
-65 | /     match c {
-66 | |         Cow::Borrowed(..) => dummy(),
-67 | |         Cow::Owned(..) => (),
-68 | |     };
+74 | /     match c {
+75 | |         Cow::Borrowed(..) => dummy(),
+76 | |         Cow::Owned(..) => (),
+77 | |     };
    | |_____^ help: try this: `if let Cow::Borrowed(..) = c { dummy() }`
 
-error: aborting due to 5 previous errors
+error: aborting due to 6 previous errors