Skip to content

Commit cf3af23

Browse files
committed
refactor matching and if let chains
1 parent d3137d9 commit cf3af23

File tree

1 file changed

+37
-46
lines changed
  • compiler/rustc_parse_format/src

1 file changed

+37
-46
lines changed

compiler/rustc_parse_format/src/lib.rs

Lines changed: 37 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -424,12 +424,13 @@ impl<'a> Parser<'a> {
424424
/// returned, otherwise the character is consumed and the current position is
425425
/// returned.
426426
fn consume_pos(&mut self, ch: char) -> Option<(Range<usize>, usize)> {
427-
if let Some((r, i, c)) = self.peek() {
428-
if ch == c {
429-
self.input_vec_index += 1;
430-
return Some((r, i));
431-
}
427+
if let Some((r, i, c)) = self.peek()
428+
&& ch == c
429+
{
430+
self.input_vec_index += 1;
431+
return Some((r, i));
432432
}
433+
433434
None
434435
}
435436

@@ -549,30 +550,25 @@ impl<'a> Parser<'a> {
549550
let word = self.word();
550551

551552
// Recover from `r#ident` in format strings.
552-
// FIXME: use a let chain
553-
if word == "r" {
554-
if let Some((r, _, '#')) = self.peek() {
555-
if self
556-
.peek_ahead()
557-
.is_some_and(|(_, _, c)| rustc_lexer::is_id_start(c))
558-
{
559-
self.input_vec_index += 1;
560-
let prefix_end = r.end;
561-
let word = self.word();
562-
let prefix_span = start..prefix_end;
563-
let full_span =
564-
start..self.input_vec_index2range(self.input_vec_index).start;
565-
self.errors.insert(0, ParseError {
553+
if word == "r"
554+
&& let Some((r, _, '#')) = self.peek()
555+
&& self.peek_ahead().is_some_and(|(_, _, c)| rustc_lexer::is_id_start(c))
556+
{
557+
self.input_vec_index += 1;
558+
let prefix_end = r.end;
559+
let word = self.word();
560+
let prefix_span = start..prefix_end;
561+
let full_span =
562+
start..self.input_vec_index2range(self.input_vec_index).start;
563+
self.errors.insert(0, ParseError {
566564
description: "raw identifiers are not supported".to_owned(),
567565
note: Some("identifiers in format strings can be keywords and don't need to be prefixed with `r#`".to_string()),
568566
label: "raw identifier used here".to_owned(),
569567
span: full_span,
570568
secondary_label: None,
571569
suggestion: Suggestion::RemoveRawIdent(prefix_span),
572570
});
573-
return Some(ArgumentNamed(word));
574-
}
575-
}
571+
return Some(ArgumentNamed(word));
576572
}
577573

578574
Some(ArgumentNamed(word))
@@ -620,12 +616,10 @@ impl<'a> Parser<'a> {
620616
}
621617

622618
// fill character
623-
if let Some((r, _, c)) = self.peek() {
624-
if let Some((_, _, '>' | '<' | '^')) = self.peek_ahead() {
625-
self.input_vec_index += 1;
626-
spec.fill = Some(c);
627-
spec.fill_span = Some(r);
628-
}
619+
if let (Some((r, _, c)), Some((_, _, '>' | '<' | '^'))) = (self.peek(), self.peek_ahead()) {
620+
self.input_vec_index += 1;
621+
spec.fill = Some(c);
622+
spec.fill_span = Some(r);
629623
}
630624
// Alignment
631625
if self.consume('<') {
@@ -703,24 +697,21 @@ impl<'a> Parser<'a> {
703697
}
704698
} else if let Some((range, _)) = self.consume_pos('?') {
705699
spec.ty = "?";
706-
if let Some((r, _, c)) = self.peek() {
707-
match c {
708-
'#' | 'x' | 'X' => self.errors.insert(
709-
0,
710-
ParseError {
711-
description: format!("expected `}}`, found `{c}`"),
712-
note: None,
713-
label: "expected `'}'`".into(),
714-
span: r.clone(),
715-
secondary_label: None,
716-
suggestion: Suggestion::ReorderFormatParameter(
717-
range.start..r.end,
718-
format!("{c}?"),
719-
),
720-
},
721-
),
722-
_ => (),
723-
}
700+
if let Some((r, _, c @ ('#' | 'x' | 'X'))) = self.peek() {
701+
self.errors.insert(
702+
0,
703+
ParseError {
704+
description: format!("expected `}}`, found `{c}`"),
705+
note: None,
706+
label: "expected `'}'`".into(),
707+
span: r.clone(),
708+
secondary_label: None,
709+
suggestion: Suggestion::ReorderFormatParameter(
710+
range.start..r.end,
711+
format!("{c}?"),
712+
),
713+
},
714+
);
724715
}
725716
} else {
726717
spec.ty = self.word();

0 commit comments

Comments
 (0)