Skip to content

Point to closure when emitting 'cannot move out' for captured variable #87456

Closed
@Aaron1011

Description

@Aaron1011

Given the following code: (playground):

fn take_mut(_val: impl FnMut()) {}

fn main() {
    let val = String::new();
    take_mut(|| {
        let _foo: String = val;
    })
}

The current output is:

error[E0507]: cannot move out of `val`, a captured variable in an `FnMut` closure
 --> src/main.rs:6:28
  |
4 |     let val = String::new();
  |         --- captured outer variable
5 |     take_mut(|| {
6 |         let _foo: String = val;
  |                            ^^^
  |                            |
  |                            move occurs because `val` has type `String`, which does not implement the `Copy` trait
  |                            help: consider borrowing here: `&val`

However, we do not point to the closure, or explain why it implements FnMut. In a larger function, this can be confusing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions