@@ -325,25 +325,17 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
325325 self . cannot_move_out_of ( span, & description)
326326 }
327327
328- fn suggest_clone_of_captured_var_in_move_closure (
328+ pub ( in crate :: diagnostics ) fn suggest_clone_of_captured_var_in_move_closure (
329329 & self ,
330330 err : & mut Diag < ' _ > ,
331- upvar_hir_id : HirId ,
332331 upvar_name : & str ,
333332 use_spans : Option < UseSpans < ' tcx > > ,
334333 ) {
335334 let tcx = self . infcx . tcx ;
336- let typeck_results = tcx. typeck ( self . mir_def_id ( ) ) ;
337335 let Some ( use_spans) = use_spans else { return } ;
338336 // We only care about the case where a closure captured a binding.
339337 let UseSpans :: ClosureUse { args_span, .. } = use_spans else { return } ;
340338 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- } ;
347339 // Find the closure that captured the binding.
348340 let mut expr_finder = FindExprBySpan :: new ( args_span, tcx) ;
349341 expr_finder. include_closures = true ;
@@ -396,7 +388,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
396388 . indentation_before ( stmt. span )
397389 . unwrap_or_else ( || " " . to_string ( ) ) ;
398390 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" ,
400392 vec ! [
401393 (
402394 stmt. span. shrink_to_lo( ) ,
@@ -426,7 +418,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
426418 . indentation_before ( closure_expr. span )
427419 . unwrap_or_else ( || " " . to_string ( ) ) ;
428420 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" ,
430422 vec ! [
431423 (
432424 closure_expr. span. shrink_to_lo( ) ,
@@ -523,20 +515,12 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
523515 ) ;
524516
525517 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)
528519 . with_span_label ( upvar_span, "captured outer variable" )
529520 . with_span_label (
530521 closure_span,
531522 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+ )
540524 }
541525 _ => {
542526 let source = self . borrowed_content_source ( deref_base) ;
@@ -597,7 +581,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
597581 } ;
598582
599583 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 ) ;
601585 }
602586
603587 err. subdiagnostic ( crate :: session_diagnostics:: TypeNoCopy :: Label {
@@ -629,7 +613,13 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
629613 } ;
630614
631615 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+ ) ;
633623 }
634624
635625 err. subdiagnostic ( crate :: session_diagnostics:: TypeNoCopy :: Label {
@@ -832,7 +822,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
832822 let place_desc = self . local_name ( * local) . map ( |sym| format ! ( "`{sym}`" ) ) ;
833823
834824 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 ) ;
836827 }
837828
838829 err. subdiagnostic ( crate :: session_diagnostics:: TypeNoCopy :: Label {
0 commit comments