From: Esteban Küber Date: Tue, 5 Nov 2019 20:08:22 +0000 (-0800) Subject: Provide structured suggestions for valid formatting descriptors X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=c271db284bea3882fe033afb1cd4c6f370c69dd5;p=rust.git Provide structured suggestions for valid formatting descriptors --- diff --git a/src/libsyntax_ext/format.rs b/src/libsyntax_ext/format.rs index b8d053a2162..b47038397b8 100644 --- a/src/libsyntax_ext/format.rs +++ b/src/libsyntax_ext/format.rs @@ -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(); "" }