From a8088220937764db329135b3e778de935a9abbb2 Mon Sep 17 00:00:00 2001 From: Cameron Steffen Date: Wed, 10 Mar 2021 11:04:16 -0600 Subject: [PATCH] Simplify lower ast block --- compiler/rustc_ast_lowering/src/lib.rs | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 057e4d81f80..3a97321ceb6 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -2409,26 +2409,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { } fn lower_block_noalloc(&mut self, b: &Block, targeted_by_break: bool) -> hir::Block<'hir> { - let mut expr: Option<&'hir _> = None; - - let stmts = self.arena.alloc_from_iter( - b.stmts - .iter() - .enumerate() - .filter_map(|(index, stmt)| { - if index == b.stmts.len() - 1 { - if let StmtKind::Expr(ref e) = stmt.kind { - expr = Some(self.lower_expr(e)); - None - } else { - Some(self.lower_stmt(stmt)) - } - } else { - Some(self.lower_stmt(stmt)) - } - }) - .flatten(), - ); + let (stmts, expr) = match &*b.stmts { + [stmts @ .., Stmt { kind: StmtKind::Expr(e), .. }] => (stmts, Some(&*e)), + stmts => (stmts, None), + }; + let stmts = self.arena.alloc_from_iter(stmts.iter().flat_map(|stmt| self.lower_stmt(stmt))); + let expr = expr.map(|e| self.lower_expr(e)); let rules = self.lower_block_check_mode(&b.rules); let hir_id = self.lower_node_id(b.id);