]> git.lizzy.rs Git - rust.git/commitdiff
review comments: use structured suggestion
authorEsteban Küber <esteban@kuber.com.ar>
Fri, 9 Aug 2019 16:39:30 +0000 (09:39 -0700)
committerEsteban Küber <esteban@kuber.com.ar>
Fri, 9 Aug 2019 16:40:26 +0000 (09:40 -0700)
src/libsyntax/ext/expand.rs
src/libsyntax/ext/tt/macro_rules.rs
src/test/ui/suggestions/vec-macro-in-pattern.fixed [new file with mode: 0644]
src/test/ui/suggestions/vec-macro-in-pattern.rs
src/test/ui/suggestions/vec-macro-in-pattern.stderr

index 36f059531d39de73ef119edd7e264f3a74e70826..9a3195b1165b10d17798ae7f6f669aa56e7f0dc4 100644 (file)
@@ -6,6 +6,7 @@
 use crate::ext::base::*;
 use crate::ext::proc_macro::collect_derives;
 use crate::ext::hygiene::{ExpnId, SyntaxContext, ExpnInfo, ExpnKind};
+use crate::ext::tt::macro_rules::annotate_err_with_kind;
 use crate::ext::placeholders::{placeholder, PlaceholderExpander};
 use crate::feature_gate::{self, Features, GateIssue, is_builtin_attr, emit_feature_err};
 use crate::mut_visit::*;
@@ -701,21 +702,7 @@ fn parse_ast_fragment(
             }
             Err(mut err) => {
                 err.set_span(span);
-                match kind {
-                    AstFragmentKind::Ty => {
-                        err.span_label(
-                            span,
-                            "this macro call doesn't expand to a type",
-                        );
-                    }
-                    AstFragmentKind::Pat => {
-                        err.span_label(
-                            span,
-                            "this macro call doesn't expand to a pattern",
-                        );
-                    }
-                    _ => {}
-                };
+                annotate_err_with_kind(&mut err, kind, span);
                 err.emit();
                 self.cx.trace_macros_diag();
                 kind.dummy(span)
index a9d0b739d6ac70aa576cd03643abaa1a16465f74..b057a9ad44d0bd966a71ab5998211d9baf36ac7a 100644 (file)
@@ -17,7 +17,7 @@
 use crate::tokenstream::{DelimSpan, TokenStream, TokenTree};
 use crate::{ast, attr, attr::TransparencyError};
 
-use errors::FatalError;
+use errors::{DiagnosticBuilder, FatalError};
 use log::debug;
 use syntax_pos::Span;
 
@@ -43,6 +43,18 @@ pub struct ParserAnyMacro<'a> {
     arm_span: Span,
 }
 
+pub fn annotate_err_with_kind(err: &mut DiagnosticBuilder<'_>, kind: AstFragmentKind, span: Span) {
+    match kind {
+        AstFragmentKind::Ty => {
+            err.span_label(span, "this macro call doesn't expand to a type");
+        }
+        AstFragmentKind::Pat => {
+            err.span_label(span, "this macro call doesn't expand to a pattern");
+        }
+        _ => {}
+    };
+}
+
 impl<'a> ParserAnyMacro<'a> {
     pub fn make(mut self: Box<ParserAnyMacro<'a>>, kind: AstFragmentKind) -> AstFragment {
         let ParserAnyMacro { site_span, macro_ident, ref mut parser, arm_span } = *self;
@@ -71,27 +83,30 @@ pub fn make(mut self: Box<ParserAnyMacro<'a>>, kind: AstFragmentKind) -> AstFrag
                 e.span_label(site_span, "in this macro invocation");
             }
             match kind {
-                AstFragmentKind::Ty => {
-                    e.span_label(
-                        site_span,
-                        "this macro call doesn't expand to a type",
-                    );
-                }
                 AstFragmentKind::Pat if macro_ident.name == sym::vec => {
-                    e.span_label(
-                        site_span,
-                        "use a slice pattern here instead",
-                    );
+                    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");
-                }
-                AstFragmentKind::Pat => {
-                    e.span_label(
-                        site_span,
-                        "this macro call doesn't expand to a pattern",
-                    );
+                            rust-2018/slice-patterns.html");
                 }
-                _ => {}
+                _ => annotate_err_with_kind(&mut e, kind, site_span),
             };
             e
         }));
diff --git a/src/test/ui/suggestions/vec-macro-in-pattern.fixed b/src/test/ui/suggestions/vec-macro-in-pattern.fixed
new file mode 100644 (file)
index 0000000..e1695d6
--- /dev/null
@@ -0,0 +1,8 @@
+// run-rustfix
+fn main() {
+    // everything after `.as_ref` should be suggested
+    match Some(vec![3]).as_ref().map(|v| v.as_slice()) {
+        Some([_x]) => (), //~ ERROR unexpected `(` after qualified path
+        _ => (),
+    }
+}
index 5c42a6bdbd44ed40fb79ed1adb8540047173b7f3..4843629fbcf904788e0567d86717418ce7005794 100644 (file)
@@ -1,6 +1,8 @@
+// run-rustfix
 fn main() {
-    match Some(vec![3]) {
-        Some(vec![x]) => (), //~ ERROR unexpected `(` after qualified path
+    // everything after `.as_ref` should be suggested
+    match Some(vec![3]).as_ref().map(|v| v.as_slice()) {
+        Some(vec![_x]) => (), //~ ERROR unexpected `(` after qualified path
         _ => (),
     }
 }
index f94cb93a520b5abff0b4ba3c4adb74c6467b0062..59ca8ebbf63399eb34928b67a59563c6521c7f39 100644 (file)
@@ -1,12 +1,12 @@
 error: unexpected `(` after qualified path
-  --> $DIR/vec-macro-in-pattern.rs:3:14
+  --> $DIR/vec-macro-in-pattern.rs:5:14
    |
-LL |         Some(vec![x]) => (),
-   |              ^^^^^^^
+LL |         Some(vec![_x]) => (),
+   |              ^^^^^^^^
    |              |
    |              unexpected `(` after qualified path
    |              in this macro invocation
-   |              use a slice pattern here instead
+   |              help: use a slice pattern here instead: `[_x]`
    |
    = help: for more information, see https://doc.rust-lang.org/edition-guide/rust-2018/slice-patterns.html
    = note: this warning originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)