@@ -464,52 +464,53 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
464464 span : Span ,
465465 ) -> Option < TypeErrorAdditionalDiags > {
466466 let hir = self . tcx . hir ( ) ;
467- if let Some ( node) = self . tcx . hir ( ) . find_by_def_id ( cause. body_id ) &&
468- let hir:: Node :: Item ( hir:: Item {
469- kind : hir:: ItemKind :: Fn ( _sig, _, body_id) , ..
470- } ) = node {
471- let body = hir. body ( * body_id) ;
472-
473- /// Find the if expression with given span
474- struct IfVisitor {
475- pub result : bool ,
476- pub found_if : bool ,
477- pub err_span : Span ,
478- }
467+ if let Some ( body_id) = self . tcx . hir ( ) . maybe_body_owned_by ( cause. body_id ) {
468+ let body = hir. body ( body_id) ;
469+
470+ /// Find the if expression with given span
471+ struct IfVisitor {
472+ pub result : bool ,
473+ pub found_if : bool ,
474+ pub err_span : Span ,
475+ }
479476
480- impl < ' v > Visitor < ' v > for IfVisitor {
481- fn visit_expr ( & mut self , ex : & ' v hir:: Expr < ' v > ) {
482- if self . result { return ; }
483- match ex. kind {
484- hir:: ExprKind :: If ( cond, _, _) => {
485- self . found_if = true ;
486- walk_expr ( self , cond) ;
487- self . found_if = false ;
477+ impl < ' v > Visitor < ' v > for IfVisitor {
478+ fn visit_expr ( & mut self , ex : & ' v hir:: Expr < ' v > ) {
479+ if self . result {
480+ return ;
481+ }
482+ match ex. kind {
483+ hir:: ExprKind :: If ( cond, _, _) => {
484+ self . found_if = true ;
485+ walk_expr ( self , cond) ;
486+ self . found_if = false ;
487+ }
488+ _ => walk_expr ( self , ex) ,
488489 }
489- _ => walk_expr ( self , ex) ,
490490 }
491- }
492491
493- fn visit_stmt ( & mut self , ex : & ' v hir:: Stmt < ' v > ) {
494- if let hir:: StmtKind :: Local ( hir:: Local {
495- span, pat : hir:: Pat { ..} , ty : None , init : Some ( _) , ..
496- } ) = & ex. kind
497- && self . found_if
498- && span. eq ( & self . err_span ) {
499- self . result = true ;
492+ fn visit_stmt ( & mut self , ex : & ' v hir:: Stmt < ' v > ) {
493+ if let hir:: StmtKind :: Local ( hir:: Local {
494+ span, pat : hir:: Pat { ..} , ty : None , init : Some ( _) , ..
495+ } ) = & ex. kind
496+ && self . found_if
497+ && span. eq ( & self . err_span ) {
498+ self . result = true ;
499+ }
500+ walk_stmt ( self , ex) ;
500501 }
501- walk_stmt ( self , ex) ;
502- }
503502
504- fn visit_body ( & mut self , body : & ' v hir:: Body < ' v > ) {
505- hir:: intravisit:: walk_body ( self , body) ;
503+ fn visit_body ( & mut self , body : & ' v hir:: Body < ' v > ) {
504+ hir:: intravisit:: walk_body ( self , body) ;
505+ }
506506 }
507- }
508507
509- let mut visitor = IfVisitor { err_span : span, found_if : false , result : false } ;
510- visitor. visit_body ( & body) ;
511- if visitor. result {
512- return Some ( TypeErrorAdditionalDiags :: AddLetForLetChains { span : span. shrink_to_lo ( ) } ) ;
508+ let mut visitor = IfVisitor { err_span : span, found_if : false , result : false } ;
509+ visitor. visit_body ( & body) ;
510+ if visitor. result {
511+ return Some ( TypeErrorAdditionalDiags :: AddLetForLetChains {
512+ span : span. shrink_to_lo ( ) ,
513+ } ) ;
513514 }
514515 }
515516 None
0 commit comments