@@ -10,6 +10,7 @@ use rustc_hir::intravisit::Visitor;
1010use rustc_hir:: { self as hir, BindingMode , ByRef , Node } ;
1111use rustc_middle:: bug;
1212use rustc_middle:: hir:: place:: PlaceBase ;
13+ use rustc_middle:: mir:: visit:: PlaceContext ;
1314use rustc_middle:: mir:: {
1415 self , BindingForm , Local , LocalDecl , LocalInfo , LocalKind , Location , Mutability , Place ,
1516 PlaceRef , ProjectionElem ,
@@ -22,7 +23,6 @@ use rustc_trait_selection::traits;
2223use tracing:: debug;
2324
2425use crate :: diagnostics:: BorrowedContentSource ;
25- use crate :: util:: FindAssignments ;
2626use crate :: { MirBorrowckCtxt , session_diagnostics} ;
2727
2828#[ derive( Copy , Clone , Debug , Eq , PartialEq ) ]
@@ -1084,6 +1084,38 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
10841084 }
10851085 }
10861086
1087+ /// Finds all statements that assign directly to local (i.e., X = ...) and returns their
1088+ /// locations.
1089+ fn find_assignments ( & self , local : Local ) -> Vec < Location > {
1090+ use rustc_middle:: mir:: visit:: Visitor ;
1091+
1092+ struct FindLocalAssignmentVisitor {
1093+ needle : Local ,
1094+ locations : Vec < Location > ,
1095+ }
1096+
1097+ impl < ' tcx > Visitor < ' tcx > for FindLocalAssignmentVisitor {
1098+ fn visit_local (
1099+ & mut self ,
1100+ local : Local ,
1101+ place_context : PlaceContext ,
1102+ location : Location ,
1103+ ) {
1104+ if self . needle != local {
1105+ return ;
1106+ }
1107+
1108+ if place_context. is_place_assignment ( ) {
1109+ self . locations . push ( location) ;
1110+ }
1111+ }
1112+ }
1113+
1114+ let mut visitor = FindLocalAssignmentVisitor { needle : local, locations : vec ! [ ] } ;
1115+ visitor. visit_body ( self . body ) ;
1116+ visitor. locations
1117+ }
1118+
10871119 fn suggest_make_local_mut ( & self , err : & mut Diag < ' _ > , local : Local , name : Symbol ) {
10881120 let local_decl = & self . body . local_decls [ local] ;
10891121
@@ -1117,7 +1149,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
11171149 } ) ) => {
11181150 // check if the RHS is from desugaring
11191151 let opt_assignment_rhs_span =
1120- self . body . find_assignments ( local) . first ( ) . map ( |& location| {
1152+ self . find_assignments ( local) . first ( ) . map ( |& location| {
11211153 if let Some ( mir:: Statement {
11221154 source_info : _,
11231155 kind :
0 commit comments