Skip to content

Invalid suggestion on E0507  #67190

Closed
@hellow554

Description

@hellow554
struct NotCopyable;

fn func<F: FnMut() -> H, H: FnMut()>(_: F) {}

fn parse() {
    let mut var = None;
    func(|| {
        move || {
            var = Some(NotCopyable);
        }
    });
}

gives the error:

error[E0507]: cannot move out of `var`, a captured variable in an `FnMut` closure
 --> src/lib.rs:8:9
  |
6 |     let mut var= None;
  |         --------- captured outer variable
7 |     func(|| {
8 |         move || {
  |         ^^^^^^^
  |         |
  |         move out of `var` occurs here
  |         help: consider borrowing the `Option`'s content: `move ||.as_ref()`
9 |             var = Some(NotCopyable);
  |             -----
  |             |
  |             move occurs because `var` has type `std::option::Option<NotCopyable>`, which does not implement the `Copy` trait
  |             move occurs due to use in closure

Error0507 gives a wrong suggestion here:

help: consider borrowing the `Option`'s content: `move ||.as_ref()`

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-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