@@ -325,25 +325,17 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
325
325
self . cannot_move_out_of ( span, & description)
326
326
}
327
327
328
- fn suggest_clone_of_captured_var_in_move_closure (
328
+ pub ( in crate :: diagnostics ) fn suggest_clone_of_captured_var_in_move_closure (
329
329
& self ,
330
330
err : & mut Diag < ' _ > ,
331
- upvar_hir_id : HirId ,
332
331
upvar_name : & str ,
333
332
use_spans : Option < UseSpans < ' tcx > > ,
334
333
) {
335
334
let tcx = self . infcx . tcx ;
336
- let typeck_results = tcx. typeck ( self . mir_def_id ( ) ) ;
337
335
let Some ( use_spans) = use_spans else { return } ;
338
336
// We only care about the case where a closure captured a binding.
339
337
let UseSpans :: ClosureUse { args_span, .. } = use_spans else { return } ;
340
338
let Some ( body_id) = tcx. hir_node ( self . mir_hir_id ( ) ) . body_id ( ) else { return } ;
341
- // Fetch the type of the expression corresponding to the closure-captured binding.
342
- let Some ( captured_ty) = typeck_results. node_type_opt ( upvar_hir_id) else { return } ;
343
- if !self . implements_clone ( captured_ty) {
344
- // We only suggest cloning the captured binding if the type can actually be cloned.
345
- return ;
346
- } ;
347
339
// Find the closure that captured the binding.
348
340
let mut expr_finder = FindExprBySpan :: new ( args_span, tcx) ;
349
341
expr_finder. include_closures = true ;
@@ -396,7 +388,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
396
388
. indentation_before ( stmt. span )
397
389
. unwrap_or_else ( || " " . to_string ( ) ) ;
398
390
err. multipart_suggestion_verbose (
399
- "clone the value before moving it into the closure" ,
391
+ "consider cloning the value before moving it into the closure" ,
400
392
vec ! [
401
393
(
402
394
stmt. span. shrink_to_lo( ) ,
@@ -426,7 +418,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
426
418
. indentation_before ( closure_expr. span )
427
419
. unwrap_or_else ( || " " . to_string ( ) ) ;
428
420
err. multipart_suggestion_verbose (
429
- "clone the value before moving it into the closure" ,
421
+ "consider cloning the value before moving it into the closure" ,
430
422
vec ! [
431
423
(
432
424
closure_expr. span. shrink_to_lo( ) ,
@@ -523,20 +515,12 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
523
515
) ;
524
516
525
517
let closure_span = tcx. def_span ( def_id) ;
526
- let mut err = self
527
- . cannot_move_out_of ( span, & place_description)
518
+ self . cannot_move_out_of ( span, & place_description)
528
519
. with_span_label ( upvar_span, "captured outer variable" )
529
520
. with_span_label (
530
521
closure_span,
531
522
format ! ( "captured by this `{closure_kind}` closure" ) ,
532
- ) ;
533
- self . suggest_clone_of_captured_var_in_move_closure (
534
- & mut err,
535
- upvar_hir_id,
536
- & upvar_name,
537
- use_spans,
538
- ) ;
539
- err
523
+ )
540
524
}
541
525
_ => {
542
526
let source = self . borrowed_content_source ( deref_base) ;
@@ -597,7 +581,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
597
581
} ;
598
582
599
583
if let Some ( expr) = self . find_expr ( span) {
600
- self . suggest_cloning ( err, place_ty, expr, None ) ;
584
+ self . suggest_cloning ( err, move_from . as_ref ( ) , place_ty, expr, None ) ;
601
585
}
602
586
603
587
err. subdiagnostic ( crate :: session_diagnostics:: TypeNoCopy :: Label {
@@ -629,7 +613,13 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
629
613
} ;
630
614
631
615
if let Some ( expr) = self . find_expr ( use_span) {
632
- self . suggest_cloning ( err, place_ty, expr, Some ( use_spans) ) ;
616
+ self . suggest_cloning (
617
+ err,
618
+ original_path. as_ref ( ) ,
619
+ place_ty,
620
+ expr,
621
+ Some ( use_spans) ,
622
+ ) ;
633
623
}
634
624
635
625
err. subdiagnostic ( crate :: session_diagnostics:: TypeNoCopy :: Label {
@@ -832,7 +822,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
832
822
let place_desc = self . local_name ( * local) . map ( |sym| format ! ( "`{sym}`" ) ) ;
833
823
834
824
if let Some ( expr) = self . find_expr ( binding_span) {
835
- self . suggest_cloning ( err, bind_to. ty , expr, None ) ;
825
+ let local_place: PlaceRef < ' tcx > = ( * local) . into ( ) ;
826
+ self . suggest_cloning ( err, local_place, bind_to. ty , expr, None ) ;
836
827
}
837
828
838
829
err. subdiagnostic ( crate :: session_diagnostics:: TypeNoCopy :: Label {
0 commit comments