@@ -14,11 +14,11 @@ use proc_macro2::{Punct, Spacing::*, Span, TokenStream, TokenTree};
1414use syn:: spanned:: Spanned as _;
1515use syn:: {
1616 AttrStyle , BareVariadic , Block , Expr , ExprBinary , ExprBlock , ExprBreak , ExprCast , ExprField ,
17- ExprIndex , ExprParen , ExprUnary , FnArg , ForeignItem , ForeignItemFn , ForeignItemMacro ,
18- ForeignItemStatic , ForeignItemType , Ident , Item , ItemConst , ItemEnum , ItemExternCrate , ItemFn ,
19- ItemForeignMod , ItemImpl , ItemMacro , ItemMod , ItemStatic , ItemStruct , ItemTrait ,
20- ItemTraitAlias , ItemType , ItemUnion , ItemUse , Lit , MacroDelimiter , PathSegment , ReturnType ,
21- Stmt , Type , TypeTuple , UseTree , Visibility ,
17+ ExprIndex , ExprParen , ExprReturn , ExprUnary , FnArg , ForeignItem , ForeignItemFn ,
18+ ForeignItemMacro , ForeignItemStatic , ForeignItemType , Ident , Item , ItemConst , ItemEnum ,
19+ ItemExternCrate , ItemFn , ItemForeignMod , ItemImpl , ItemMacro , ItemMod , ItemStatic , ItemStruct ,
20+ ItemTrait , ItemTraitAlias , ItemType , ItemUnion , ItemUse , Lit , MacroDelimiter , PathSegment ,
21+ ReturnType , Stmt , Type , TypeTuple , UseTree , Visibility ,
2222} ;
2323use syn:: { BinOp , UnOp } ; // To override `c_ast::{BinOp,UnOp}` from glob import.
2424
@@ -2404,6 +2404,7 @@ impl<'c> Translation<'c> {
24042404 } ;
24052405 let mut converted_body =
24062406 self . convert_function_body ( ctx, name, body_ids, return_type, ret) ?;
2407+ strip_tail_return ( & mut converted_body) ;
24072408
24082409 // If `alloca` was used in the function body, include a variable to hold the
24092410 // allocations.
@@ -2520,7 +2521,6 @@ impl<'c> Translation<'c> {
25202521 graph : cfg:: Cfg < cfg:: Label , cfg:: StmtOrDecl > ,
25212522 store : cfg:: DeclStmtStore ,
25222523 live_in : IndexSet < CDeclId > ,
2523- cut_out_trailing_ret : bool ,
25242524 ) -> TranslationResult < Vec < Stmt > > {
25252525 if self . tcfg . dump_function_cfgs {
25262526 graph
@@ -2582,7 +2582,6 @@ impl<'c> Translation<'c> {
25822582 & mut self . comment_store . borrow_mut ( ) ,
25832583 current_block,
25842584 self . tcfg . debug_relooper_labels ,
2585- cut_out_trailing_ret,
25862585 ) ?) ;
25872586 Ok ( stmts)
25882587 }
@@ -2598,7 +2597,7 @@ impl<'c> Translation<'c> {
25982597 // Function body scope
25992598 self . with_scope ( || {
26002599 let ( graph, store) = cfg:: Cfg :: from_stmts ( self , ctx, body_ids, ret, ret_ty) ?;
2601- self . convert_cfg ( name, graph, store, IndexSet :: new ( ) , true )
2600+ self . convert_cfg ( name, graph, store, IndexSet :: new ( ) )
26022601 } )
26032602 }
26042603
@@ -5266,3 +5265,11 @@ impl<'c> Translation<'c> {
52665265 }
52675266 }
52685267}
5268+
5269+ fn strip_tail_return ( stmts : & mut Vec < Stmt > ) {
5270+ // If the very last statement in the vector is a `return`, we can either cut it out or replace
5271+ // it with the returned value.
5272+ if let Some ( Stmt :: Expr ( Expr :: Return ( ExprReturn { expr : None , .. } ) , _) ) = stmts. last ( ) {
5273+ stmts. pop ( ) ;
5274+ }
5275+ }
0 commit comments