]> git.lizzy.rs Git - rust.git/commitdiff
Provide structured suggestions for valid formatting descriptors
authorEsteban Küber <esteban@kuber.com.ar>
Tue, 5 Nov 2019 20:08:22 +0000 (12:08 -0800)
committerEsteban Küber <esteban@kuber.com.ar>
Tue, 5 Nov 2019 22:06:38 +0000 (14:06 -0800)
src/libsyntax_ext/format.rs

index b8d053a2162de8a62b457d4ae9dc07315b3aed70..b47038397b80ba549d6a49bf33d133e8eef5f4ba 100644 (file)
@@ -256,8 +256,9 @@ fn verify_piece(&mut self, p: &parse::Piece<'_>) {
                     "X" => "UpperHex",
                     _ => {
                         let fmtsp = self.fmtsp;
+                        let sp = arg.format.ty_span.map(|sp| fmtsp.from_inner(sp));
                         let mut err = self.ecx.struct_span_err(
-                            arg.format.ty_span.map(|sp| fmtsp.from_inner(sp)).unwrap_or(fmtsp),
+                            sp.unwrap_or(fmtsp),
                             &format!("unknown format trait `{}`", arg.format.ty),
                         );
                         err.note("the only appropriate formatting traits are:\n\
@@ -270,6 +271,26 @@ fn verify_piece(&mut self, p: &parse::Piece<'_>) {
                                 - `b`, which uses the `Binary` trait\n\
                                 - `x`, which uses the `LowerHex` trait\n\
                                 - `X`, which uses the `UpperHex` trait");
+                        if let Some(sp) = sp {
+                            for (fmt, name) in &[
+                                ("", "Display"),
+                                ("?", "Debug"),
+                                ("e", "LowerExp"),
+                                ("E", "UpperExp"),
+                                ("o", "Octal"),
+                                ("p", "Pointer"),
+                                ("b", "Binary"),
+                                ("x", "LowerHex"),
+                                ("X", "UpperHex"),
+                            ] {
+                                err.tool_only_span_suggestion(
+                                    sp,
+                                    &format!("use the `{}` trait", name),
+                                    fmt.to_string(),
+                                    Applicability::MaybeIncorrect,
+                                );
+                            }
+                        }
                         err.emit();
                         "<invalid>"
                     }