@@ -10,6 +10,7 @@ use rustc_hir::intravisit::Visitor;
10
10
use rustc_hir:: { self as hir, BindingMode , ByRef , Node } ;
11
11
use rustc_middle:: bug;
12
12
use rustc_middle:: hir:: place:: PlaceBase ;
13
+ use rustc_middle:: mir:: visit:: PlaceContext ;
13
14
use rustc_middle:: mir:: {
14
15
self , BindingForm , Local , LocalDecl , LocalInfo , LocalKind , Location , Mutability , Place ,
15
16
PlaceRef , ProjectionElem ,
@@ -22,7 +23,6 @@ use rustc_trait_selection::traits;
22
23
use tracing:: debug;
23
24
24
25
use crate :: diagnostics:: BorrowedContentSource ;
25
- use crate :: util:: FindAssignments ;
26
26
use crate :: { MirBorrowckCtxt , session_diagnostics} ;
27
27
28
28
#[ derive( Copy , Clone , Debug , Eq , PartialEq ) ]
@@ -1084,6 +1084,38 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
1084
1084
}
1085
1085
}
1086
1086
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
+
1087
1119
fn suggest_make_local_mut ( & self , err : & mut Diag < ' _ > , local : Local , name : Symbol ) {
1088
1120
let local_decl = & self . body . local_decls [ local] ;
1089
1121
@@ -1117,7 +1149,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
1117
1149
} ) ) => {
1118
1150
// check if the RHS is from desugaring
1119
1151
let opt_assignment_rhs_span =
1120
- self . body . find_assignments ( local) . first ( ) . map ( |& location| {
1152
+ self . find_assignments ( local) . first ( ) . map ( |& location| {
1121
1153
if let Some ( mir:: Statement {
1122
1154
source_info : _,
1123
1155
kind :
0 commit comments