diff options
author | Matthias Krüger <matthias.krueger@famsik.de> | 2023-02-03 06:30:23 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-03 06:30:23 +0100 |
commit | 2fdb3559c47fbd2e9b959ce2080dc5eeec2554a9 (patch) | |
tree | 1bec960c0de2ae5fbf16a048d1451f2ddd6f399b /compiler/rustc_parse_format | |
parent | 6b94f4dccc8bd68ff407819fd51f2d62d9ca1ade (diff) | |
parent | f922c8395d1cfc54c67cbd645f8456a13fb0189f (diff) | |
download | rust-2fdb3559c47fbd2e9b959ce2080dc5eeec2554a9.tar.gz |
Rollup merge of #106805 - madsravn:master, r=compiler-errors
Suggest `{var:?}` when finding `{?:var}` in inline format strings
Link to issue: https://github.com/rust-lang/rust/issues/106572
This is my first PR to this project, so hopefully I can get some good pointers with me from the first PR.
Currently my idea was to test out whether or not this is the correct solution to this issue and then hopefully expand upon the idea to not only work for Debug formatting but for all of them. If this is a valid solution, I will create a new issue to give a better error message to a broader range of wrong-order formatting.
Diffstat (limited to 'compiler/rustc_parse_format')
-rw-r--r-- | compiler/rustc_parse_format/src/lib.rs | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/compiler/rustc_parse_format/src/lib.rs b/compiler/rustc_parse_format/src/lib.rs index a6dfcd29762..088a87ca571 100644 --- a/compiler/rustc_parse_format/src/lib.rs +++ b/compiler/rustc_parse_format/src/lib.rs @@ -273,7 +273,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))) } @@ -832,6 +838,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, + }, + ); + } + } + 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); |