about summary refs log tree commit diff
path: root/compiler/rustc_builtin_macros
diff options
context:
space:
mode:
authorJacob Pratt <jacob@jhpratt.dev>2025-09-13 03:26:03 -0400
committerGitHub <noreply@github.com>2025-09-13 03:26:03 -0400
commitc4539b2d58d97093fc08a42561397fa98e75c684 (patch)
treecdb5ce7ceab304a413cbccd03cf1c176f42b5291 /compiler/rustc_builtin_macros
parent7be418947e858d0b6f52d453ef377cd77de9aeaf (diff)
parent43a6b1041819c5f5b70faf1ad5833400087dc1b0 (diff)
downloadrust-c4539b2d58d97093fc08a42561397fa98e75c684.tar.gz
rust-c4539b2d58d97093fc08a42561397fa98e75c684.zip
Rollup merge of #146456 - IoaNNUwU:issue-146446, r=estebank
Fix panic and incorrectly suggested examples in `format_args` macro.

Follow up on rust-lang/rust#146123
Fixes: rust-lang/rust#146446
r? `@estebank`
Diffstat (limited to 'compiler/rustc_builtin_macros')
-rw-r--r--compiler/rustc_builtin_macros/src/format.rs14
1 files changed, 10 insertions, 4 deletions
diff --git a/compiler/rustc_builtin_macros/src/format.rs b/compiler/rustc_builtin_macros/src/format.rs
index a6c8e7d29cc..d70888205a5 100644
--- a/compiler/rustc_builtin_macros/src/format.rs
+++ b/compiler/rustc_builtin_macros/src/format.rs
@@ -569,6 +569,7 @@ fn make_format_args(
             detect_foreign_fmt,
             str_style,
             fmt_str,
+            uncooked_fmt_str.1.as_str(),
             fmt_span,
         );
     }
@@ -650,6 +651,7 @@ fn report_missing_placeholders(
     detect_foreign_fmt: bool,
     str_style: Option<usize>,
     fmt_str: &str,
+    uncooked_fmt_str: &str,
     fmt_span: Span,
 ) {
     let mut diag = if let &[(span, named)] = &unused[..] {
@@ -773,16 +775,20 @@ fn report_missing_placeholders(
                 diag.note(format!("consider adding {} format specifiers", unused.len()));
             }
         } else {
-            let original_fmt_str =
-                if fmt_str.len() >= 1 { &fmt_str[..fmt_str.len() - 1] } else { "" };
-
             let msg = if unused.len() == 1 {
                 "a format specifier".to_string()
             } else {
                 format!("{} format specifiers", unused.len())
             };
 
-            let sugg = format!("\"{}{}\"", original_fmt_str, "{}".repeat(unused.len()));
+            let sugg = match str_style {
+                None => format!("\"{}{}\"", uncooked_fmt_str, "{}".repeat(unused.len())),
+                Some(n_hashes) => format!(
+                    "r{hashes}\"{uncooked_fmt_str}{fmt_specifiers}\"{hashes}",
+                    hashes = "#".repeat(n_hashes),
+                    fmt_specifiers = "{}".repeat(unused.len())
+                ),
+            };
             let msg = format!("format specifiers use curly braces, consider adding {msg}");
 
             diag.span_suggestion_verbose(fmt_span, msg, sugg, Applicability::MaybeIncorrect);