Rollup merge of #131430 - surechen:fix_130495, r=jieyouxu
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
This commit is contained in:
commit
75231f8764
@ -195,12 +195,26 @@ fn make_format_args(
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
} else {
|
||||
let sugg_fmt = match args.explicit_args().len() {
|
||||
0 => "{}".to_string(),
|
||||
count => {
|
||||
format!("{}{{}}", "{} ".repeat(count))
|
||||
// `{}` or `()`
|
||||
let should_suggest = |kind: &ExprKind| -> bool {
|
||||
match kind {
|
||||
ExprKind::Block(b, None) if b.stmts.is_empty() => true,
|
||||
ExprKind::Tup(v) if v.is_empty() => true,
|
||||
_ => false,
|
||||
}
|
||||
};
|
||||
|
||||
let mut sugg_fmt = String::new();
|
||||
for kind in std::iter::once(&efmt.kind)
|
||||
.chain(args.explicit_args().into_iter().map(|a| &a.expr.kind))
|
||||
{
|
||||
sugg_fmt.push_str(if should_suggest(kind) {
|
||||
"{:?} "
|
||||
} else {
|
||||
"{} "
|
||||
});
|
||||
}
|
||||
sugg_fmt = sugg_fmt.trim_end().to_string();
|
||||
err.span_suggestion(
|
||||
unexpanded_fmt_span.shrink_to_lo(),
|
||||
"you might be missing a string literal to format with",
|
||||
|
@ -0,0 +1,13 @@
|
||||
//@ run-rustfix
|
||||
|
||||
fn main() {
|
||||
let s = "123";
|
||||
println!("{:?} {} {}", {}, "sss", s);
|
||||
//~^ ERROR format argument must be a string literal
|
||||
println!("{:?}", {});
|
||||
//~^ ERROR format argument must be a string literal
|
||||
println!("{} {} {} {:?}", s, "sss", s, {});
|
||||
//~^ ERROR format argument must be a string literal
|
||||
println!("{:?} {} {:?}", (), s, {});
|
||||
//~^ ERROR format argument must be a string literal
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
//@ run-rustfix
|
||||
|
||||
fn main() {
|
||||
let s = "123";
|
||||
println!({}, "sss", s);
|
||||
//~^ ERROR format argument must be a string literal
|
||||
println!({});
|
||||
//~^ ERROR format argument must be a string literal
|
||||
println!(s, "sss", s, {});
|
||||
//~^ ERROR format argument must be a string literal
|
||||
println!((), s, {});
|
||||
//~^ ERROR format argument must be a string literal
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
error: format argument must be a string literal
|
||||
--> $DIR/format-empty-block-unit-tuple-suggestion-130170.rs:5:14
|
||||
|
|
||||
LL | println!({}, "sss", s);
|
||||
| ^^
|
||||
|
|
||||
help: you might be missing a string literal to format with
|
||||
|
|
||||
LL | println!("{:?} {} {}", {}, "sss", s);
|
||||
| +++++++++++++
|
||||
|
||||
error: format argument must be a string literal
|
||||
--> $DIR/format-empty-block-unit-tuple-suggestion-130170.rs:7:14
|
||||
|
|
||||
LL | println!({});
|
||||
| ^^
|
||||
|
|
||||
help: you might be missing a string literal to format with
|
||||
|
|
||||
LL | println!("{:?}", {});
|
||||
| +++++++
|
||||
|
||||
error: format argument must be a string literal
|
||||
--> $DIR/format-empty-block-unit-tuple-suggestion-130170.rs:9:14
|
||||
|
|
||||
LL | println!(s, "sss", s, {});
|
||||
| ^
|
||||
|
|
||||
help: you might be missing a string literal to format with
|
||||
|
|
||||
LL | println!("{} {} {} {:?}", s, "sss", s, {});
|
||||
| ++++++++++++++++
|
||||
|
||||
error: format argument must be a string literal
|
||||
--> $DIR/format-empty-block-unit-tuple-suggestion-130170.rs:11:14
|
||||
|
|
||||
LL | println!((), s, {});
|
||||
| ^^
|
||||
|
|
||||
help: you might be missing a string literal to format with
|
||||
|
|
||||
LL | println!("{:?} {} {:?}", (), s, {});
|
||||
| +++++++++++++++
|
||||
|
||||
error: aborting due to 4 previous errors
|
||||
|
Loading…
Reference in New Issue
Block a user