Skip to content

Commit ccb9674

Browse files
committed
simplify similar_tokens from Option<Vec<_>> to Vec<_>
1 parent cf577f3 commit ccb9674

File tree

4 files changed

+17
-21
lines changed

4 files changed

+17
-21
lines changed

Diff for: compiler/rustc_ast/src/token.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -527,13 +527,13 @@ impl TokenKind {
527527

528528
/// Returns tokens that are likely to be typed accidentally instead of the current token.
529529
/// Enables better error recovery when the wrong token is found.
530-
pub fn similar_tokens(&self) -> Option<Vec<TokenKind>> {
531-
match *self {
532-
Comma => Some(vec![Dot, Lt, Semi]),
533-
Semi => Some(vec![Colon, Comma]),
534-
Colon => Some(vec![Semi]),
535-
FatArrow => Some(vec![Eq, RArrow, Ge, Gt]),
536-
_ => None,
530+
pub fn similar_tokens(&self) -> Vec<TokenKind> {
531+
match self {
532+
Comma => vec![Dot, Lt, Semi],
533+
Semi => vec![Colon, Comma],
534+
Colon => vec![Semi],
535+
FatArrow => vec![Eq, RArrow, Ge, Gt],
536+
_ => vec![],
537537
}
538538
}
539539

Diff for: compiler/rustc_builtin_macros/src/format.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,14 @@ fn parse_args<'a>(ecx: &ExtCtxt<'a>, sp: Span, tts: TokenStream) -> PResult<'a,
101101

102102
match p.expect(exp!(Comma)) {
103103
Err(err) => {
104-
match token::TokenKind::Comma.similar_tokens() {
105-
Some(tks) if tks.contains(&p.token.kind) => {
106-
// If a similar token is found, then it may be a typo. We
107-
// consider it as a comma, and continue parsing.
108-
err.emit();
109-
p.bump();
110-
}
104+
if token::TokenKind::Comma.similar_tokens().contains(&p.token.kind) {
105+
// If a similar token is found, then it may be a typo. We
106+
// consider it as a comma, and continue parsing.
107+
err.emit();
108+
p.bump();
109+
} else {
111110
// Otherwise stop the parsing and return the error.
112-
_ => return Err(err),
111+
return Err(err);
113112
}
114113
}
115114
Ok(Recovered::Yes(_)) => (),

Diff for: compiler/rustc_parse/src/parser/expr.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -3115,8 +3115,7 @@ impl<'a> Parser<'a> {
31153115
let arm_body;
31163116
let is_fat_arrow = this.check(exp!(FatArrow));
31173117
let is_almost_fat_arrow = TokenKind::FatArrow
3118-
.similar_tokens()
3119-
.is_some_and(|similar_tokens| similar_tokens.contains(&this.token.kind));
3118+
.similar_tokens().contains(&this.token.kind);
31203119

31213120
// this avoids the compiler saying that a `,` or `}` was expected even though
31223121
// the pattern isn't a never pattern (and thus an arm body is required)

Diff for: compiler/rustc_parse/src/parser/mod.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -923,10 +923,8 @@ impl<'a> Parser<'a> {
923923

924924
_ => {
925925
// Attempt to keep parsing if it was a similar separator.
926-
if let Some(tokens) = exp.tok.similar_tokens() {
927-
if tokens.contains(&self.token.kind) {
928-
self.bump();
929-
}
926+
if exp.tok.similar_tokens().contains(&self.token.kind) {
927+
self.bump();
930928
}
931929
}
932930
}

0 commit comments

Comments
 (0)