Skip to content

Commit 2c16f9e

Browse files
committed
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
1 parent 2eaa5de commit 2c16f9e

5 files changed

Lines changed: 136 additions & 3 deletions

File tree

compiler/rustc_builtin_macros/src/format.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,16 @@ fn make_format_args(
173173
style: fmt_style,
174174
uncooked_symbol: uncooked_fmt_str,
175175
} = {
176+
// Extract snippet so that we can check cases `{}`, `{:?}` and `{:#?}` and emit help for
177+
// them later.
178+
let snippet = if let ExprKind::Block(b, None) = &efmt.kind
179+
&& b.stmts.len() <= 1
180+
{
181+
Some(ecx.sess.source_map().span_to_snippet(unexpanded_fmt_span))
182+
} else {
183+
None
184+
};
185+
176186
let ExpandResult::Ready(mac) = expr_to_spanned_string(ecx, efmt.clone(), msg) else {
177187
return ExpandResult::Retry(());
178188
};
@@ -222,12 +232,26 @@ fn make_format_args(
222232
});
223233
}
224234
sugg_fmt = sugg_fmt.trim_end().to_string();
225-
err.span_suggestion(
235+
err.span_suggestion_verbose(
226236
unexpanded_fmt_span.shrink_to_lo(),
227237
"you might be missing a string literal to format with",
228238
format!("\"{sugg_fmt}\", "),
229239
Applicability::MaybeIncorrect,
230240
);
241+
242+
if let Some(Ok(snippet)) = snippet.as_ref() {
243+
match snippet.as_str() {
244+
"{}" | "{:?}" | "{:#?}" => {
245+
err.span_suggestion_verbose(
246+
unexpanded_fmt_span,
247+
format!("you might want to enclose `{snippet}` with `\"\"`"),
248+
format!("\"{snippet}\""),
249+
Applicability::MaybeIncorrect,
250+
);
251+
}
252+
_ => {}
253+
};
254+
}
231255
}
232256
}
233257
err.emit()

tests/ui/macros/format-empty-block-unit-tuple-suggestion-130170.fixed

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
fn main() {
44
let s = "123";
5-
println!("{:?} {} {}", {}, "sss", s);
5+
println!("{:?} {} {}", "{}", "sss", s);
66
//~^ ERROR format argument must be a string literal
7-
println!("{:?}", {});
7+
println!("{:?}", "{}");
88
//~^ ERROR format argument must be a string literal
99
println!("{} {} {} {:?}", s, "sss", s, {});
1010
//~^ ERROR format argument must be a string literal

tests/ui/macros/format-empty-block-unit-tuple-suggestion-130170.stderr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ help: you might be missing a string literal to format with
88
|
99
LL | println!("{:?} {} {}", {}, "sss", s);
1010
| +++++++++++++
11+
help: you might want to enclose `{}` with `""`
12+
|
13+
LL - println!({}, "sss", s);
14+
LL + println!("{}", "sss", s);
15+
|
1116

1217
error: format argument must be a string literal
1318
--> $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
1924
|
2025
LL | println!("{:?}", {});
2126
| +++++++
27+
help: you might want to enclose `{}` with `""`
28+
|
29+
LL - println!({});
30+
LL + println!("{}");
31+
|
2232

2333
error: format argument must be a string literal
2434
--> $DIR/format-empty-block-unit-tuple-suggestion-130170.rs:9:14
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Suggest enclosing the format string with `""` when it is one of `{}`, `{:?}`, and `{:#?}`.
2+
3+
#[derive(Debug)]
4+
enum UwU {
5+
QwQ,
6+
AwA,
7+
QAQ,
8+
}
9+
10+
fn main() {
11+
println!({}, UwU::QwQ);
12+
//~^ ERROR format argument must be a string literal
13+
//~| HELP you might be missing a string literal to format with
14+
//~| HELP you might want to enclose `{}` with `""`
15+
println!({:?}, UwU::QwQ);
16+
//~^ ERROR expected expression, found `:`
17+
//~| ERROR format argument must be a string literal
18+
//~| HELP you might be missing a string literal to format with
19+
//~| HELP maybe write a path separator here
20+
//~| HELP you might want to enclose `{:?}` with `""`
21+
println!({:#?}, UwU::QwQ);
22+
//~^ ERROR expected expression, found `:`
23+
//~| ERROR format argument must be a string literal
24+
//~| HELP you might be missing a string literal to format with
25+
//~| HELP maybe write a path separator here
26+
//~| HELP you might want to enclose `{:#?}` with `""`
27+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
error: format argument must be a string literal
2+
--> $DIR/suggest-enclosing-format-string.rs:11:14
3+
|
4+
LL | println!({}, UwU::QwQ);
5+
| ^^
6+
|
7+
help: you might be missing a string literal to format with
8+
|
9+
LL | println!("{:?} {}", {}, UwU::QwQ);
10+
| ++++++++++
11+
help: you might want to enclose `{}` with `""`
12+
|
13+
LL - println!({}, UwU::QwQ);
14+
LL + println!("{}", UwU::QwQ);
15+
|
16+
17+
error: expected expression, found `:`
18+
--> $DIR/suggest-enclosing-format-string.rs:15:15
19+
|
20+
LL | println!({:?}, UwU::QwQ);
21+
| ^ expected expression
22+
|
23+
help: maybe write a path separator here
24+
|
25+
LL | println!({::?}, UwU::QwQ);
26+
| +
27+
28+
error: format argument must be a string literal
29+
--> $DIR/suggest-enclosing-format-string.rs:15:14
30+
|
31+
LL | println!({:?}, UwU::QwQ);
32+
| ^^^^
33+
|
34+
help: you might be missing a string literal to format with
35+
|
36+
LL | println!("{} {}", {:?}, UwU::QwQ);
37+
| ++++++++
38+
help: you might want to enclose `{:?}` with `""`
39+
|
40+
LL - println!({:?}, UwU::QwQ);
41+
LL + println!("{:?}", UwU::QwQ);
42+
|
43+
44+
error: expected expression, found `:`
45+
--> $DIR/suggest-enclosing-format-string.rs:21:15
46+
|
47+
LL | println!({:#?}, UwU::QwQ);
48+
| ^ expected expression
49+
|
50+
help: maybe write a path separator here
51+
|
52+
LL | println!({::#?}, UwU::QwQ);
53+
| +
54+
55+
error: format argument must be a string literal
56+
--> $DIR/suggest-enclosing-format-string.rs:21:14
57+
|
58+
LL | println!({:#?}, UwU::QwQ);
59+
| ^^^^^
60+
|
61+
help: you might be missing a string literal to format with
62+
|
63+
LL | println!("{} {}", {:#?}, UwU::QwQ);
64+
| ++++++++
65+
help: you might want to enclose `{:#?}` with `""`
66+
|
67+
LL - println!({:#?}, UwU::QwQ);
68+
LL + println!("{:#?}", UwU::QwQ);
69+
|
70+
71+
error: aborting due to 5 previous errors
72+

0 commit comments

Comments
 (0)