Skip to content

Commit 530e8de

Browse files
authored
Unrolled build for #146456
Rollup merge of #146456 - IoaNNUwU:issue-146446, r=estebank Fix panic and incorrectly suggested examples in `format_args` macro. Follow up on #146123 Fixes: #146446 r? `@estebank`
2 parents 064cc81 + 43a6b10 commit 530e8de

File tree

3 files changed

+87
-5
lines changed

3 files changed

+87
-5
lines changed

compiler/rustc_builtin_macros/src/format.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ fn make_format_args(
569569
detect_foreign_fmt,
570570
str_style,
571571
fmt_str,
572+
uncooked_fmt_str.1.as_str(),
572573
fmt_span,
573574
);
574575
}
@@ -650,6 +651,7 @@ fn report_missing_placeholders(
650651
detect_foreign_fmt: bool,
651652
str_style: Option<usize>,
652653
fmt_str: &str,
654+
uncooked_fmt_str: &str,
653655
fmt_span: Span,
654656
) {
655657
let mut diag = if let &[(span, named)] = &unused[..] {
@@ -773,16 +775,20 @@ fn report_missing_placeholders(
773775
diag.note(format!("consider adding {} format specifiers", unused.len()));
774776
}
775777
} else {
776-
let original_fmt_str =
777-
if fmt_str.len() >= 1 { &fmt_str[..fmt_str.len() - 1] } else { "" };
778-
779778
let msg = if unused.len() == 1 {
780779
"a format specifier".to_string()
781780
} else {
782781
format!("{} format specifiers", unused.len())
783782
};
784783

785-
let sugg = format!("\"{}{}\"", original_fmt_str, "{}".repeat(unused.len()));
784+
let sugg = match str_style {
785+
None => format!("\"{}{}\"", uncooked_fmt_str, "{}".repeat(unused.len())),
786+
Some(n_hashes) => format!(
787+
"r{hashes}\"{uncooked_fmt_str}{fmt_specifiers}\"{hashes}",
788+
hashes = "#".repeat(n_hashes),
789+
fmt_specifiers = "{}".repeat(unused.len())
790+
),
791+
};
786792
let msg = format!("format specifiers use curly braces, consider adding {msg}");
787793

788794
diag.span_suggestion_verbose(fmt_span, msg, sugg, Applicability::MaybeIncorrect);

tests/ui/suggestions/missing-format-specifiers-issue-68293.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,33 @@ fn missing_format_specifiers_multiple_unused_args() {
3232
//~| NOTE consider adding 2 format specifiers
3333
}
3434

35+
fn unicode_unused_args() {
36+
panic!("👆", "👆", 1);
37+
//~^ ERROR multiple unused formatting arguments
38+
//~| NOTE multiple missing formatting specifiers
39+
//~| NOTE argument never used
40+
//~| NOTE argument never used
41+
//~| HELP format specifiers use curly braces, consider adding 2 format specifiers
42+
}
43+
44+
fn raw_str_unused_arg() {
45+
format_args!(r##"lJ𐏿Æ�.𐏿�"##, r#"r}J𐏿Æ" {}"#, 1);
46+
//~^ ERROR multiple unused formatting arguments
47+
//~| NOTE multiple missing formatting specifiers
48+
//~| NOTE argument never used
49+
//~| NOTE argument never used
50+
//~| HELP format specifiers use curly braces, consider adding 2 format specifiers
51+
}
52+
53+
fn valid_new_lines_unused_args() {
54+
panic!("Expect 2 newlines
55+
56+
", "👆", 1);
57+
//~^ ERROR multiple unused formatting arguments
58+
//~| NOTE argument never used
59+
//~| NOTE argument never used
60+
//~^^^^^^ NOTE multiple missing formatting specifiers
61+
//~| HELP format specifiers use curly braces, consider adding 2 format specifiers
62+
}
63+
3564
fn main() { }

tests/ui/suggestions/missing-format-specifiers-issue-68293.stderr

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,52 @@ LL | println!("list: {}", 1, 2, 3);
4545
|
4646
= note: consider adding 2 format specifiers
4747

48-
error: aborting due to 4 previous errors
48+
error: multiple unused formatting arguments
49+
--> $DIR/missing-format-specifiers-issue-68293.rs:36:17
50+
|
51+
LL | panic!("👆", "👆", 1);
52+
| ---- ^^^^ ^ argument never used
53+
| | |
54+
| | argument never used
55+
| multiple missing formatting specifiers
56+
|
57+
help: format specifiers use curly braces, consider adding 2 format specifiers
58+
|
59+
LL | panic!("👆{}{}", "👆", 1);
60+
| ++++
61+
62+
error: multiple unused formatting arguments
63+
--> $DIR/missing-format-specifiers-issue-68293.rs:45:35
64+
|
65+
LL | format_args!(r##"lJ𐏿Æ�.𐏿�"##, r#"r}J𐏿Æ" {}"#, 1);
66+
| --------------- ^^^^^^^^^^^^^^ ^ argument never used
67+
| | |
68+
| | argument never used
69+
| multiple missing formatting specifiers
70+
|
71+
help: format specifiers use curly braces, consider adding 2 format specifiers
72+
|
73+
LL | format_args!(r##"lJ𐏿Æ�.𐏿�{}{}"##, r#"r}J𐏿Æ" {}"#, 1);
74+
| ++++
75+
76+
error: multiple unused formatting arguments
77+
--> $DIR/missing-format-specifiers-issue-68293.rs:56:4
78+
|
79+
LL | panic!("Expect 2 newlines
80+
| ____________-
81+
LL | |
82+
LL | | ", "👆", 1);
83+
| | - ^^^^ ^ argument never used
84+
| | | |
85+
| |_| argument never used
86+
| multiple missing formatting specifiers
87+
|
88+
help: format specifiers use curly braces, consider adding 2 format specifiers
89+
|
90+
LL | panic!("Expect 2 newlines
91+
LL |
92+
LL ~ {}{}", "👆", 1);
93+
|
94+
95+
error: aborting due to 7 previous errors
4996

0 commit comments

Comments
 (0)