rust-lang / rust-lang/rust-clippy
Lifetime conficts when recommending closures
@tamaroning is already working on this.
Since May 24, 2022.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.