Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest {var:?} when finding {?:var} in inline format strings #106805

Merged
merged 1 commit into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion compiler/rustc_parse_format/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,13 @@ impl<'a> Iterator for Parser<'a> {
);
}
} else {
self.suggest_positional_arg_instead_of_captured_arg(arg);
if let Some(&(_, maybe)) = self.cur.peek() {
if maybe == '?' {
self.suggest_format();
} else {
self.suggest_positional_arg_instead_of_captured_arg(arg);
}
}
}
Some(NextArgument(Box::new(arg)))
}
Expand Down Expand Up @@ -823,6 +829,27 @@ impl<'a> Parser<'a> {
if found { Some(cur) } else { None }
}

fn suggest_format(&mut self) {
if let (Some(pos), Some(_)) = (self.consume_pos('?'), self.consume_pos(':')) {
let word = self.word();
let _end = self.current_pos();
let pos = self.to_span_index(pos);
self.errors.insert(
0,
ParseError {
description: "expected format parameter to occur after `:`".to_owned(),
note: Some(
format!("`?` comes after `:`, try `{}:{}` instead", word, "?").to_owned(),
),
label: "expected `?` to occur after `:`".to_owned(),
span: pos.to(pos),
secondary_label: None,
should_be_replaced_with_positional_argument: false,
},
);
compiler-errors marked this conversation as resolved.
Show resolved Hide resolved
}
}

fn suggest_positional_arg_instead_of_captured_arg(&mut self, arg: Argument<'a>) {
if let Some(end) = self.consume_pos('.') {
let byte_pos = self.to_span_index(end);
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/fmt/format-string-wrong-order.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
fn main() {
let bar = 3;
format!("{?:}", bar);
//~^ ERROR invalid format string: expected format parameter to occur after `:`
format!("{?:bar}");
//~^ ERROR invalid format string: expected format parameter to occur after `:`
format!("{?:?}", bar);
//~^ ERROR invalid format string: expected format parameter to occur after `:`
format!("{??}", bar);
//~^ ERROR invalid format string: expected `'}'`, found `'?'`
format!("{?;bar}");
//~^ ERROR invalid format string: expected `'}'`, found `'?'`
format!("{?:#?}", bar);
//~^ ERROR invalid format string: expected format parameter to occur after `:`
}
54 changes: 54 additions & 0 deletions tests/ui/fmt/format-string-wrong-order.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
error: invalid format string: expected format parameter to occur after `:`
--> $DIR/format-string-wrong-order.rs:3:15
|
LL | format!("{?:}", bar);
| ^ expected `?` to occur after `:` in format string
|
= note: `?` comes after `:`, try `:?` instead

error: invalid format string: expected format parameter to occur after `:`
--> $DIR/format-string-wrong-order.rs:5:15
|
LL | format!("{?:bar}");
| ^ expected `?` to occur after `:` in format string
|
= note: `?` comes after `:`, try `bar:?` instead

error: invalid format string: expected format parameter to occur after `:`
--> $DIR/format-string-wrong-order.rs:7:15
|
LL | format!("{?:?}", bar);
| ^ expected `?` to occur after `:` in format string
|
= note: `?` comes after `:`, try `:?` instead

error: invalid format string: expected `'}'`, found `'?'`
--> $DIR/format-string-wrong-order.rs:9:15
|
LL | format!("{??}", bar);
| -^ expected `}` in format string
| |
| because of this opening brace
|
= note: if you intended to print `{`, you can escape it using `{{`

error: invalid format string: expected `'}'`, found `'?'`
--> $DIR/format-string-wrong-order.rs:11:15
|
LL | format!("{?;bar}");
| -^ expected `}` in format string
| |
| because of this opening brace
|
= note: if you intended to print `{`, you can escape it using `{{`

error: invalid format string: expected format parameter to occur after `:`
--> $DIR/format-string-wrong-order.rs:13:15
|
LL | format!("{?:#?}", bar);
| ^ expected `?` to occur after `:` in format string
|
= note: `?` comes after `:`, try `:?` instead

error: aborting due to 6 previous errors