Skip to content

Commit 9a893cc

Browse files
committed
Add span label for format str missing specifier
1 parent 7bd94e0 commit 9a893cc

File tree

4 files changed

+42
-21
lines changed

4 files changed

+42
-21
lines changed

src/libsyntax_ext/format.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -915,12 +915,13 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt,
915915
errs.push((cx.args[i].span, msg));
916916
}
917917
}
918-
if errs.len() > 0 {
919-
let args_used = cx.arg_types.len() - errs.len();
920-
let args_unused = errs.len();
918+
let errs_len = errs.len();
919+
if errs_len > 0 {
920+
let args_used = cx.arg_types.len() - errs_len;
921+
let args_unused = errs_len;
921922

922923
let mut diag = {
923-
if errs.len() == 1 {
924+
if errs_len == 1 {
924925
let (sp, msg) = errs.into_iter().next().unwrap();
925926
cx.ecx.struct_span_err(sp, msg)
926927
} else {
@@ -933,6 +934,8 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt,
933934
}
934935
};
935936

937+
// Used to ensure we only report translations for *one* kind of foreign format.
938+
let mut found_foreign = false;
936939
// Decide if we want to look for foreign formatting directives.
937940
if args_used < args_unused {
938941
use super::format_foreign as foreign;
@@ -941,9 +944,6 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt,
941944
// with `%d should be written as {}` over and over again.
942945
let mut explained = HashSet::new();
943946

944-
// Used to ensure we only report translations for *one* kind of foreign format.
945-
let mut found_foreign = false;
946-
947947
macro_rules! check_foreign {
948948
($kind:ident) => {{
949949
let mut show_doc_note = false;
@@ -987,7 +987,7 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt,
987987
}
988988
if suggestions.len() > 0 {
989989
diag.multipart_suggestion(
990-
"format specifiers in Rust are written using `{}`",
990+
"format specifiers use curly braces",
991991
suggestions,
992992
);
993993
}
@@ -999,6 +999,9 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt,
999999
check_foreign!(shell);
10001000
}
10011001
}
1002+
if !found_foreign && errs_len == 1 {
1003+
diag.span_label(cx.fmtsp, "formatting specifier missing");
1004+
}
10021005

10031006
diag.emit();
10041007
}

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

+22-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ error: argument never used
1616
--> $DIR/ifmt-bad-arg.rs:19:20
1717
|
1818
LL | format!("{1}", 1);
19-
| ^
19+
| ----- ^
20+
| |
21+
| formatting specifier missing
2022

2123
error: 2 positional arguments in format string, but no arguments were given
2224
--> $DIR/ifmt-bad-arg.rs:23:14
@@ -86,31 +88,41 @@ error: argument never used
8688
--> $DIR/ifmt-bad-arg.rs:43:22
8789
|
8890
LL | format!("{}", 1, 2); //~ ERROR: argument never used
89-
| ^
91+
| ---- ^
92+
| |
93+
| formatting specifier missing
9094

9195
error: argument never used
9296
--> $DIR/ifmt-bad-arg.rs:44:20
9397
|
9498
LL | format!("{1}", 1, 2); //~ ERROR: argument never used
95-
| ^
99+
| ----- ^
100+
| |
101+
| formatting specifier missing
96102

97103
error: named argument never used
98104
--> $DIR/ifmt-bad-arg.rs:45:26
99105
|
100106
LL | format!("{}", 1, foo=2); //~ ERROR: named argument never used
101-
| ^
107+
| ---- ^
108+
| |
109+
| formatting specifier missing
102110

103111
error: argument never used
104112
--> $DIR/ifmt-bad-arg.rs:46:22
105113
|
106114
LL | format!("{foo}", 1, foo=2); //~ ERROR: argument never used
107-
| ^
115+
| ------- ^
116+
| |
117+
| formatting specifier missing
108118

109119
error: named argument never used
110120
--> $DIR/ifmt-bad-arg.rs:47:21
111121
|
112122
LL | format!("", foo=2); //~ ERROR: named argument never used
113-
| ^
123+
| -- ^
124+
| |
125+
| formatting specifier missing
114126

115127
error: multiple unused formatting arguments
116128
--> $DIR/ifmt-bad-arg.rs:48:32
@@ -148,7 +160,9 @@ error: named argument never used
148160
--> $DIR/ifmt-bad-arg.rs:55:51
149161
|
150162
LL | format!("{valuea} {valueb}", valuea=5, valuec=7);
151-
| ^
163+
| ------------------- ^
164+
| |
165+
| formatting specifier missing
152166

153167
error: invalid format string: expected `'}'` but string was terminated
154168
--> $DIR/ifmt-bad-arg.rs:61:15
@@ -180,7 +194,7 @@ error: argument never used
180194
LL | format!("foo %s baz", "bar"); //~ ERROR: argument never used
181195
| -- ^^^^^
182196
| |
183-
| help: format specifiers in Rust are written using `{}`: `{}`
197+
| help: format specifiers use curly braces: `{}`
184198
|
185199
= note: printf formatting not supported; see the documentation for `std::fmt`
186200

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | println!("%.*3$s %s!/n", "Hello,", "World", 4); //~ ERROR multiple unus
77
| multiple missing formatting specifiers
88
|
99
= note: printf formatting not supported; see the documentation for `std::fmt`
10-
help: format specifiers in Rust are written using `{}`
10+
help: format specifiers use curly braces
1111
|
1212
LL | println!("{:.2$} {}!/n", "Hello,", "World", 4); //~ ERROR multiple unused formatting arguments
1313
| ^^^^^^ ^^
@@ -18,7 +18,7 @@ error: argument never used
1818
LL | println!("%1$*2$.*3$f", 123.456); //~ ERROR never used
1919
| ----------- ^^^^^^^
2020
| |
21-
| help: format specifiers in Rust are written using `{}`: `{0:1$.2$}`
21+
| help: format specifiers use curly braces: `{0:1$.2$}`
2222
|
2323
= note: printf formatting not supported; see the documentation for `std::fmt`
2424

@@ -34,7 +34,7 @@ LL | | "###, "Hello,", "World", 4);
3434
| multiple missing formatting specifiers
3535
|
3636
= note: printf formatting not supported; see the documentation for `std::fmt`
37-
help: format specifiers in Rust are written using `{}`
37+
help: format specifiers use curly braces
3838
|
3939
LL | println!(r###"{:.2$}
4040
LL | {}!/n
@@ -44,7 +44,9 @@ error: argument never used
4444
--> $DIR/format-foreign.rs:22:30
4545
|
4646
LL | println!("{} %f", "one", 2.0); //~ ERROR never used
47-
| ^^^
47+
| ------- ^^^
48+
| |
49+
| formatting specifier missing
4850

4951
error: named argument never used
5052
--> $DIR/format-foreign.rs:24:39

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ error: named argument never used
2222
--> $DIR/format-unused-lables.rs:21:35
2323
|
2424
LL | println!("Some stuff", UNUSED="args"); //~ ERROR named argument never used
25-
| ^^^^^^
25+
| ------------ ^^^^^^
26+
| |
27+
| formatting specifier missing
2628

2729
error: multiple unused formatting arguments
2830
--> $DIR/format-unused-lables.rs:24:9

0 commit comments

Comments
 (0)