about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-12-03 07:13:59 +0100
committerMazdak Farrokhzad <twingoow@gmail.com>2019-12-20 22:41:28 +0100
commit2ddea30178bb2b95f9366943702a9f2711f7c547 (patch)
tree3c9a1b9bca9f76075bb461b856153455762996b6
parentc54c9ef863f3b9bfb3dd6e74cf183282fafacbd7 (diff)
downloadrust-2ddea30178bb2b95f9366943702a9f2711f7c547.tar.gz
rust-2ddea30178bb2b95f9366943702a9f2711f7c547.zip
extract suggest_slice_pat
-rw-r--r--src/libsyntax_expand/mbe/macro_rules.rs46
1 files changed, 25 insertions, 21 deletions
diff --git a/src/libsyntax_expand/mbe/macro_rules.rs b/src/libsyntax_expand/mbe/macro_rules.rs
index 2dd15872a9f..107fe388ed0 100644
--- a/src/libsyntax_expand/mbe/macro_rules.rs
+++ b/src/libsyntax_expand/mbe/macro_rules.rs
@@ -63,6 +63,30 @@ crate fn annotate_err_with_kind(
     };
 }
 
+/// Instead of e.g. `vec![a, b, c]` in a pattern context, suggest `[a, b, c]`.
+fn suggest_slice_pat(e: &mut DiagnosticBuilder<'_>, site_span: Span, parser: &Parser<'_>) {
+    let mut suggestion = None;
+    if let Ok(code) = parser.sess.source_map().span_to_snippet(site_span) {
+        if let Some(bang) = code.find('!') {
+            suggestion = Some(code[bang + 1..].to_string());
+        }
+    }
+    if let Some(suggestion) = suggestion {
+        e.span_suggestion(
+            site_span,
+            "use a slice pattern here instead",
+            suggestion,
+            Applicability::MachineApplicable,
+        );
+    } else {
+        e.span_label(site_span, "use a slice pattern here instead");
+    }
+    e.help(
+        "for more information, see https://doc.rust-lang.org/edition-guide/\
+        rust-2018/slice-patterns.html"
+    );
+}
+
 impl<'a> ParserAnyMacro<'a> {
     crate fn make(mut self: Box<ParserAnyMacro<'a>>, kind: AstFragmentKind) -> AstFragment {
         let ParserAnyMacro { site_span, macro_ident, ref mut parser, arm_span } = *self;
@@ -92,27 +116,7 @@ impl<'a> ParserAnyMacro<'a> {
             }
             match kind {
                 AstFragmentKind::Pat if macro_ident.name == sym::vec => {
-                    let mut suggestion = None;
-                    if let Ok(code) = parser.sess.source_map().span_to_snippet(site_span) {
-                        if let Some(bang) = code.find('!') {
-                            suggestion = Some(code[bang + 1..].to_string());
-                        }
-                    }
-                    if let Some(suggestion) = suggestion {
-                        e.span_suggestion(
-                            site_span,
-                            "use a slice pattern here instead",
-                            suggestion,
-                            Applicability::MachineApplicable,
-                        );
-                    } else {
-                        e.span_label(
-                            site_span,
-                            "use a slice pattern here instead",
-                        );
-                    }
-                    e.help("for more information, see https://doc.rust-lang.org/edition-guide/\
-                            rust-2018/slice-patterns.html");
+                    suggest_slice_pat(&mut e, site_span, parser);
                 }
                 _ => annotate_err_with_kind(&mut e, kind, site_span),
             };