Skip to content

Commit f922c83

Browse files
committed
PR fixing wrong order of format parameters in strings. Issue rust-lang#106572
Adding Adding Fixing small issues for PR Adding tests Removing unused binding Changing the wording on note Fixing PR comment
1 parent 0b90256 commit f922c83

File tree

3 files changed

+97
-1
lines changed

3 files changed

+97
-1
lines changed

compiler/rustc_parse_format/src/lib.rs

+28-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,13 @@ impl<'a> Iterator for Parser<'a> {
271271
);
272272
}
273273
} else {
274-
self.suggest_positional_arg_instead_of_captured_arg(arg);
274+
if let Some(&(_, maybe)) = self.cur.peek() {
275+
if maybe == '?' {
276+
self.suggest_format();
277+
} else {
278+
self.suggest_positional_arg_instead_of_captured_arg(arg);
279+
}
280+
}
275281
}
276282
Some(NextArgument(Box::new(arg)))
277283
}
@@ -823,6 +829,27 @@ impl<'a> Parser<'a> {
823829
if found { Some(cur) } else { None }
824830
}
825831

832+
fn suggest_format(&mut self) {
833+
if let (Some(pos), Some(_)) = (self.consume_pos('?'), self.consume_pos(':')) {
834+
let word = self.word();
835+
let _end = self.current_pos();
836+
let pos = self.to_span_index(pos);
837+
self.errors.insert(
838+
0,
839+
ParseError {
840+
description: "expected format parameter to occur after `:`".to_owned(),
841+
note: Some(
842+
format!("`?` comes after `:`, try `{}:{}` instead", word, "?").to_owned(),
843+
),
844+
label: "expected `?` to occur after `:`".to_owned(),
845+
span: pos.to(pos),
846+
secondary_label: None,
847+
should_be_replaced_with_positional_argument: false,
848+
},
849+
);
850+
}
851+
}
852+
826853
fn suggest_positional_arg_instead_of_captured_arg(&mut self, arg: Argument<'a>) {
827854
if let Some(end) = self.consume_pos('.') {
828855
let byte_pos = self.to_span_index(end);
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
fn main() {
2+
let bar = 3;
3+
format!("{?:}", bar);
4+
//~^ ERROR invalid format string: expected format parameter to occur after `:`
5+
format!("{?:bar}");
6+
//~^ ERROR invalid format string: expected format parameter to occur after `:`
7+
format!("{?:?}", bar);
8+
//~^ ERROR invalid format string: expected format parameter to occur after `:`
9+
format!("{??}", bar);
10+
//~^ ERROR invalid format string: expected `'}'`, found `'?'`
11+
format!("{?;bar}");
12+
//~^ ERROR invalid format string: expected `'}'`, found `'?'`
13+
format!("{?:#?}", bar);
14+
//~^ ERROR invalid format string: expected format parameter to occur after `:`
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
error: invalid format string: expected format parameter to occur after `:`
2+
--> $DIR/format-string-wrong-order.rs:3:15
3+
|
4+
LL | format!("{?:}", bar);
5+
| ^ expected `?` to occur after `:` in format string
6+
|
7+
= note: `?` comes after `:`, try `:?` instead
8+
9+
error: invalid format string: expected format parameter to occur after `:`
10+
--> $DIR/format-string-wrong-order.rs:5:15
11+
|
12+
LL | format!("{?:bar}");
13+
| ^ expected `?` to occur after `:` in format string
14+
|
15+
= note: `?` comes after `:`, try `bar:?` instead
16+
17+
error: invalid format string: expected format parameter to occur after `:`
18+
--> $DIR/format-string-wrong-order.rs:7:15
19+
|
20+
LL | format!("{?:?}", bar);
21+
| ^ expected `?` to occur after `:` in format string
22+
|
23+
= note: `?` comes after `:`, try `:?` instead
24+
25+
error: invalid format string: expected `'}'`, found `'?'`
26+
--> $DIR/format-string-wrong-order.rs:9:15
27+
|
28+
LL | format!("{??}", bar);
29+
| -^ expected `}` in format string
30+
| |
31+
| because of this opening brace
32+
|
33+
= note: if you intended to print `{`, you can escape it using `{{`
34+
35+
error: invalid format string: expected `'}'`, found `'?'`
36+
--> $DIR/format-string-wrong-order.rs:11:15
37+
|
38+
LL | format!("{?;bar}");
39+
| -^ expected `}` in format string
40+
| |
41+
| because of this opening brace
42+
|
43+
= note: if you intended to print `{`, you can escape it using `{{`
44+
45+
error: invalid format string: expected format parameter to occur after `:`
46+
--> $DIR/format-string-wrong-order.rs:13:15
47+
|
48+
LL | format!("{?:#?}", bar);
49+
| ^ expected `?` to occur after `:` in format string
50+
|
51+
= note: `?` comes after `:`, try `:?` instead
52+
53+
error: aborting due to 6 previous errors
54+

0 commit comments

Comments
 (0)