Rollup merge of #101285 - TaKO8Ki:do-not-suggest-adding-move-when-closure-is-already-marked-as-move, r=oli-obk
Do not suggest adding `move` to closure when `move` is already used Fixes #101227
This commit is contained in:
commit
1bafe0b020
3 changed files with 37 additions and 3 deletions
|
@ -902,7 +902,11 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
|
||||||
hir::ExprKind::MethodCall(.., args, _) => {
|
hir::ExprKind::MethodCall(.., args, _) => {
|
||||||
// only the first closre parameter of the method. args[0] is MethodCall PathSegment
|
// only the first closre parameter of the method. args[0] is MethodCall PathSegment
|
||||||
for i in 1..args.len() {
|
for i in 1..args.len() {
|
||||||
if let hir::ExprKind::Closure(..) = args[i].kind {
|
if let hir::ExprKind::Closure(hir::Closure {
|
||||||
|
capture_clause: hir::CaptureBy::Ref,
|
||||||
|
..
|
||||||
|
}) = args[i].kind
|
||||||
|
{
|
||||||
closure_span = Some(args[i].span.shrink_to_lo());
|
closure_span = Some(args[i].span.shrink_to_lo());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -911,7 +915,11 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
|
||||||
hir::ExprKind::Block(blk, _) => {
|
hir::ExprKind::Block(blk, _) => {
|
||||||
if let Some(ref expr) = blk.expr {
|
if let Some(ref expr) = blk.expr {
|
||||||
// only when the block is a closure
|
// only when the block is a closure
|
||||||
if let hir::ExprKind::Closure(..) = expr.kind {
|
if let hir::ExprKind::Closure(hir::Closure {
|
||||||
|
capture_clause: hir::CaptureBy::Ref,
|
||||||
|
..
|
||||||
|
}) = expr.kind
|
||||||
|
{
|
||||||
closure_span = Some(expr.span.shrink_to_lo());
|
closure_span = Some(expr.span.shrink_to_lo());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -921,7 +929,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
|
||||||
if let Some(closure_span) = closure_span {
|
if let Some(closure_span) = closure_span {
|
||||||
diag.span_suggestion_verbose(
|
diag.span_suggestion_verbose(
|
||||||
closure_span,
|
closure_span,
|
||||||
format!("consider adding 'move' keyword before the nested closure"),
|
"consider adding 'move' keyword before the nested closure",
|
||||||
"move ",
|
"move ",
|
||||||
Applicability::MaybeIncorrect,
|
Applicability::MaybeIncorrect,
|
||||||
);
|
);
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
fn main() {
|
||||||
|
let mut vec: Vec<i32> = Vec::new();
|
||||||
|
let closure = move || {
|
||||||
|
vec.clear();
|
||||||
|
let mut iter = vec.iter();
|
||||||
|
move || { iter.next() } //~ ERROR captured variable cannot escape `FnMut` closure bod
|
||||||
|
};
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
error: captured variable cannot escape `FnMut` closure body
|
||||||
|
--> $DIR/do-not-suggest-adding-move-when-closure-is-already-marked-as-move.rs:6:9
|
||||||
|
|
|
||||||
|
LL | let mut vec: Vec<i32> = Vec::new();
|
||||||
|
| ------- variable defined here
|
||||||
|
LL | let closure = move || {
|
||||||
|
| - inferred to be a `FnMut` closure
|
||||||
|
LL | vec.clear();
|
||||||
|
| --- variable captured here
|
||||||
|
LL | let mut iter = vec.iter();
|
||||||
|
LL | move || { iter.next() }
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^ returns a closure that contains a reference to a captured variable, which then escapes the closure body
|
||||||
|
|
|
||||||
|
= 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
|
||||||
|
|
||||||
|
error: aborting due to previous error
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue