1
Fork 0

Rollup merge of #135882 - hkBst:master, r=estebank

simplify `similar_tokens` from `Option<Vec<_>>` to `&[_]`

All uses immediately invoke contains, so maybe a further simplification is possible.
This commit is contained in:
Matthias Krüger 2025-01-30 12:45:27 +01:00 committed by GitHub
commit 78ded09912
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 18 additions and 22 deletions

View file

@ -527,13 +527,13 @@ impl TokenKind {
/// Returns tokens that are likely to be typed accidentally instead of the current token. /// Returns tokens that are likely to be typed accidentally instead of the current token.
/// Enables better error recovery when the wrong token is found. /// Enables better error recovery when the wrong token is found.
pub fn similar_tokens(&self) -> Option<Vec<TokenKind>> { pub fn similar_tokens(&self) -> &[TokenKind] {
match *self { match self {
Comma => Some(vec![Dot, Lt, Semi]), Comma => &[Dot, Lt, Semi],
Semi => Some(vec![Colon, Comma]), Semi => &[Colon, Comma],
Colon => Some(vec![Semi]), Colon => &[Semi],
FatArrow => Some(vec![Eq, RArrow, Ge, Gt]), FatArrow => &[Eq, RArrow, Ge, Gt],
_ => None, _ => &[],
} }
} }

View file

@ -101,15 +101,14 @@ fn parse_args<'a>(ecx: &ExtCtxt<'a>, sp: Span, tts: TokenStream) -> PResult<'a,
match p.expect(exp!(Comma)) { match p.expect(exp!(Comma)) {
Err(err) => { Err(err) => {
match token::TokenKind::Comma.similar_tokens() { if token::TokenKind::Comma.similar_tokens().contains(&p.token.kind) {
Some(tks) if tks.contains(&p.token.kind) => {
// If a similar token is found, then it may be a typo. We // If a similar token is found, then it may be a typo. We
// consider it as a comma, and continue parsing. // consider it as a comma, and continue parsing.
err.emit(); err.emit();
p.bump(); p.bump();
} } else {
// Otherwise stop the parsing and return the error. // Otherwise stop the parsing and return the error.
_ => return Err(err), return Err(err);
} }
} }
Ok(Recovered::Yes(_)) => (), Ok(Recovered::Yes(_)) => (),

View file

@ -3114,9 +3114,8 @@ impl<'a> Parser<'a> {
let span_before_body = this.prev_token.span; let span_before_body = this.prev_token.span;
let arm_body; let arm_body;
let is_fat_arrow = this.check(exp!(FatArrow)); let is_fat_arrow = this.check(exp!(FatArrow));
let is_almost_fat_arrow = TokenKind::FatArrow let is_almost_fat_arrow =
.similar_tokens() TokenKind::FatArrow.similar_tokens().contains(&this.token.kind);
.is_some_and(|similar_tokens| similar_tokens.contains(&this.token.kind));
// this avoids the compiler saying that a `,` or `}` was expected even though // this avoids the compiler saying that a `,` or `}` was expected even though
// the pattern isn't a never pattern (and thus an arm body is required) // the pattern isn't a never pattern (and thus an arm body is required)

View file

@ -924,13 +924,11 @@ impl<'a> Parser<'a> {
_ => { _ => {
// Attempt to keep parsing if it was a similar separator. // Attempt to keep parsing if it was a similar separator.
if let Some(tokens) = exp.tok.similar_tokens() { if exp.tok.similar_tokens().contains(&self.token.kind) {
if tokens.contains(&self.token.kind) {
self.bump(); self.bump();
} }
} }
} }
}
// If this was a missing `@` in a binding pattern // If this was a missing `@` in a binding pattern
// bail with a suggestion // bail with a suggestion