rustc_lint::impl_trait_overcaptures

Static IMPL_TRAIT_OVERCAPTURES

source
pub static IMPL_TRAIT_OVERCAPTURES: &Lint
Expand description

The impl_trait_overcaptures lint warns against cases where lifetime capture behavior will differ in edition 2024.

In the 2024 edition, impl Traits will capture all lifetimes in scope, rather than just the lifetimes that are mentioned in the bounds of the type. Often these sets are equal, but if not, it means that the impl Trait may cause erroneous borrow-checker errors.

§Example

let mut x = vec![];
x.push(1);

fn test(x: &Vec<i32>) -> impl Display {
    x[0]
}

let element = test(&x);
x.push(2);
println!("{element}");

{{produces}}

§Explanation

In edition < 2024, the returned impl Display doesn’t capture the lifetime from the &Vec<i32>, so the vector can be mutably borrowed while the impl Display is live.

To fix this, we can explicitly state that the impl Display doesn’t capture any lifetimes, using impl Display + use<>.