rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
96.73k stars 12.5k forks source link

`single_use_lifetimes`: false positive (suggests unstable anonymous lifetimes in `impl Trait`) #129255

Open ivan-aksamentov opened 3 weeks ago

ivan-aksamentov commented 3 weeks ago

Consider this code (playground):

#[warn(single_use_lifetimes)]

// warning: lifetime parameter `'a` only used once
fn foo<'a>(x: impl IntoIterator<Item = &'a i32>) -> Vec<i32> {
  x.into_iter().copied().collect()
}

fn main() {
  foo(&[1, 2, 3]);
}

The warning is issued: warning: lifetime parameter `'a` only used once. However, removing the lifetime as suggested makes code to not compile:

#[warn(single_use_lifetimes)]

// error[E0658]: anonymous lifetimes in `impl Trait` are unstable
fn foo(x: impl IntoIterator<Item = &i32>) -> Vec<i32> {
  x.into_iter().copied().collect()
}

fn main() {
  foo(&[1, 2, 3]);
}

So the compiler contradicts itself.

I suspect single_use_lifetimes does not correctly verify whether the compiler feature is available. My understanding is that it will be stabilized in 2024 edition (https://github.com/rust-lang/rust/issues/117587).

Meta

If you're using the stable version of the compiler, you should also check if the bug also exists in the beta or nightly versions.

I tried in the playground by setting "nightly" and both 2021 and 2024 editions, and the problem is currently still there.

ChayimFriedman2 commented 3 weeks ago

@rustbot label +A-lint +F-lint-single_use_lifetimes +D-incorrect -needs-triage