diff options
| author | mejrs <59372212+mejrs@users.noreply.github.com> | 2025-05-23 20:48:22 +0200 |
|---|---|---|
| committer | mejrs <59372212+mejrs@users.noreply.github.com> | 2025-06-09 16:28:57 +0200 |
| commit | d3137d91b8bf87197757c5429d9acfcda79c0911 (patch) | |
| tree | 580c2053fd59b6ec00c1eba11c5187aa095d54b2 /compiler/rustc_parse_format/src | |
| parent | 716190632f58f073f3cac903324b62899599876b (diff) | |
| download | rust-d3137d91b8bf87197757c5429d9acfcda79c0911.tar.gz rust-d3137d91b8bf87197757c5429d9acfcda79c0911.zip | |
Move the "missing closing brace" error creation to one place
Diffstat (limited to 'compiler/rustc_parse_format/src')
| -rw-r--r-- | compiler/rustc_parse_format/src/lib.rs | 36 |
1 files changed, 15 insertions, 21 deletions
diff --git a/compiler/rustc_parse_format/src/lib.rs b/compiler/rustc_parse_format/src/lib.rs index 358723a17ef..9a45a392b77 100644 --- a/compiler/rustc_parse_format/src/lib.rs +++ b/compiler/rustc_parse_format/src/lib.rs @@ -254,19 +254,15 @@ impl<'a> Iterator for Parser<'a> { // single open brace self.last_open_brace = Some(start..end); let arg = self.argument(); - if let Some(close_brace_range) = self.consume_closing_brace(&arg) { + self.ws(); + if let Some((close_brace_range, _)) = self.consume_pos('}') { if self.is_source_literal { self.arg_places.push(start..close_brace_range.end); } - } else if let Some((_, _, c)) = self.peek() { - match c { - '?' => self.suggest_format_debug(), - '<' | '^' | '>' => self.suggest_format_align(c), - _ => { - self.suggest_positional_arg_instead_of_captured_arg(arg.clone()) - } - } + } else { + self.missing_closing_brace(&arg); } + Some(Piece::NextArgument(Box::new(arg))) } } @@ -437,17 +433,9 @@ impl<'a> Parser<'a> { None } - /// Forces consumption of the specified character. If the character is not - /// found, an error is emitted. - fn consume_closing_brace(&mut self, arg: &Argument<'_>) -> Option<Range<usize>> { - self.ws(); - + /// Called if a closing brace was not found. + fn missing_closing_brace(&mut self, arg: &Argument<'_>) { let (range, description) = if let Some((r, _, c)) = self.peek() { - if c == '}' { - self.input_vec_index += 1; - return Some(r); - } - // or r.clone()? (r.start..r.start, format!("expected `}}`, found `{}`", c.escape_debug())) } else { ( @@ -480,7 +468,13 @@ impl<'a> Parser<'a> { suggestion: Suggestion::None, }); - None + if let Some((_, _, c)) = self.peek() { + match c { + '?' => self.suggest_format_debug(), + '<' | '^' | '>' => self.suggest_format_align(c), + _ => self.suggest_positional_arg_instead_of_captured_arg(arg), + } + } } /// Consumes all whitespace characters until the first non-whitespace character @@ -905,7 +899,7 @@ impl<'a> Parser<'a> { } } - fn suggest_positional_arg_instead_of_captured_arg(&mut self, arg: Argument<'a>) { + fn suggest_positional_arg_instead_of_captured_arg(&mut self, arg: &Argument<'_>) { // If the argument is not an identifier, it is not a field access. if !arg.is_identifier() { return; |
