Skip to content

Commit

Permalink
Provide structured suggestions for valid formatting descriptors
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Nov 5, 2019
1 parent 08b235b commit c271db2
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/libsyntax_ext/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,9 @@ impl<'a, 'b> Context<'a, 'b> {
"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\
Expand All @@ -270,6 +271,26 @@ impl<'a, 'b> Context<'a, 'b> {
- `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>"
}
Expand Down

0 comments on commit c271db2

Please sign in to comment.