@@ -546,6 +546,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
546
546
executing...",
547
547
) ;
548
548
diag. note ( "...therefore, they cannot allow references to captured variables to escape" ) ;
549
+ self . suggest_move_on_borrowing_closure ( & mut diag) ;
549
550
550
551
diag
551
552
}
@@ -716,6 +717,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
716
717
717
718
self . add_static_impl_trait_suggestion ( & mut diag, * fr, fr_name, * outlived_fr) ;
718
719
self . suggest_adding_lifetime_params ( & mut diag, * fr, * outlived_fr) ;
720
+ self . suggest_move_on_borrowing_closure ( & mut diag) ;
719
721
720
722
diag
721
723
}
@@ -901,4 +903,39 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
901
903
902
904
suggest_adding_lifetime_params ( self . infcx . tcx , sub, ty_sup, ty_sub, diag) ;
903
905
}
906
+
907
+ fn suggest_move_on_borrowing_closure ( & self , diag : & mut Diagnostic ) {
908
+ let map = self . infcx . tcx . hir ( ) ;
909
+ let body_id = map. body_owned_by ( self . mir_def_id ( ) ) ;
910
+ let expr = & map. body ( body_id) . value ;
911
+ let mut closure_span = None :: < rustc_span:: Span > ;
912
+ match expr. kind {
913
+ hir:: ExprKind :: MethodCall ( .., args, _) => {
914
+ // only the first closre parameter of the method. args[0] is MethodCall PathSegment
915
+ for i in 1 ..args. len ( ) {
916
+ if let hir:: ExprKind :: Closure ( ..) = args[ i] . kind {
917
+ closure_span = Some ( args[ i] . span . shrink_to_lo ( ) ) ;
918
+ break ;
919
+ }
920
+ }
921
+ }
922
+ hir:: ExprKind :: Block ( blk, _) => {
923
+ if let Some ( ref expr) = blk. expr {
924
+ // only when the block is a closure
925
+ if let hir:: ExprKind :: Closure ( ..) = expr. kind {
926
+ closure_span = Some ( expr. span . shrink_to_lo ( ) ) ;
927
+ }
928
+ }
929
+ }
930
+ _ => { }
931
+ }
932
+ if let Some ( closure_span) = closure_span {
933
+ diag. span_suggestion_verbose (
934
+ closure_span,
935
+ format ! ( "consider adding 'move' keyword before the nested closure" ) ,
936
+ "move " ,
937
+ Applicability :: MaybeIncorrect ,
938
+ ) ;
939
+ }
940
+ }
904
941
}
0 commit comments