@@ -628,8 +628,9 @@ impl ExprCollector<'_> {
628628
629629 fn collect_macro_as_stmt (
630630 & mut self ,
631+ statements : & mut Vec < Statement > ,
631632 mac : ast:: MacroExpr ,
632- ) -> Option < ( Vec < Statement > , Option < ExprId > ) > {
633+ ) -> Option < ExprId > {
633634 let mac_call = mac. macro_call ( ) ?;
634635 let syntax_ptr = AstPtr :: new ( & ast:: Expr :: from ( mac) ) ;
635636 let macro_ptr = AstPtr :: new ( & mac_call) ;
@@ -639,49 +640,32 @@ impl ExprCollector<'_> {
639640 false ,
640641 |this, expansion : Option < ast:: MacroStmts > | match expansion {
641642 Some ( expansion) => {
642- let mut statements: Vec < _ > = expansion
643- . statements ( )
644- . filter_map ( |stmt| this. collect_stmt ( stmt) )
645- . flatten ( )
646- . collect ( ) ;
647- let tail = expansion. expr ( ) . and_then ( |expr| match expr {
648- ast:: Expr :: MacroExpr ( mac) => {
649- let ( stmts, tail) = this. collect_macro_as_stmt ( mac) ?;
650- statements. extend ( stmts) ;
651- tail
652- }
643+ expansion. statements ( ) . for_each ( |stmt| this. collect_stmt ( statements, stmt) ) ;
644+ expansion. expr ( ) . and_then ( |expr| match expr {
645+ ast:: Expr :: MacroExpr ( mac) => this. collect_macro_as_stmt ( statements, mac) ,
653646 expr => Some ( this. collect_expr ( expr) ) ,
654- } ) ;
655- Some ( ( statements, tail) )
647+ } )
656648 }
657649 None => None ,
658650 } ,
659651 ) ;
660- let mut stmts = Vec :: new ( ) ;
661- let expr = match expansion {
662- Some ( ( statements, tail) ) => {
663- stmts. extend ( statements) ;
652+ match expansion {
653+ Some ( tail) => {
664654 // Make the macro-call point to its expanded expression so we can query
665655 // semantics on syntax pointers to the macro
666656 let src = self . expander . to_source ( syntax_ptr) ;
667- match tail {
668- Some ( tail) => {
669- self . source_map . expr_map . insert ( src, tail) ;
670- tail
671- }
672- None => self . make_expr ( Expr :: Missing , Ok ( src. clone ( ) ) ) ,
673- }
657+ self . source_map . expr_map . insert ( src, tail) ;
658+ Some ( tail)
674659 }
675- None => self . alloc_expr ( Expr :: Missing , syntax_ptr) ,
676- } ;
677- Some ( ( stmts, Some ( expr) ) )
660+ None => None ,
661+ }
678662 }
679663
680- fn collect_stmt ( & mut self , s : ast:: Stmt ) -> Option < Vec < Statement > > {
664+ fn collect_stmt ( & mut self , statements : & mut Vec < Statement > , s : ast:: Stmt ) {
681665 match s {
682666 ast:: Stmt :: LetStmt ( stmt) => {
683667 if self . check_cfg ( & stmt) . is_none ( ) {
684- return None ;
668+ return ;
685669 }
686670 let pat = self . collect_pat_opt ( stmt. pat ( ) ) ;
687671 let type_ref =
@@ -691,29 +675,26 @@ impl ExprCollector<'_> {
691675 . let_else ( )
692676 . and_then ( |let_else| let_else. block_expr ( ) )
693677 . map ( |block| self . collect_block ( block) ) ;
694- Some ( vec ! [ Statement :: Let { pat, type_ref, initializer, else_branch } ] )
678+ statements . push ( Statement :: Let { pat, type_ref, initializer, else_branch } ) ;
695679 }
696680 ast:: Stmt :: ExprStmt ( stmt) => {
697681 let expr = stmt. expr ( ) ;
698- if let Some ( expr) = & expr {
699- if self . check_cfg ( expr) . is_none ( ) {
700- return None ;
701- }
682+ match & expr {
683+ Some ( expr) if self . check_cfg ( expr) . is_none ( ) => return ,
684+ _ => ( ) ,
702685 }
703686 let has_semi = stmt. semicolon_token ( ) . is_some ( ) ;
704687 // Note that macro could be expanded to multiple statements
705688 if let Some ( ast:: Expr :: MacroExpr ( mac) ) = expr {
706- let ( mut statements, tail) = self . collect_macro_as_stmt ( mac) ?;
707- if let Some ( expr) = tail {
708- statements. push ( Statement :: Expr { expr, has_semi } ) ;
689+ if let Some ( expr) = self . collect_macro_as_stmt ( statements, mac) {
690+ statements. push ( Statement :: Expr { expr, has_semi } )
709691 }
710- Some ( statements)
711692 } else {
712693 let expr = self . collect_expr_opt ( expr) ;
713- Some ( vec ! [ Statement :: Expr { expr, has_semi } ] )
694+ statements . push ( Statement :: Expr { expr, has_semi } ) ;
714695 }
715696 }
716- ast:: Stmt :: Item ( _item) => None ,
697+ ast:: Stmt :: Item ( _item) => ( ) ,
717698 }
718699 }
719700
@@ -734,14 +715,10 @@ impl ExprCollector<'_> {
734715 let prev_def_map = mem:: replace ( & mut self . expander . def_map , def_map) ;
735716 let prev_local_module = mem:: replace ( & mut self . expander . module , module) ;
736717
737- let mut statements: Vec < _ > =
738- block. statements ( ) . filter_map ( |s| self . collect_stmt ( s ) ) . flatten ( ) . collect ( ) ;
718+ let mut statements = Vec :: new ( ) ;
719+ block. statements ( ) . for_each ( |s| self . collect_stmt ( & mut statements , s ) ) ;
739720 let tail = block. tail_expr ( ) . and_then ( |e| match e {
740- ast:: Expr :: MacroExpr ( mac) => {
741- let ( stmts, tail) = self . collect_macro_as_stmt ( mac) ?;
742- statements. extend ( stmts) ;
743- tail
744- }
721+ ast:: Expr :: MacroExpr ( mac) => self . collect_macro_as_stmt ( & mut statements, mac) ,
745722 expr => self . maybe_collect_expr ( expr) ,
746723 } ) ;
747724 let tail = tail. or_else ( || {
0 commit comments