Skip to content

Commit 5e952e3

Browse files
committed
Add span label to unused string formatting argument
Fix rust-lang#55350.
1 parent 5e75001 commit 5e952e3

File tree

4 files changed

+46
-31
lines changed

4 files changed

+46
-31
lines changed

src/libsyntax_ext/format.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -957,13 +957,18 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt,
957957
let mut diag = {
958958
if errs_len == 1 {
959959
let (sp, msg) = errs.into_iter().next().unwrap();
960-
cx.ecx.struct_span_err(sp, msg)
960+
let mut diag = cx.ecx.struct_span_err(sp, msg);
961+
diag.span_label(sp, msg);
962+
diag
961963
} else {
962964
let mut diag = cx.ecx.struct_span_err(
963965
errs.iter().map(|&(sp, _)| sp).collect::<Vec<Span>>(),
964966
"multiple unused formatting arguments",
965967
);
966968
diag.span_label(cx.fmtsp, "multiple missing formatting specifiers");
969+
for (sp, msg) in errs {
970+
diag.span_label(sp, msg);
971+
}
967972
diag
968973
}
969974
};

src/test/ui/if/ifmt-bad-arg.stderr

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ error: argument never used
1616
--> $DIR/ifmt-bad-arg.rs:9:20
1717
|
1818
LL | format!("{1}", 1);
19-
| ----- ^
19+
| ----- ^ argument never used
2020
| |
2121
| formatting specifier missing
2222

@@ -80,56 +80,58 @@ error: multiple unused formatting arguments
8080
--> $DIR/ifmt-bad-arg.rs:32:17
8181
|
8282
LL | format!("", 1, 2); //~ ERROR: multiple unused formatting arguments
83-
| -- ^ ^
84-
| |
83+
| -- ^ ^ argument never used
84+
| | |
85+
| | argument never used
8586
| multiple missing formatting specifiers
8687

8788
error: argument never used
8889
--> $DIR/ifmt-bad-arg.rs:33:22
8990
|
9091
LL | format!("{}", 1, 2); //~ ERROR: argument never used
91-
| ---- ^
92+
| ---- ^ argument never used
9293
| |
9394
| formatting specifier missing
9495

9596
error: argument never used
9697
--> $DIR/ifmt-bad-arg.rs:34:20
9798
|
9899
LL | format!("{1}", 1, 2); //~ ERROR: argument never used
99-
| ----- ^
100+
| ----- ^ argument never used
100101
| |
101102
| formatting specifier missing
102103

103104
error: named argument never used
104105
--> $DIR/ifmt-bad-arg.rs:35:26
105106
|
106107
LL | format!("{}", 1, foo=2); //~ ERROR: named argument never used
107-
| ---- ^
108+
| ---- ^ named argument never used
108109
| |
109110
| formatting specifier missing
110111

111112
error: argument never used
112113
--> $DIR/ifmt-bad-arg.rs:36:22
113114
|
114115
LL | format!("{foo}", 1, foo=2); //~ ERROR: argument never used
115-
| ------- ^
116+
| ------- ^ argument never used
116117
| |
117118
| formatting specifier missing
118119

119120
error: named argument never used
120121
--> $DIR/ifmt-bad-arg.rs:37:21
121122
|
122123
LL | format!("", foo=2); //~ ERROR: named argument never used
123-
| -- ^
124+
| -- ^ named argument never used
124125
| |
125126
| formatting specifier missing
126127

127128
error: multiple unused formatting arguments
128129
--> $DIR/ifmt-bad-arg.rs:38:32
129130
|
130131
LL | format!("{} {}", 1, 2, foo=1, bar=2); //~ ERROR: multiple unused formatting arguments
131-
| ------- ^ ^
132-
| |
132+
| ------- ^ ^ named argument never used
133+
| | |
134+
| | named argument never used
133135
| multiple missing formatting specifiers
134136

135137
error: duplicate argument named `foo`
@@ -160,7 +162,7 @@ error: named argument never used
160162
--> $DIR/ifmt-bad-arg.rs:45:51
161163
|
162164
LL | format!("{valuea} {valueb}", valuea=5, valuec=7);
163-
| ------------------- ^
165+
| ------------------- ^ named argument never used
164166
| |
165167
| formatting specifier missing
166168

@@ -194,7 +196,7 @@ error: argument never used
194196
--> $DIR/ifmt-bad-arg.rs:56:27
195197
|
196198
LL | format!("foo %s baz", "bar"); //~ ERROR: argument never used
197-
| -- ^^^^^
199+
| -- ^^^^^ argument never used
198200
| |
199201
| help: format specifiers use curly braces: `{}`
200202
|

src/test/ui/macros/format-foreign.stderr

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ error: multiple unused formatting arguments
22
--> $DIR/format-foreign.rs:2:30
33
|
44
LL | println!("%.*3$s %s!/n", "Hello,", "World", 4); //~ ERROR multiple unused formatting arguments
5-
| -------------- ^^^^^^^^ ^^^^^^^ ^
6-
| |
5+
| -------------- ^^^^^^^^ ^^^^^^^ ^ argument never used
6+
| | | |
7+
| | | argument never used
8+
| | argument never used
79
| multiple missing formatting specifiers
810
|
911
= note: printf formatting not supported; see the documentation for `std::fmt`
@@ -16,7 +18,7 @@ error: argument never used
1618
--> $DIR/format-foreign.rs:3:29
1719
|
1820
LL | println!("%1$*2$.*3$f", 123.456); //~ ERROR never used
19-
| ----------- ^^^^^^^
21+
| ----------- ^^^^^^^ argument never used
2022
| |
2123
| help: format specifiers use curly braces: `{0:1$.2$}`
2224
|
@@ -29,8 +31,10 @@ LL | println!(r###"%.*3$s
2931
| ______________-
3032
LL | | %s!/n
3133
LL | | "###, "Hello,", "World", 4);
32-
| | - ^^^^^^^^ ^^^^^^^ ^
33-
| |____|
34+
| | - ^^^^^^^^ ^^^^^^^ ^ argument never used
35+
| | | | |
36+
| | | | argument never used
37+
| |____| argument never used
3438
| multiple missing formatting specifiers
3539
|
3640
= note: printf formatting not supported; see the documentation for `std::fmt`
@@ -44,15 +48,15 @@ error: argument never used
4448
--> $DIR/format-foreign.rs:12:30
4549
|
4650
LL | println!("{} %f", "one", 2.0); //~ ERROR never used
47-
| ------- ^^^
51+
| ------- ^^^ argument never used
4852
| |
4953
| formatting specifier missing
5054

5155
error: named argument never used
5256
--> $DIR/format-foreign.rs:14:39
5357
|
5458
LL | println!("Hi there, $NAME.", NAME="Tim"); //~ ERROR never used
55-
| ----- ^^^^^
59+
| ----- ^^^^^ named argument never used
5660
| |
5761
| help: format specifiers use curly braces: `{NAME}`
5862
|
@@ -62,8 +66,10 @@ error: multiple unused formatting arguments
6266
--> $DIR/format-foreign.rs:15:32
6367
|
6468
LL | println!("$1 $0 $$ $NAME", 1, 2, NAME=3);
65-
| ---------------- ^ ^ ^
66-
| |
69+
| ---------------- ^ ^ ^ named argument never used
70+
| | | |
71+
| | | argument never used
72+
| | argument never used
6773
| multiple missing formatting specifiers
6874
|
6975
= note: shell formatting not supported; see the documentation for `std::fmt`

src/test/ui/macros/format-unused-lables.stderr

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ error: multiple unused formatting arguments
22
--> $DIR/format-unused-lables.rs:2:22
33
|
44
LL | println!("Test", 123, 456, 789);
5-
| ------ ^^^ ^^^ ^^^
6-
| |
5+
| ------ ^^^ ^^^ ^^^ argument never used
6+
| | | |
7+
| | | argument never used
8+
| | argument never used
79
| multiple missing formatting specifiers
810

911
error: multiple unused formatting arguments
@@ -12,17 +14,17 @@ error: multiple unused formatting arguments
1214
LL | println!("Test2",
1315
| ------- multiple missing formatting specifiers
1416
LL | 123, //~ ERROR multiple unused formatting arguments
15-
| ^^^
17+
| ^^^ argument never used
1618
LL | 456,
17-
| ^^^
19+
| ^^^ argument never used
1820
LL | 789
19-
| ^^^
21+
| ^^^ argument never used
2022

2123
error: named argument never used
2224
--> $DIR/format-unused-lables.rs:11:35
2325
|
2426
LL | println!("Some stuff", UNUSED="args"); //~ ERROR named argument never used
25-
| ------------ ^^^^^^
27+
| ------------ ^^^^^^ named argument never used
2628
| |
2729
| formatting specifier missing
2830

@@ -35,12 +37,12 @@ LL | println!("Some more $STUFF",
3537
| | help: format specifiers use curly braces: `{STUFF}`
3638
| multiple missing formatting specifiers
3739
LL | "woo!", //~ ERROR multiple unused formatting arguments
38-
| ^^^^^^
40+
| ^^^^^^ argument never used
3941
LL | STUFF=
4042
LL | "things"
41-
| ^^^^^^^^
43+
| ^^^^^^^^ named argument never used
4244
LL | , UNUSED="args");
43-
| ^^^^^^
45+
| ^^^^^^ named argument never used
4446
|
4547
= note: shell formatting not supported; see the documentation for `std::fmt`
4648

0 commit comments

Comments
 (0)