Skip to content

move recommendation with closure and invalid lifetime is inserted at the wrong place #113087

Closed
@sosthene-nitrokey

Description

@sosthene-nitrokey

Code

#[derive(Default)]
struct A {}

struct B<'a> {
    a: &'a mut A,
}

struct C<'a, 'b> {
    b: &'b mut B<'a>,
}

fn some_fn<'a>(b: &mut B<'a>, f: impl FnOnce(&C<'a, '_>)) {}

fn main() {
    let some_closure = |_fs| {};

    for _ in [1] {
        let mut a = Default::default();
        let mut b = B { a: &mut a };
        some_fn(&mut b, |c| {
            some_closure(c);
        });
    }
}

Current output

error[E0373]: closure may outlive the current block, but it borrows `a`, which is owned by the current block
  --> src/main.rs:19:28
   |
19 |         let mut b = B { a: &mut a };
   |                            ^^^^^^
   |                            |
   |                            `a` is borrowed here
   |                            may outlive borrowed value `a`
   |
note: block requires argument type to outlive `'1`
  --> src/main.rs:21:13
   |
21 |             some_closure(c);
   |             ^^^^^^^^^^^^
help: to force the closure to take ownership of `a` (and any other referenced variables), use the `move` keyword
   |
19 |         let mut b = B { a: move &mut a };
   |                            ++++

Desired output

move should not be recommended as it does not solve any issue. This is the case even if put at the closure definition since the closure does not capture anything.

Rationale and extra context

The recommendation for the move is wrong.

Other cases

No response

Anything else?

Playground link.

Rust version:

Nightly version: 1.72.0-nightly

(2023-06-21 065a1f5df9c2f1d93269)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.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