Skip to content

Commit 78ded09

Browse files
authored
Rollup merge of rust-lang#135882 - hkBst:master, r=estebank
simplify `similar_tokens` from `Option<Vec<_>>` to `&[_]` All uses immediately invoke contains, so maybe a further simplification is possible.
2 parents aedc0a3 + 5f01e12 commit 78ded09

File tree

4 files changed

+18
-22
lines changed

4 files changed

+18
-22
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) -> &[TokenKind] {
531+
match self {
532+
Comma => &[Dot, Lt, Semi],
533+
Semi => &[Colon, Comma],
534+
Colon => &[Semi],
535+
FatArrow => &[Eq, RArrow, Ge, Gt],
536+
_ => &[],
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

+2-3
Original file line numberDiff line numberDiff line change
@@ -3114,9 +3114,8 @@ impl<'a> Parser<'a> {
31143114
let span_before_body = this.prev_token.span;
31153115
let arm_body;
31163116
let is_fat_arrow = this.check(exp!(FatArrow));
3117-
let is_almost_fat_arrow = TokenKind::FatArrow
3118-
.similar_tokens()
3119-
.is_some_and(|similar_tokens| similar_tokens.contains(&this.token.kind));
3117+
let is_almost_fat_arrow =
3118+
TokenKind::FatArrow.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
@@ -924,10 +924,8 @@ impl<'a> Parser<'a> {
924924

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

0 commit comments

Comments
 (0)