@@ -195,8 +195,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantClone {
195
195
let is_temp = mir_read_only. local_kind ( ret_local) == mir:: LocalKind :: Temp ;
196
196
197
197
// 1. `local` can be moved out if it is not used later.
198
- // 2. If `ret_local` is a temporary and is not consumed, we can remove this `clone` call anyway.
199
- let ( used, consumed) = traversal:: ReversePostorder :: new ( & mir, bb) . skip ( 1 ) . fold (
198
+ // 2. If `ret_local` is a temporary and is neither consumed nor mutated, we can remove this `clone`
199
+ // call anyway.
200
+ let ( used, consumed_or_mutated) = traversal:: ReversePostorder :: new ( & mir, bb) . skip ( 1 ) . fold (
200
201
( false , !is_temp) ,
201
202
|( used, consumed) , ( tbb, tdata) | {
202
203
// Short-circuit
@@ -209,14 +210,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantClone {
209
210
210
211
let mut vis = LocalUseVisitor {
211
212
used : ( local, false ) ,
212
- consumed : ( ret_local, false ) ,
213
+ consumed_or_mutated : ( ret_local, false ) ,
213
214
} ;
214
215
vis. visit_basic_block_data ( tbb, tdata) ;
215
- ( used || vis. used . 1 , consumed || vis. consumed . 1 )
216
+ ( used || vis. used . 1 , consumed || vis. consumed_or_mutated . 1 )
216
217
} ,
217
218
) ;
218
219
219
- if !used || !consumed {
220
+ if !used || !consumed_or_mutated {
220
221
let span = terminator. source_info . span ;
221
222
let scope = terminator. source_info . scope ;
222
223
let node = mir. source_scopes [ scope]
@@ -253,7 +254,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantClone {
253
254
if used {
254
255
db. span_note(
255
256
span,
256
- "cloned value is not consumed" ,
257
+ "cloned value is neither consumed nor mutated " ,
257
258
) ;
258
259
} else {
259
260
db. span_note(
@@ -355,7 +356,7 @@ fn base_local_and_movability<'tcx>(
355
356
356
357
struct LocalUseVisitor {
357
358
used : ( mir:: Local , bool ) ,
358
- consumed : ( mir:: Local , bool ) ,
359
+ consumed_or_mutated : ( mir:: Local , bool ) ,
359
360
}
360
361
361
362
impl < ' tcx > mir:: visit:: Visitor < ' tcx > for LocalUseVisitor {
@@ -381,8 +382,14 @@ impl<'tcx> mir::visit::Visitor<'tcx> for LocalUseVisitor {
381
382
self . used . 1 = true ;
382
383
}
383
384
384
- if * local == self . consumed . 0 && matches ! ( ctx, PlaceContext :: NonMutatingUse ( NonMutatingUseContext :: Move ) ) {
385
- self . consumed . 1 = true ;
385
+ if * local == self . consumed_or_mutated . 0 {
386
+ match ctx {
387
+ PlaceContext :: NonMutatingUse ( NonMutatingUseContext :: Move )
388
+ | PlaceContext :: MutatingUse ( MutatingUseContext :: Borrow ) => {
389
+ self . consumed_or_mutated . 1 = true ;
390
+ } ,
391
+ _ => { } ,
392
+ }
386
393
}
387
394
}
388
395
}
0 commit comments