Skip to content

Commit e9497c0

Browse files
authored
Unrolled build for #146123
Rollup merge of #146123 - IoaNNUwU:issue-68293, r=estebank Suggest examples of format specifiers in error messages Format macro now suggests adding `{}` if no formatting specifiers are present. It also gives an example: ```rust LL | println!("Hello", "World"); | ------- ^^^^^^^ argument never used | | | formatting specifier missing | = note: format specifiers use curly braces: `{}` help: consider adding format specifier | LL | println!("Hello{}", "World"); | ++ ``` When one or more `{}` are present, it doesn't show 'format specifiers use curly braces: `{}`' and example, just small hint on how many you missing: ```rust LL | println!("list: {}", 1, 2, 3); | ---------- ^ ^ argument never used | | | | | argument never used | multiple missing formatting specifiers | = help: consider adding 2 format specifiers ``` Original issue: #68293 Based on discussion in this PR: #76443 Let me know if something is missing
2 parents 565a9ca + 43a6f56 commit e9497c0

File tree

6 files changed

+143
-0
lines changed

6 files changed

+143
-0
lines changed

compiler/rustc_builtin_macros/src/format.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,7 @@ fn make_format_args(
565565
&used,
566566
&args,
567567
&pieces,
568+
&invalid_refs,
568569
detect_foreign_fmt,
569570
str_style,
570571
fmt_str,
@@ -645,6 +646,7 @@ fn report_missing_placeholders(
645646
used: &[bool],
646647
args: &FormatArguments,
647648
pieces: &[parse::Piece<'_>],
649+
invalid_refs: &[(usize, Option<Span>, PositionUsedAs, FormatArgPositionKind)],
648650
detect_foreign_fmt: bool,
649651
str_style: Option<usize>,
650652
fmt_str: &str,
@@ -762,6 +764,31 @@ fn report_missing_placeholders(
762764
diag.span_label(fmt_span, "formatting specifier missing");
763765
}
764766

767+
if !found_foreign && invalid_refs.is_empty() {
768+
// Show example if user didn't use any format specifiers
769+
let show_example = used.iter().all(|used| !used);
770+
771+
if !show_example {
772+
if unused.len() > 1 {
773+
diag.note(format!("consider adding {} format specifiers", unused.len()));
774+
}
775+
} else {
776+
let original_fmt_str =
777+
if fmt_str.len() >= 1 { &fmt_str[..fmt_str.len() - 1] } else { "" };
778+
779+
let msg = if unused.len() == 1 {
780+
"a format specifier".to_string()
781+
} else {
782+
format!("{} format specifiers", unused.len())
783+
};
784+
785+
let sugg = format!("\"{}{}\"", original_fmt_str, "{}".repeat(unused.len()));
786+
let msg = format!("format specifiers use curly braces, consider adding {msg}");
787+
788+
diag.span_suggestion_verbose(fmt_span, msg, sugg, Applicability::MaybeIncorrect);
789+
}
790+
}
791+
765792
diag.emit();
766793
}
767794

tests/ui/fmt/ifmt-bad-arg.stderr

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ LL | format!("", 1, 2);
6262
| | |
6363
| | argument never used
6464
| multiple missing formatting specifiers
65+
|
66+
help: format specifiers use curly braces, consider adding 2 format specifiers
67+
|
68+
LL | format!("{}{}", 1, 2);
69+
| ++++
6570

6671
error: argument never used
6772
--> $DIR/ifmt-bad-arg.rs:33:22
@@ -102,6 +107,11 @@ LL | format!("", foo=2);
102107
| -- ^ named argument never used
103108
| |
104109
| formatting specifier missing
110+
|
111+
help: format specifiers use curly braces, consider adding a format specifier
112+
|
113+
LL | format!("{}", foo=2);
114+
| ++
105115

106116
error: multiple unused formatting arguments
107117
--> $DIR/ifmt-bad-arg.rs:38:32
@@ -111,6 +121,8 @@ LL | format!("{} {}", 1, 2, foo=1, bar=2);
111121
| | |
112122
| | named argument never used
113123
| multiple missing formatting specifiers
124+
|
125+
= note: consider adding 2 format specifiers
114126

115127
error: duplicate argument named `foo`
116128
--> $DIR/ifmt-bad-arg.rs:40:29

tests/ui/macros/format-unused-lables.stderr

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ LL | println!("Test", 123, 456, 789);
77
| | | argument never used
88
| | argument never used
99
| multiple missing formatting specifiers
10+
|
11+
help: format specifiers use curly braces, consider adding 3 format specifiers
12+
|
13+
LL | println!("Test{}{}{}", 123, 456, 789);
14+
| ++++++
1015

1116
error: multiple unused formatting arguments
1217
--> $DIR/format-unused-lables.rs:6:9
@@ -19,6 +24,11 @@ LL | 456,
1924
| ^^^ argument never used
2025
LL | 789
2126
| ^^^ argument never used
27+
|
28+
help: format specifiers use curly braces, consider adding 3 format specifiers
29+
|
30+
LL | println!("Test2{}{}{}",
31+
| ++++++
2232

2333
error: named argument never used
2434
--> $DIR/format-unused-lables.rs:11:35
@@ -27,6 +37,11 @@ LL | println!("Some stuff", UNUSED="args");
2737
| ------------ ^^^^^^ named argument never used
2838
| |
2939
| formatting specifier missing
40+
|
41+
help: format specifiers use curly braces, consider adding a format specifier
42+
|
43+
LL | println!("Some stuff{}", UNUSED="args");
44+
| ++
3045

3146
error: multiple unused formatting arguments
3247
--> $DIR/format-unused-lables.rs:14:9

tests/ui/mir/unsized-extern-static.stderr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ LL | println!("C", unsafe { &symbol });
55
| --- ^^^^^^^^^^^^^^^^^^ argument never used
66
| |
77
| formatting specifier missing
8+
|
9+
help: format specifiers use curly braces, consider adding a format specifier
10+
|
11+
LL | println!("C{}", unsafe { &symbol });
12+
| ++
813

914
error[E0277]: the size for values of type `[i8]` cannot be known at compilation time
1015
--> $DIR/unsized-extern-static.rs:6:5
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
fn no_format_specifier_two_unused_args() {
2+
println!("Hello", "World");
3+
//~^ ERROR argument never used
4+
//~| NOTE formatting specifier missing
5+
//~| NOTE argument never used
6+
//~| HELP format specifiers use curly braces, consider adding a format specifier
7+
}
8+
9+
fn no_format_specifier_multiple_unused_args() {
10+
println!("list: ", 1, 2, 3);
11+
//~^ ERROR multiple unused formatting arguments
12+
//~| NOTE multiple missing formatting specifiers
13+
//~| NOTE argument never used
14+
//~| NOTE argument never used
15+
//~| NOTE argument never used
16+
//~| HELP format specifiers use curly braces, consider adding 3 format specifiers
17+
}
18+
19+
fn missing_format_specifiers_one_unused_arg() {
20+
println!("list: {}, {}", 1, 2, 3);
21+
//~^ ERROR argument never used
22+
//~| NOTE formatting specifier missing
23+
//~| NOTE argument never used
24+
}
25+
26+
fn missing_format_specifiers_multiple_unused_args() {
27+
println!("list: {}", 1, 2, 3);
28+
//~^ ERROR multiple unused formatting arguments
29+
//~| NOTE multiple missing formatting specifiers
30+
//~| NOTE argument never used
31+
//~| NOTE argument never used
32+
//~| NOTE consider adding 2 format specifiers
33+
}
34+
35+
fn main() { }
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
error: argument never used
2+
--> $DIR/missing-format-specifiers-issue-68293.rs:2:23
3+
|
4+
LL | println!("Hello", "World");
5+
| ------- ^^^^^^^ argument never used
6+
| |
7+
| formatting specifier missing
8+
|
9+
help: format specifiers use curly braces, consider adding a format specifier
10+
|
11+
LL | println!("Hello{}", "World");
12+
| ++
13+
14+
error: multiple unused formatting arguments
15+
--> $DIR/missing-format-specifiers-issue-68293.rs:10:24
16+
|
17+
LL | println!("list: ", 1, 2, 3);
18+
| -------- ^ ^ ^ argument never used
19+
| | | |
20+
| | | argument never used
21+
| | argument never used
22+
| multiple missing formatting specifiers
23+
|
24+
help: format specifiers use curly braces, consider adding 3 format specifiers
25+
|
26+
LL | println!("list: {}{}{}", 1, 2, 3);
27+
| ++++++
28+
29+
error: argument never used
30+
--> $DIR/missing-format-specifiers-issue-68293.rs:20:36
31+
|
32+
LL | println!("list: {}, {}", 1, 2, 3);
33+
| -------------- ^ argument never used
34+
| |
35+
| formatting specifier missing
36+
37+
error: multiple unused formatting arguments
38+
--> $DIR/missing-format-specifiers-issue-68293.rs:27:29
39+
|
40+
LL | println!("list: {}", 1, 2, 3);
41+
| ---------- ^ ^ argument never used
42+
| | |
43+
| | argument never used
44+
| multiple missing formatting specifiers
45+
|
46+
= note: consider adding 2 format specifiers
47+
48+
error: aborting due to 4 previous errors
49+

0 commit comments

Comments
 (0)