Skip to content

false positive: single-use-lifetimes '_ is a reserved lifetime name #143131

@matthiaskrgr

Description

@matthiaskrgr

Using the following flags

--force-warn single-use-lifetimes

this code:

//@ run-pass
// Issue #122622: `#[derive(Clone)]` should work for HRTB function type taking an associated type
#![allow(dead_code)]
trait SomeTrait {
    type SomeType<'a>;
}

#[derive(Clone)]
struct Foo<T: SomeTrait> {
    x: for<'a> fn(T::SomeType<'a>)
}

fn main() {}

caused the following diagnostics:

    Blocking waiting for file lock on package cache
    Blocking waiting for file lock on package cache
    Blocking waiting for file lock on package cache
    Blocking waiting for file lock on package cache
    Checking _derive-hrtb-for-bare-fn-field-with-lifetime v0.1.0 (/tmp/icemaker_global_tempdir.GjvWA1sVwdV5/icemaker_clippyfix_tempdir.IduBJuzrFHf8/_derive-hrtb-for-bare-fn-field-with-lifetime)
warning: lifetime parameter `'a` only used once
  --> src/main.rs:10:12
   |
10 |     x: for<'a> fn(T::SomeType<'a>)
   |            ^^                 -- ...is used only here
   |            |
   |            this lifetime...
   |
   = note: requested on the command line with `--force-warn single-use-lifetimes`
help: elide the single-use lifetime
   |
10 -     x: for<'a> fn(T::SomeType<'a>)
10 +     x: fn(T::SomeType<'_>)
   |

warning: `_derive-hrtb-for-bare-fn-field-with-lifetime` (bin "_derive-hrtb-for-bare-fn-field-with-lifetime") generated 1 warning (run `cargo clippy --fix --bin "_derive-hrtb-for-bare-fn-field-with-lifetime"` to apply 1 suggestion)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.31s

However after applying these diagnostics, the resulting code:

//@ run-pass
// Issue #122622: `#[derive(Clone)]` should work for HRTB function type taking an associated type
#![allow(dead_code)]
trait SomeTrait {
    type SomeType<'a>;
}

#[derive(Clone)]
struct Foo<T: SomeTrait> {
    x: fn(T::SomeType<'_>)
}

fn main() {}

no longer compiled:

    Checking _derive-hrtb-for-bare-fn-field-with-lifetime v0.1.0 (/tmp/icemaker_global_tempdir.GjvWA1sVwdV5/icemaker_clippyfix_tempdir.IduBJuzrFHf8/_derive-hrtb-for-bare-fn-field-with-lifetime)
error[E0637]: `'_` cannot be used here
  --> src/main.rs:10:23
   |
10 |     x: fn(T::SomeType<'_>)
   |                       ^^ `'_` is a reserved lifetime name

error[E0282]: type annotations needed
 --> src/main.rs:8:10
  |
8 | #[derive(Clone)]
  |          ^^^^^ cannot infer type for type parameter `T`

Some errors have detailed explanations: E0282, E0637.
For more information about an error, try `rustc --explain E0282`.
error: could not compile `_derive-hrtb-for-bare-fn-field-with-lifetime` (bin "_derive-hrtb-for-bare-fn-field-with-lifetime") due to 2 previous errors
warning: build failed, waiting for other jobs to finish...
error: could not compile `_derive-hrtb-for-bare-fn-field-with-lifetime` (bin "_derive-hrtb-for-bare-fn-field-with-lifetime" test) due to 2 previous errors

Version:

rustc 1.90.0-nightly (bdaba05a9 2025-06-27)
binary: rustc
commit-hash: bdaba05a953eb5abeba0011cdda2560d157aed2e
commit-date: 2025-06-27
host: x86_64-unknown-linux-gnu
release: 1.90.0-nightly
LLVM version: 20.1.7

Metadata

Metadata

Assignees

Labels

A-higher-rankedArea: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs)A-lifetimesArea: Lifetimes / regionsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.L-false-positiveLint: False positive (should not have fired).L-single_use_lifetimesLint: single_use_lifetimesT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions