@@ -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 ) ]
@@ -1081,6 +1081,38 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
1081
1081
}
1082
1082
}
1083
1083
1084
+ /// Finds all statements that assign directly to local (i.e., X = ...) and returns their
1085
+ /// locations.
1086
+ fn find_assignments ( & self , local : Local ) -> Vec < Location > {
1087
+ use rustc_middle:: mir:: visit:: Visitor ;
1088
+
1089
+ struct FindLocalAssignmentVisitor {
1090
+ needle : Local ,
1091
+ locations : Vec < Location > ,
1092
+ }
1093
+
1094
+ impl < ' tcx > Visitor < ' tcx > for FindLocalAssignmentVisitor {
1095
+ fn visit_local (
1096
+ & mut self ,
1097
+ local : Local ,
1098
+ place_context : PlaceContext ,
1099
+ location : Location ,
1100
+ ) {
1101
+ if self . needle != local {
1102
+ return ;
1103
+ }
1104
+
1105
+ if place_context. is_place_assignment ( ) {
1106
+ self . locations . push ( location) ;
1107
+ }
1108
+ }
1109
+ }
1110
+
1111
+ let mut visitor = FindLocalAssignmentVisitor { needle : local, locations : vec ! [ ] } ;
1112
+ visitor. visit_body ( self . body ) ;
1113
+ visitor. locations
1114
+ }
1115
+
1084
1116
fn suggest_make_local_mut ( & self , err : & mut Diag < ' _ > , local : Local , name : Symbol ) {
1085
1117
let local_decl = & self . body . local_decls [ local] ;
1086
1118
@@ -1114,7 +1146,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
1114
1146
} ) ) => {
1115
1147
// check if the RHS is from desugaring
1116
1148
let opt_assignment_rhs_span =
1117
- self . body . find_assignments ( local) . first ( ) . map ( |& location| {
1149
+ self . find_assignments ( local) . first ( ) . map ( |& location| {
1118
1150
if let Some ( mir:: Statement {
1119
1151
source_info : _,
1120
1152
kind :
0 commit comments