rust-lang / rust-lang/rust

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

Open
#143,131 2 comments 0 reactions 1 assignee View on GitHub

@sgasho is already working on this.

Since May 5, 2026.

A-higher-ranked A-lifetimes A-lints A-suggestion-diagnostics C-bug D-invalid-suggestion L-false-positive L-single_use_lifetimes T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.