diff --git a/compiler/rustc_builtin_macros/src/format.rs b/compiler/rustc_builtin_macros/src/format.rs index 9501e93bad534..6a83d98e84019 100644 --- a/compiler/rustc_builtin_macros/src/format.rs +++ b/compiler/rustc_builtin_macros/src/format.rs @@ -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)) + let not_impl_display_trait = |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 = "".to_string(); + for kind in [&efmt.kind] + .into_iter() + .chain(args.explicit_args().into_iter().map(|a| &a.expr.kind)) + { + sugg_fmt.push_str(if not_impl_display_trait(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", diff --git a/tests/ui/macros/format-empty-block-unit-tuple-suggestion-130170.fixed b/tests/ui/macros/format-empty-block-unit-tuple-suggestion-130170.fixed new file mode 100644 index 0000000000000..85a367327d3c9 --- /dev/null +++ b/tests/ui/macros/format-empty-block-unit-tuple-suggestion-130170.fixed @@ -0,0 +1,14 @@ +//@ run-rustfix +#![allow(unused_labels)] + +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 +} diff --git a/tests/ui/macros/format-empty-block-unit-tuple-suggestion-130170.rs b/tests/ui/macros/format-empty-block-unit-tuple-suggestion-130170.rs new file mode 100644 index 0000000000000..fd093153a94ec --- /dev/null +++ b/tests/ui/macros/format-empty-block-unit-tuple-suggestion-130170.rs @@ -0,0 +1,14 @@ +//@ run-rustfix +#![allow(unused_labels)] + +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 +} diff --git a/tests/ui/macros/format-empty-block-unit-tuple-suggestion-130170.stderr b/tests/ui/macros/format-empty-block-unit-tuple-suggestion-130170.stderr new file mode 100644 index 0000000000000..7671150686c01 --- /dev/null +++ b/tests/ui/macros/format-empty-block-unit-tuple-suggestion-130170.stderr @@ -0,0 +1,46 @@ +error: format argument must be a string literal + --> $DIR/format-empty-block-unit-tuple-suggestion-130170.rs:6: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:8: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:10: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:12: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 +