From e656e52ccb33f92ca8bfd7ad78ee5b028ff61a62 Mon Sep 17 00:00:00 2001 From: IoaNNUwU Date: Tue, 2 Sep 2025 16:15:23 +0200 Subject: Suggest examples of format specifiers in error messages --- compiler/rustc_builtin_macros/src/format.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'compiler/rustc_builtin_macros/src') diff --git a/compiler/rustc_builtin_macros/src/format.rs b/compiler/rustc_builtin_macros/src/format.rs index 6415e55e0b0..ce9bda10721 100644 --- a/compiler/rustc_builtin_macros/src/format.rs +++ b/compiler/rustc_builtin_macros/src/format.rs @@ -758,8 +758,31 @@ fn report_missing_placeholders( check_foreign!(shell); } } - if !found_foreign && unused.len() == 1 { - diag.span_label(fmt_span, "formatting specifier missing"); + if !found_foreign { + if unused.len() == 1 { + diag.span_label(fmt_span, "formatting specifier missing"); + } + if used.iter().all(|used| !used) { + diag.note("format specifiers use curly braces: `{}`"); + } + + let mut suggest_fixed_fmt = format!("\"{}", &fmt_str[..fmt_str.len() - 1]); + for _ in &unused { + suggest_fixed_fmt.push_str("{}"); + } + suggest_fixed_fmt.push('"'); + + let suggest_fmt_count = if unused.len() == 1 { + "consider adding format specifier".to_string() + } else { + format!("consider adding {} format specifiers", unused.len()) + }; + diag.span_suggestion_verbose( + fmt_span, + suggest_fmt_count, + suggest_fixed_fmt, + Applicability::MaybeIncorrect, + ); } diag.emit(); -- cgit 1.4.1-3-g733a5