@@ -600,7 +600,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
600600
601601 // Visit to make sure there's a single `return` type to suggest `impl Trait`,
602602 // otherwise suggest using `Box<dyn Trait>` or an enum.
603- let mut visitor = ReturnsVisitor :: new ( ) ;
603+ let mut visitor = ReturnsVisitor :: default ( ) ;
604604 visitor. visit_body ( & body) ;
605605
606606 let tables = self . in_progress_tables . map ( |t| t. borrow ( ) ) . unwrap ( ) ;
@@ -742,7 +742,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
742742 {
743743 let body = hir. body ( * body_id) ;
744744 // Point at all the `return`s in the function as they have failed trait bounds.
745- let mut visitor = ReturnsVisitor :: new ( ) ;
745+ let mut visitor = ReturnsVisitor :: default ( ) ;
746746 visitor. visit_body ( & body) ;
747747 let tables = self . in_progress_tables . map ( |t| t. borrow ( ) ) . unwrap ( ) ;
748748 for expr in & visitor. returns {
@@ -1696,17 +1696,12 @@ pub fn suggest_constraining_type_param(
16961696
16971697/// Collect all the returned expressions within the input expression.
16981698/// Used to point at the return spans when we want to suggest some change to them.
1699+ #[ derive( Default ) ]
16991700struct ReturnsVisitor < ' v > {
17001701 returns : Vec < & ' v hir:: Expr < ' v > > ,
17011702 in_block_tail : bool ,
17021703}
17031704
1704- impl ReturnsVisitor < ' _ > {
1705- fn new ( ) -> Self {
1706- ReturnsVisitor { returns : vec ! [ ] , in_block_tail : false }
1707- }
1708- }
1709-
17101705impl < ' v > Visitor < ' v > for ReturnsVisitor < ' v > {
17111706 type Map = rustc:: hir:: map:: Map < ' v > ;
17121707
@@ -1715,6 +1710,10 @@ impl<'v> Visitor<'v> for ReturnsVisitor<'v> {
17151710 }
17161711
17171712 fn visit_expr ( & mut self , ex : & ' v hir:: Expr < ' v > ) {
1713+ // Visit every expression to detect `return` paths, either through the function's tail
1714+ // expression or `return` statements. We walk all nodes to find `return` statements, but
1715+ // we only care about tail expressions when `in_block_tail` is `true`, which means that
1716+ // they're in the return path of the function body.
17181717 match ex. kind {
17191718 hir:: ExprKind :: Ret ( Some ( ex) ) => {
17201719 self . returns . push ( ex) ;
@@ -1741,7 +1740,7 @@ impl<'v> Visitor<'v> for ReturnsVisitor<'v> {
17411740 }
17421741
17431742 fn visit_body ( & mut self , body : & ' v hir:: Body < ' v > ) {
1744- let prev = self . in_block_tail ;
1743+ assert ! ( ! self . in_block_tail) ;
17451744 if body. generator_kind ( ) . is_none ( ) {
17461745 if let hir:: ExprKind :: Block ( block, None ) = body. value . kind {
17471746 if block. expr . is_some ( ) {
@@ -1750,6 +1749,5 @@ impl<'v> Visitor<'v> for ReturnsVisitor<'v> {
17501749 }
17511750 }
17521751 hir:: intravisit:: walk_body ( self , body) ;
1753- self . in_block_tail = prev;
17541752 }
17551753}
0 commit comments