Skip to content

Commit c271db2

Browse files
committed
Provide structured suggestions for valid formatting descriptors
1 parent 08b235b commit c271db2

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/libsyntax_ext/format.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,9 @@ impl<'a, 'b> Context<'a, 'b> {
256256
"X" => "UpperHex",
257257
_ => {
258258
let fmtsp = self.fmtsp;
259+
let sp = arg.format.ty_span.map(|sp| fmtsp.from_inner(sp));
259260
let mut err = self.ecx.struct_span_err(
260-
arg.format.ty_span.map(|sp| fmtsp.from_inner(sp)).unwrap_or(fmtsp),
261+
sp.unwrap_or(fmtsp),
261262
&format!("unknown format trait `{}`", arg.format.ty),
262263
);
263264
err.note("the only appropriate formatting traits are:\n\
@@ -270,6 +271,26 @@ impl<'a, 'b> Context<'a, 'b> {
270271
- `b`, which uses the `Binary` trait\n\
271272
- `x`, which uses the `LowerHex` trait\n\
272273
- `X`, which uses the `UpperHex` trait");
274+
if let Some(sp) = sp {
275+
for (fmt, name) in &[
276+
("", "Display"),
277+
("?", "Debug"),
278+
("e", "LowerExp"),
279+
("E", "UpperExp"),
280+
("o", "Octal"),
281+
("p", "Pointer"),
282+
("b", "Binary"),
283+
("x", "LowerHex"),
284+
("X", "UpperHex"),
285+
] {
286+
err.tool_only_span_suggestion(
287+
sp,
288+
&format!("use the `{}` trait", name),
289+
fmt.to_string(),
290+
Applicability::MaybeIncorrect,
291+
);
292+
}
293+
}
273294
err.emit();
274295
"<invalid>"
275296
}

0 commit comments

Comments
 (0)