Skip to content

Commit 8e0b06b

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 rust-lang#130170
1 parent 1b5aa96 commit 8e0b06b

File tree

4 files changed

+75
-4
lines changed

4 files changed

+75
-4
lines changed

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 is_empty_tuple = |kind: &ExprKind| -> bool {
199+
match kind {
200+
ExprKind::Block(b, _) 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 is_empty_tuple(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",
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
error: format argument must be a string literal
2+
--> $DIR/format-issue-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-issue-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-issue-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: aborting due to 3 previous errors
35+

0 commit comments

Comments
 (0)