Skip to content

Lifetime conficts when recommending closures #8346

Open

Description

Description

This affects any lint where we recommend moving code into a closure. Off the top of my head are manual_map, option_if_let_else, and map_entry, but there are probably more.

Given the following code:

let mut x = 0;
let ref_x = &mut x;

let _ = match Some(0) {
    Some(y) => Some({
        *ref_x = y;
        some_fn_call(&mut x) + y
    }),
    None => None,
};

Clippy will suggest to use Option::map

let _ = Some(0).map(|y| {
    *ref_x = y;
    some_fn_call(&mut x) + y
});

This will fail with multiple mutable borrows of x due to the closure capturing both ref_x and x. The original code would have the borrow held by ref_x end right before x is borrowed for some_fn_call.

Solving this would require the lifetimes computed by borrowck which I don't believe we have easy access to. It may also require turning these as MIR lints.

Version

No response

Additional Labels

@rustbot label +I-false-positive
@rustbot label +E-hard
@rustbot label +T-MIR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

E-hardCall for participation: This a hard problem and requires more experience or effort to work onI-false-positiveIssue: The lint was triggered on code it shouldn't haveT-MIRType: This lint will require working with the MIR

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions