Suggest enclosing format string with "" under special cases

* Suggest enclosing format string under special cases

This commit add suggestions about enclosing format string when it falls
into the following cases: `{}`, `{:?}`, `{:#?}`.
* Add HELP annotations in the UI test
This commit is contained in:
cclfmht
2026-04-26 10:50:05 +00:00
parent 2eaa5de4a3
commit 2c16f9edf5
5 changed files with 136 additions and 3 deletions
+25 -1
View File
@@ -173,6 +173,16 @@ fn make_format_args(
style: fmt_style,
uncooked_symbol: uncooked_fmt_str,
} = {
// Extract snippet so that we can check cases `{}`, `{:?}` and `{:#?}` and emit help for
// them later.
let snippet = if let ExprKind::Block(b, None) = &efmt.kind
&& b.stmts.len() <= 1
{
Some(ecx.sess.source_map().span_to_snippet(unexpanded_fmt_span))
} else {
None
};
let ExpandResult::Ready(mac) = expr_to_spanned_string(ecx, efmt.clone(), msg) else {
return ExpandResult::Retry(());
};
@@ -222,12 +232,26 @@ fn make_format_args(
});
}
sugg_fmt = sugg_fmt.trim_end().to_string();
err.span_suggestion(
err.span_suggestion_verbose(
unexpanded_fmt_span.shrink_to_lo(),
"you might be missing a string literal to format with",
format!("\"{sugg_fmt}\", "),
Applicability::MaybeIncorrect,
);
if let Some(Ok(snippet)) = snippet.as_ref() {
match snippet.as_str() {
"{}" | "{:?}" | "{:#?}" => {
err.span_suggestion_verbose(
unexpanded_fmt_span,
format!("you might want to enclose `{snippet}` with `\"\"`"),
format!("\"{snippet}\""),
Applicability::MaybeIncorrect,
);
}
_ => {}
};
}
}
}
err.emit()
@@ -2,9 +2,9 @@
fn main() {
let s = "123";
println!("{:?} {} {}", {}, "sss", s);
println!("{:?} {} {}", "{}", "sss", s);
//~^ ERROR format argument must be a string literal
println!("{:?}", {});
println!("{:?}", "{}");
//~^ ERROR format argument must be a string literal
println!("{} {} {} {:?}", s, "sss", s, {});
//~^ ERROR format argument must be a string literal
@@ -8,6 +8,11 @@ help: you might be missing a string literal to format with
|
LL | println!("{:?} {} {}", {}, "sss", s);
| +++++++++++++
help: you might want to enclose `{}` with `""`
|
LL - println!({}, "sss", s);
LL + println!("{}", "sss", s);
|
error: format argument must be a string literal
--> $DIR/format-empty-block-unit-tuple-suggestion-130170.rs:7:14
@@ -19,6 +24,11 @@ help: you might be missing a string literal to format with
|
LL | println!("{:?}", {});
| +++++++
help: you might want to enclose `{}` with `""`
|
LL - println!({});
LL + println!("{}");
|
error: format argument must be a string literal
--> $DIR/format-empty-block-unit-tuple-suggestion-130170.rs:9:14
@@ -0,0 +1,27 @@
// Suggest enclosing the format string with `""` when it is one of `{}`, `{:?}`, and `{:#?}`.
#[derive(Debug)]
enum UwU {
QwQ,
AwA,
QAQ,
}
fn main() {
println!({}, UwU::QwQ);
//~^ ERROR format argument must be a string literal
//~| HELP you might be missing a string literal to format with
//~| HELP you might want to enclose `{}` with `""`
println!({:?}, UwU::QwQ);
//~^ ERROR expected expression, found `:`
//~| ERROR format argument must be a string literal
//~| HELP you might be missing a string literal to format with
//~| HELP maybe write a path separator here
//~| HELP you might want to enclose `{:?}` with `""`
println!({:#?}, UwU::QwQ);
//~^ ERROR expected expression, found `:`
//~| ERROR format argument must be a string literal
//~| HELP you might be missing a string literal to format with
//~| HELP maybe write a path separator here
//~| HELP you might want to enclose `{:#?}` with `""`
}
@@ -0,0 +1,72 @@
error: format argument must be a string literal
--> $DIR/suggest-enclosing-format-string.rs:11:14
|
LL | println!({}, UwU::QwQ);
| ^^
|
help: you might be missing a string literal to format with
|
LL | println!("{:?} {}", {}, UwU::QwQ);
| ++++++++++
help: you might want to enclose `{}` with `""`
|
LL - println!({}, UwU::QwQ);
LL + println!("{}", UwU::QwQ);
|
error: expected expression, found `:`
--> $DIR/suggest-enclosing-format-string.rs:15:15
|
LL | println!({:?}, UwU::QwQ);
| ^ expected expression
|
help: maybe write a path separator here
|
LL | println!({::?}, UwU::QwQ);
| +
error: format argument must be a string literal
--> $DIR/suggest-enclosing-format-string.rs:15:14
|
LL | println!({:?}, UwU::QwQ);
| ^^^^
|
help: you might be missing a string literal to format with
|
LL | println!("{} {}", {:?}, UwU::QwQ);
| ++++++++
help: you might want to enclose `{:?}` with `""`
|
LL - println!({:?}, UwU::QwQ);
LL + println!("{:?}", UwU::QwQ);
|
error: expected expression, found `:`
--> $DIR/suggest-enclosing-format-string.rs:21:15
|
LL | println!({:#?}, UwU::QwQ);
| ^ expected expression
|
help: maybe write a path separator here
|
LL | println!({::#?}, UwU::QwQ);
| +
error: format argument must be a string literal
--> $DIR/suggest-enclosing-format-string.rs:21:14
|
LL | println!({:#?}, UwU::QwQ);
| ^^^^^
|
help: you might be missing a string literal to format with
|
LL | println!("{} {}", {:#?}, UwU::QwQ);
| ++++++++
help: you might want to enclose `{:#?}` with `""`
|
LL - println!({:#?}, UwU::QwQ);
LL + println!("{:#?}", UwU::QwQ);
|
error: aborting due to 5 previous errors