Skip to content

Commit 689cf08

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 689cf08

4 files changed

+92
-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+
let not_impl_display_trait = |kind: &ExprKind| -> bool {
199+
match kind {
200+
ExprKind::Block(b, None) if b.stmts.is_empty() => true,
201+
ExprKind::Tup(v) if v.is_empty() => true,
202+
_ => false,
202203
}
203204
};
205+
206+
let mut sugg_fmt = "".to_string();
207+
for kind in [&efmt.kind]
208+
.into_iter()
209+
.chain(args.explicit_args().into_iter().map(|a| &a.expr.kind))
210+
{
211+
sugg_fmt.push_str(if not_impl_display_trait(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,14 @@
1+
//@ run-rustfix
2+
#![allow(unused_labels)]
3+
4+
fn main() {
5+
let s = "123";
6+
println!("{:?} {} {}", {}, "sss", s);
7+
//~^ ERROR format argument must be a string literal
8+
println!("{:?}", {});
9+
//~^ ERROR format argument must be a string literal
10+
println!("{} {} {} {:?}", s, "sss", s, {});
11+
//~^ ERROR format argument must be a string literal
12+
println!("{:?} {} {:?}", (), s, {});
13+
//~^ ERROR format argument must be a string literal
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//@ run-rustfix
2+
#![allow(unused_labels)]
3+
4+
fn main() {
5+
let s = "123";
6+
println!({}, "sss", s);
7+
//~^ ERROR format argument must be a string literal
8+
println!({});
9+
//~^ ERROR format argument must be a string literal
10+
println!(s, "sss", s, {});
11+
//~^ ERROR format argument must be a string literal
12+
println!((), s, {});
13+
//~^ ERROR format argument must be a string literal
14+
}
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:6: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:8: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:10: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:12: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)