@@ -2,7 +2,6 @@ use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed
22use rustc_hir as hir;
33use rustc_hir:: intravisit:: Visitor ;
44use rustc_hir:: Node ;
5- use rustc_middle:: hir:: map:: Map ;
65use rustc_middle:: mir:: { Mutability , Place , PlaceRef , ProjectionElem } ;
76use rustc_middle:: ty:: { self , Ty , TyCtxt } ;
87use rustc_middle:: {
@@ -646,14 +645,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
646645 }
647646 let hir_map = self . infcx . tcx . hir ( ) ;
648647 let def_id = self . body . source . def_id ( ) ;
649- let hir_id = hir_map. local_def_id_to_hir_id ( def_id. as_local ( ) . unwrap ( ) ) ;
650- let node = hir_map. find ( hir_id) ;
651- let Some ( hir:: Node :: Item ( item) ) = node else {
652- return ;
653- } ;
654- let hir:: ItemKind :: Fn ( .., body_id) = item. kind else {
655- return ;
656- } ;
648+ let Some ( local_def_id) = def_id. as_local ( ) else { return } ;
649+ let Some ( body_id) = hir_map. maybe_body_owned_by ( local_def_id) else { return } ;
657650 let body = self . infcx . tcx . hir ( ) . body ( body_id) ;
658651
659652 let mut v = V { assign_span : span, err, ty, suggested : false } ;
@@ -790,23 +783,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
790783 // In the future, attempt in all path but initially for RHS of for_loop
791784 fn suggest_similar_mut_method_for_for_loop ( & self , err : & mut Diagnostic ) {
792785 use hir:: {
793- BodyId , Expr ,
786+ Expr ,
794787 ExprKind :: { Block , Call , DropTemps , Match , MethodCall } ,
795- HirId , ImplItem , ImplItemKind , Item , ItemKind ,
796788 } ;
797789
798- fn maybe_body_id_of_fn ( hir_map : Map < ' _ > , id : HirId ) -> Option < BodyId > {
799- match hir_map. find ( id) {
800- Some ( Node :: Item ( Item { kind : ItemKind :: Fn ( _, _, body_id) , .. } ) )
801- | Some ( Node :: ImplItem ( ImplItem { kind : ImplItemKind :: Fn ( _, body_id) , .. } ) ) => {
802- Some ( * body_id)
803- }
804- _ => None ,
805- }
806- }
807790 let hir_map = self . infcx . tcx . hir ( ) ;
808- let mir_body_hir_id = self . mir_hir_id ( ) ;
809- if let Some ( fn_body_id) = maybe_body_id_of_fn ( hir_map, mir_body_hir_id) {
791+ if let Some ( body_id) = hir_map. maybe_body_owned_by ( self . mir_def_id ( ) ) {
810792 if let Block (
811793 hir:: Block {
812794 expr :
@@ -840,7 +822,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
840822 ..
841823 } ,
842824 _,
843- ) = hir_map. body ( fn_body_id ) . value . kind
825+ ) = hir_map. body ( body_id ) . value . kind
844826 {
845827 let opt_suggestions = self
846828 . infcx
@@ -1102,46 +1084,45 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
11021084 }
11031085 let hir_map = self . infcx . tcx . hir ( ) ;
11041086 let def_id = self . body . source . def_id ( ) ;
1105- let hir_id = hir_map. local_def_id_to_hir_id ( def_id. expect_local ( ) ) ;
1106- let node = hir_map. find ( hir_id) ;
1107- let hir_id = if let Some ( hir:: Node :: Item ( item) ) = node
1108- && let hir:: ItemKind :: Fn ( .., body_id) = item. kind
1109- {
1110- let body = hir_map. body ( body_id) ;
1111- let mut v = BindingFinder {
1112- span : err_label_span,
1113- hir_id : None ,
1087+ let hir_id = if let Some ( local_def_id) = def_id. as_local ( ) &&
1088+ let Some ( body_id) = hir_map. maybe_body_owned_by ( local_def_id)
1089+ {
1090+ let body = hir_map. body ( body_id) ;
1091+ let mut v = BindingFinder {
1092+ span : err_label_span,
1093+ hir_id : None ,
1094+ } ;
1095+ v. visit_body ( body) ;
1096+ v. hir_id
1097+ } else {
1098+ None
11141099 } ;
1115- v. visit_body ( body) ;
1116- v. hir_id
1117- } else {
1118- None
1119- } ;
1100+
11201101 if let Some ( hir_id) = hir_id
11211102 && let Some ( hir:: Node :: Local ( local) ) = hir_map. find ( hir_id)
1122- {
1123- let ( changing, span, sugg) = match local. ty {
1124- Some ( ty) => ( "changing" , ty. span , message) ,
1125- None => (
1126- "specifying" ,
1127- local. pat . span . shrink_to_hi ( ) ,
1128- format ! ( ": {message}" ) ,
1129- ) ,
1130- } ;
1131- err. span_suggestion_verbose (
1132- span,
1133- format ! ( "consider {changing} this binding's type" ) ,
1134- sugg,
1135- Applicability :: HasPlaceholders ,
1136- ) ;
1137- } else {
1138- err. span_label (
1139- err_label_span,
1140- format ! (
1141- "consider changing this binding's type to be: `{message}`"
1142- ) ,
1143- ) ;
1144- }
1103+ {
1104+ let ( changing, span, sugg) = match local. ty {
1105+ Some ( ty) => ( "changing" , ty. span , message) ,
1106+ None => (
1107+ "specifying" ,
1108+ local. pat . span . shrink_to_hi ( ) ,
1109+ format ! ( ": {message}" ) ,
1110+ ) ,
1111+ } ;
1112+ err. span_suggestion_verbose (
1113+ span,
1114+ format ! ( "consider {changing} this binding's type" ) ,
1115+ sugg,
1116+ Applicability :: HasPlaceholders ,
1117+ ) ;
1118+ } else {
1119+ err. span_label (
1120+ err_label_span,
1121+ format ! (
1122+ "consider changing this binding's type to be: `{message}`"
1123+ ) ,
1124+ ) ;
1125+ }
11451126 }
11461127 None => { }
11471128 }
0 commit comments