@@ -167,10 +167,18 @@ impl OptimizationFinder<'b, 'tcx> {
167
167
return None ;
168
168
}
169
169
170
- self . optimizations
171
- . unneeded_deref
172
- . insert ( location, * place_taken_address_of) ;
173
- return Some ( ( ) ) ;
170
+ if place_derefs_non_mutable_ref (
171
+ place_taken_address_of,
172
+ self . body ,
173
+ self . tcx ,
174
+ ) {
175
+ self . optimizations
176
+ . unneeded_deref
177
+ . insert ( location, * place_taken_address_of) ;
178
+ return Some ( ( ) ) ;
179
+ }
180
+
181
+ return None ;
174
182
}
175
183
176
184
// We found an assignment of `local_being_deref` that is not an immutable ref, e.g the following sequence
@@ -258,17 +266,29 @@ impl OptimizationFinder<'b, 'tcx> {
258
266
}
259
267
}
260
268
269
+ /// Returns whether this place derefences a type `&_`
270
+ fn place_derefs_non_mutable_ref < ' tcx > (
271
+ place : & Place < ' tcx > ,
272
+ body : & Body < ' tcx > ,
273
+ tcx : TyCtxt < ' tcx > ,
274
+ ) -> bool {
275
+ if let PlaceRef { local, projection : & [ ref proj_base @ .., ProjectionElem :: Deref ] } =
276
+ place. as_ref ( )
277
+ {
278
+ let ty = Place :: ty_from ( local, proj_base, body, tcx) . ty ;
279
+ // The dereferenced place must have type `&_`.
280
+ if let ty:: Ref ( _, _, Mutability :: Not ) = ty. kind ( ) {
281
+ return true ;
282
+ }
283
+ }
284
+ return false ;
285
+ }
286
+
261
287
impl Visitor < ' tcx > for OptimizationFinder < ' b , ' tcx > {
262
288
fn visit_rvalue ( & mut self , rvalue : & Rvalue < ' tcx > , location : Location ) {
263
289
if let Rvalue :: Ref ( _, _, place) = rvalue {
264
- if let PlaceRef { local, projection : & [ ref proj_base @ .., ProjectionElem :: Deref ] } =
265
- place. as_ref ( )
266
- {
267
- // The dereferenced place must have type `&_`.
268
- let ty = Place :: ty_from ( local, proj_base, self . body , self . tcx ) . ty ;
269
- if let ty:: Ref ( _, _, Mutability :: Not ) = ty. kind ( ) {
270
- self . optimizations . and_stars . insert ( location) ;
271
- }
290
+ if place_derefs_non_mutable_ref ( place, self . body , self . tcx ) {
291
+ self . optimizations . and_stars . insert ( location) ;
272
292
}
273
293
}
274
294
0 commit comments