Change:
Given the following code: The current output is: The actually needed fix here is the following, which should ideally be recommended <!-- TRIAGEBOTSTART --<!-- TRIAGEBOTASSIGNSTART --<!-- TRIAGEBOTASSIGNDATASTARTTRIAGEBOTASSIGNDATAEND --<!-- TRIAGEBOTASSIGNEND --<!-- TRIAGEBOTEND --
slightly more minimal:
claim
I came up with a solution of printing an extra line of to give user a recommendation of fixing the error by adding 'move' keyword before the closure parameter of map(). The following is the output for the first testing code. Is this good enough? This is the output from the second testing code ():
Refined the fix with a verbose suggestion of where to add keyword. The output of the first test case is: The output of the second test case is: In addition, the fix will also add the suggestion for nested closure block like this test case: The output of the above test case is:

Discussion:
executing...", ); diag.note("...therefore, they cannot allow references to captured variables to escape"); <ins> self.suggest_move_on_borrowing_closure(&mut diag); </ins> diag }

self.add_static_impl_trait_suggestion(&mut diag, *fr, fr_name, *outlived_fr); self.suggest_adding_lifetime_params(&mut diag, *fr, *outlived_fr); <ins> self.suggest_move_on_borrowing_closure(&mut diag); </ins> diag }

suggest_adding_lifetime_params(self.infcx.tcx, sub, ty_sup, ty_sub, diag); } <ins> fn suggest_move_on_borrowing_closure(&self, diag: &mut Diagnostic) { let map = self.infcx.tcx.hir(); let body_id = map.body_owned_by(self.mir_def_id()); let expr = &map.body(body_id).value; let mut closure_span = None::<rustc_span::Span>; match expr.kind { hir::ExprKind::MethodCall(.., args, _) => { // only the first closre parameter of the method. args[0] is MethodCall PathSegment for i in 1..args.len() { if let hir::ExprKind::Closure(..) = args[i].kind { closure_span = Some(args[i].span.shrink_to_lo()); break; } } } hir::ExprKind::Block(blk, _) => { if let Some(ref expr) = blk.expr { // only when the block is a closure if let hir::ExprKind::Closure(..) = expr.kind { closure_span = Some(expr.span.shrink_to_lo()); } } } _ => {} } if let Some(closure_span) = closure_span { diag.span_suggestion_verbose( closure_span, format!("consider adding 'move' keyword before the nested closure"), "move ", Applicability::MaybeIncorrect, ); } } </ins> }

| = note: `FnMut` closures only have access to their captured variables while they are executing... = note: ...therefore, they cannot allow references to captured variables to escape <ins> help: consider adding 'move' keyword before the nested closure | LL |                move || { |                ++++ </ins> error[E0503]: cannot use `f.x` because it was mutably borrowed --> $DIR/borrowck-describe-lvalue.rs:37:9

|         ^^^^^^ returning this value requires that `'1` must outlive `'2` | = note: closure implements `Fn`, so references to captured variables can't escape the closure <ins> help: consider adding 'move' keyword before the nested closure | LL |         move || f() // The `nested` closure |         ++++ </ins> error: aborting due to previous error

<ins> fn foo1(s: &str) -> impl Iterator<Item = String> + '_ { None.into_iter() .flat_map(move |()| s.chars().map(|c| format!("{}{}", c, s))) //~^ ERROR captured variable cannot escape `FnMut` closure body //~| HELP consider adding 'move' keyword before the nested closure } fn foo2(s: &str) -> impl Sized + '_ { move |()| s.chars().map(|c| format!("{}{}", c, s)) //~^ ERROR lifetime may not live long enough //~| HELP consider adding 'move' keyword before the nested closure } fn main() {} </ins>

<ins> error: captured variable cannot escape `FnMut` closure body --> $DIR/issue-95079-missing-move-in-nested-closure.rs:3:29 | LL | fn foo1(s: &str) -> impl Iterator<Item = String> + '_ { |         - variable defined here LL |     None.into_iter() LL |         .flat_map(move |()| s.chars().map(|c| format!("{}{}", c, s))) |                           - -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |                           | | |                           | returns a reference to a captured variable which escapes the closure body |                           | variable captured here |                           inferred to be a `FnMut` closure | = note: `FnMut` closures only have access to their captured variables while they are executing... = note: ...therefore, they cannot allow references to captured variables to escape help: consider adding 'move' keyword before the nested closure | LL |         .flat_map(move |()| s.chars().map(move |c| format!("{}{}", c, s))) |                                           ++++ error: lifetime may not live long enough --> $DIR/issue-95079-missing-move-in-nested-closure.rs:9:15 | LL |     move |()| s.chars().map(|c| format!("{}{}", c, s)) |     --------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ returning this value requires that `'1` must outlive `'2` |     |       | |     |       return type of closure `Map<Chars<'_>, [closure@$DIR/issue-95079-missing-move-in-nested-closure.rs:9:29: 9:32]>` contains a lifetime `'2` |     lifetime `'1` represents this closure's body | = note: closure implements `Fn`, so references to captured variables can't escape the closure help: consider adding 'move' keyword before the nested closure | LL |     move |()| s.chars().map(move |c| format!("{}{}", c, s)) |                             ++++ error: aborting due to 2 previous errors </ins>

| = note: `FnMut` closures only have access to their captured variables while they are executing... = note: ...therefore, they cannot allow references to captured variables to escape <ins> help: consider adding 'move' keyword before the nested closure | LL |         move || { |         ++++ </ins> error: aborting due to previous error