Rollup merge of #94445 - c410-f3r:more-let-chains, r=cjgillot

4 - Make more use of `let_chains`

Continuation of #94376.

cc #53667
This commit is contained in:
Matthias Krüger 2022-02-28 20:05:17 +01:00 committed by GitHub
commit 5be38d2bb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 72 additions and 76 deletions

View File

@ -282,14 +282,13 @@ struct TokenStreamBuilder {
impl TokenStreamBuilder { impl TokenStreamBuilder {
fn push(&mut self, (tree, joint): TreeAndSpacing) { fn push(&mut self, (tree, joint): TreeAndSpacing) {
if let Some((TokenTree::Token(prev_token), Joint)) = self.buf.last() { if let Some((TokenTree::Token(prev_token), Joint)) = self.buf.last()
if let TokenTree::Token(token) = &tree { && let TokenTree::Token(token) = &tree
if let Some(glued) = prev_token.glue(token) { && let Some(glued) = prev_token.glue(token)
self.buf.pop(); {
self.buf.push((TokenTree::Token(glued), joint)); self.buf.pop();
return; self.buf.push((TokenTree::Token(glued), joint));
} return;
}
} }
self.buf.push((tree, joint)) self.buf.push((tree, joint))
} }

View File

@ -1,9 +1,10 @@
//! The main parser interface. //! The main parser interface.
#![feature(array_windows)] #![feature(array_windows)]
#![feature(box_patterns)]
#![feature(crate_visibility_modifier)] #![feature(crate_visibility_modifier)]
#![feature(if_let_guard)] #![feature(if_let_guard)]
#![feature(box_patterns)] #![feature(let_chains)]
#![feature(let_else)] #![feature(let_else)]
#![recursion_limit = "256"] #![recursion_limit = "256"]

View File

@ -732,43 +732,42 @@ impl<'a> Parser<'a> {
mut e: DiagnosticBuilder<'a, ErrorReported>, mut e: DiagnosticBuilder<'a, ErrorReported>,
expr: &mut P<Expr>, expr: &mut P<Expr>,
) -> PResult<'a, ()> { ) -> PResult<'a, ()> {
if let ExprKind::Binary(binop, _, _) = &expr.kind { if let ExprKind::Binary(binop, _, _) = &expr.kind
if let ast::BinOpKind::Lt = binop.node { && let ast::BinOpKind::Lt = binop.node
if self.eat(&token::Comma) { && self.eat(&token::Comma)
let x = self.parse_seq_to_before_end( {
&token::Gt, let x = self.parse_seq_to_before_end(
SeqSep::trailing_allowed(token::Comma), &token::Gt,
|p| p.parse_generic_arg(None), SeqSep::trailing_allowed(token::Comma),
); |p| p.parse_generic_arg(None),
match x { );
Ok((_, _, false)) => { match x {
if self.eat(&token::Gt) { Ok((_, _, false)) => {
e.span_suggestion_verbose( if self.eat(&token::Gt) {
binop.span.shrink_to_lo(), e.span_suggestion_verbose(
TURBOFISH_SUGGESTION_STR, binop.span.shrink_to_lo(),
"::".to_string(), TURBOFISH_SUGGESTION_STR,
Applicability::MaybeIncorrect, "::".to_string(),
) Applicability::MaybeIncorrect,
.emit(); )
match self.parse_expr() { .emit();
Ok(_) => { match self.parse_expr() {
*expr = Ok(_) => {
self.mk_expr_err(expr.span.to(self.prev_token.span)); *expr =
return Ok(()); self.mk_expr_err(expr.span.to(self.prev_token.span));
} return Ok(());
Err(err) => { }
*expr = self.mk_expr_err(expr.span); Err(err) => {
err.cancel(); *expr = self.mk_expr_err(expr.span);
} err.cancel();
}
} }
} }
Err(err) => {
err.cancel();
}
_ => {}
} }
} }
Err(err) => {
err.cancel();
}
_ => {}
} }
} }
Err(e) Err(e)
@ -784,12 +783,13 @@ impl<'a> Parser<'a> {
outer_op: &Spanned<AssocOp>, outer_op: &Spanned<AssocOp>,
) -> bool /* advanced the cursor */ { ) -> bool /* advanced the cursor */ {
if let ExprKind::Binary(op, ref l1, ref r1) = inner_op.kind { if let ExprKind::Binary(op, ref l1, ref r1) = inner_op.kind {
if let ExprKind::Field(_, ident) = l1.kind { if let ExprKind::Field(_, ident) = l1.kind
if ident.as_str().parse::<i32>().is_err() && !matches!(r1.kind, ExprKind::Lit(_)) { && ident.as_str().parse::<i32>().is_err()
// The parser has encountered `foo.bar<baz`, the likelihood of the turbofish && !matches!(r1.kind, ExprKind::Lit(_))
// suggestion being the only one to apply is high. {
return false; // The parser has encountered `foo.bar<baz`, the likelihood of the turbofish
} // suggestion being the only one to apply is high.
return false;
} }
let mut enclose = |left: Span, right: Span| { let mut enclose = |left: Span, right: Span| {
err.multipart_suggestion( err.multipart_suggestion(

View File

@ -102,14 +102,12 @@ impl<'a> Parser<'a> {
) -> PResult<'a, Option<Item>> { ) -> PResult<'a, Option<Item>> {
// Don't use `maybe_whole` so that we have precise control // Don't use `maybe_whole` so that we have precise control
// over when we bump the parser // over when we bump the parser
if let token::Interpolated(nt) = &self.token.kind { if let token::Interpolated(nt) = &self.token.kind && let token::NtItem(item) = &**nt {
if let token::NtItem(item) = &**nt { let mut item = item.clone();
let mut item = item.clone(); self.bump();
self.bump();
attrs.prepend_to_nt_inner(&mut item.attrs); attrs.prepend_to_nt_inner(&mut item.attrs);
return Ok(Some(item.into_inner())); return Ok(Some(item.into_inner()));
}
}; };
let mut unclosed_delims = vec![]; let mut unclosed_delims = vec![];

View File

@ -97,15 +97,15 @@ macro_rules! maybe_whole {
#[macro_export] #[macro_export]
macro_rules! maybe_recover_from_interpolated_ty_qpath { macro_rules! maybe_recover_from_interpolated_ty_qpath {
($self: expr, $allow_qpath_recovery: expr) => { ($self: expr, $allow_qpath_recovery: expr) => {
if $allow_qpath_recovery && $self.look_ahead(1, |t| t == &token::ModSep) { if $allow_qpath_recovery
if let token::Interpolated(nt) = &$self.token.kind { && $self.look_ahead(1, |t| t == &token::ModSep)
if let token::NtTy(ty) = &**nt { && let token::Interpolated(nt) = &$self.token.kind
&& let token::NtTy(ty) = &**nt
{
let ty = ty.clone(); let ty = ty.clone();
$self.bump(); $self.bump();
return $self.maybe_recover_from_bad_qpath_stage_2($self.prev_token.span, ty); return $self.maybe_recover_from_bad_qpath_stage_2($self.prev_token.span, ty);
} }
}
}
}; };
} }

View File

@ -658,13 +658,13 @@ impl<'a> Parser<'a> {
&self, &self,
gen_arg: GenericArg, gen_arg: GenericArg,
) -> Result<(Ident, Option<GenericArgs>), GenericArg> { ) -> Result<(Ident, Option<GenericArgs>), GenericArg> {
if let GenericArg::Type(ty) = &gen_arg { if let GenericArg::Type(ty) = &gen_arg
if let ast::TyKind::Path(qself, path) = &ty.kind { && let ast::TyKind::Path(qself, path) = &ty.kind
if qself.is_none() && path.segments.len() == 1 { && qself.is_none()
let seg = &path.segments[0]; && path.segments.len() == 1
return Ok((seg.ident, seg.args.as_deref().cloned())); {
} let seg = &path.segments[0];
} return Ok((seg.ident, seg.args.as_deref().cloned()));
} }
Err(gen_arg) Err(gen_arg)
} }

View File

@ -48,15 +48,13 @@ impl<'a> Parser<'a> {
// Don't use `maybe_whole` so that we have precise control // Don't use `maybe_whole` so that we have precise control
// over when we bump the parser // over when we bump the parser
if let token::Interpolated(nt) = &self.token.kind { if let token::Interpolated(nt) = &self.token.kind && let token::NtStmt(stmt) = &**nt {
if let token::NtStmt(stmt) = &**nt { let mut stmt = stmt.clone();
let mut stmt = stmt.clone(); self.bump();
self.bump(); stmt.visit_attrs(|stmt_attrs| {
stmt.visit_attrs(|stmt_attrs| { attrs.prepend_to_nt_inner(stmt_attrs);
attrs.prepend_to_nt_inner(stmt_attrs); });
}); return Ok(Some(stmt));
return Ok(Some(stmt));
}
} }
Ok(Some(if self.token.is_keyword(kw::Let) { Ok(Some(if self.token.is_keyword(kw::Let) {