diff options
| author | IoaNNUwU <ioannxd@gmail.com> | 2025-09-02 16:15:23 +0200 |
|---|---|---|
| committer | IoaNNUwU <ioannxd@gmail.com> | 2025-09-08 17:35:40 +0200 |
| commit | e656e52ccb33f92ca8bfd7ad78ee5b028ff61a62 (patch) | |
| tree | 0ea81614cad67aac5640c816271794fbfabd8317 /compiler/rustc_builtin_macros | |
| parent | beeb8e3af54295ba494c250e84ecda4c2c5d85ff (diff) | |
| download | rust-e656e52ccb33f92ca8bfd7ad78ee5b028ff61a62.tar.gz rust-e656e52ccb33f92ca8bfd7ad78ee5b028ff61a62.zip | |
Suggest examples of format specifiers in error messages
Diffstat (limited to 'compiler/rustc_builtin_macros')
| -rw-r--r-- | compiler/rustc_builtin_macros/src/format.rs | 27 |
1 files changed, 25 insertions, 2 deletions
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(); |
