Skip to content

Commit ceced53

Browse files
committed
Special treatment empty tuple when suggest adding a string literal in format macro.
For example: ```rust let s = "123"; println!({}, "sss", s); ``` Suggest: `println!("{:?} {} {}", {}, "sss", s);` fixes #130170
1 parent 1b5aa96 commit ceced53

4 files changed

+90
-4
lines changed

Diff for: compiler/rustc_builtin_macros/src/format.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,26 @@ fn make_format_args(
195195
Applicability::MaybeIncorrect,
196196
);
197197
} else {
198-
let sugg_fmt = match args.explicit_args().len() {
199-
0 => "{}".to_string(),
200-
count => {
201-
format!("{}{{}}", "{} ".repeat(count))
198+
// `{}` or `()`
199+
let should_suggest = |kind: &ExprKind| -> bool {
200+
match kind {
201+
ExprKind::Block(b, None) if b.stmts.is_empty() => true,
202+
ExprKind::Tup(v) if v.is_empty() => true,
203+
_ => false,
202204
}
203205
};
206+
207+
let mut sugg_fmt = String::new();
208+
for kind in std::iter::once(&efmt.kind)
209+
.chain(args.explicit_args().into_iter().map(|a| &a.expr.kind))
210+
{
211+
sugg_fmt.push_str(if should_suggest(kind) {
212+
"{:?} "
213+
} else {
214+
"{} "
215+
});
216+
}
217+
sugg_fmt = sugg_fmt.trim_end().to_string();
204218
err.span_suggestion(
205219
unexpanded_fmt_span.shrink_to_lo(),
206220
"you might be missing a string literal to format with",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//@ run-rustfix
2+
3+
fn main() {
4+
let s = "123";
5+
println!("{:?} {} {}", {}, "sss", s);
6+
//~^ ERROR format argument must be a string literal
7+
println!("{:?}", {});
8+
//~^ ERROR format argument must be a string literal
9+
println!("{} {} {} {:?}", s, "sss", s, {});
10+
//~^ ERROR format argument must be a string literal
11+
println!("{:?} {} {:?}", (), s, {});
12+
//~^ ERROR format argument must be a string literal
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//@ run-rustfix
2+
3+
fn main() {
4+
let s = "123";
5+
println!({}, "sss", s);
6+
//~^ ERROR format argument must be a string literal
7+
println!({});
8+
//~^ ERROR format argument must be a string literal
9+
println!(s, "sss", s, {});
10+
//~^ ERROR format argument must be a string literal
11+
println!((), s, {});
12+
//~^ ERROR format argument must be a string literal
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
error: format argument must be a string literal
2+
--> $DIR/format-empty-block-unit-tuple-suggestion-130170.rs:5:14
3+
|
4+
LL | println!({}, "sss", s);
5+
| ^^
6+
|
7+
help: you might be missing a string literal to format with
8+
|
9+
LL | println!("{:?} {} {}", {}, "sss", s);
10+
| +++++++++++++
11+
12+
error: format argument must be a string literal
13+
--> $DIR/format-empty-block-unit-tuple-suggestion-130170.rs:7:14
14+
|
15+
LL | println!({});
16+
| ^^
17+
|
18+
help: you might be missing a string literal to format with
19+
|
20+
LL | println!("{:?}", {});
21+
| +++++++
22+
23+
error: format argument must be a string literal
24+
--> $DIR/format-empty-block-unit-tuple-suggestion-130170.rs:9:14
25+
|
26+
LL | println!(s, "sss", s, {});
27+
| ^
28+
|
29+
help: you might be missing a string literal to format with
30+
|
31+
LL | println!("{} {} {} {:?}", s, "sss", s, {});
32+
| ++++++++++++++++
33+
34+
error: format argument must be a string literal
35+
--> $DIR/format-empty-block-unit-tuple-suggestion-130170.rs:11:14
36+
|
37+
LL | println!((), s, {});
38+
| ^^
39+
|
40+
help: you might be missing a string literal to format with
41+
|
42+
LL | println!("{:?} {} {:?}", (), s, {});
43+
| +++++++++++++++
44+
45+
error: aborting due to 4 previous errors
46+

0 commit comments

Comments
 (0)