Skip to content

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

Description

@ivan-aksamentov

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 (#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.

Activity

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

Metadata

Metadata

Assignees

Labels

A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.L-single_use_lifetimesLint: single_use_lifetimes

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions