rust-lang / rust-lang/rust-clippy

False positive missing_const_for_thread_local

Open
#13,422 7 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

missing_const_for_thread_local is already solved, but it triggers.

See https://github.com/bevyengine/bevy/issues/15241

Lint Name

missing_const_for_thread_local

Reproducer

I tried this code:

#[cfg(feature = "debug_stack")]
thread_local! {
    /// The thread-local [`TypeInfoStack`] used for debugging.
    ///
    /// [`TypeInfoStack`]: crate::type_info_stack::TypeInfoStack
    pub(super) static TYPE_INFO_STACK: std::cell::RefCell<crate::type_info_stack::TypeInfoStack> = const { std::cell::RefCell::new(
        crate::type_info_stack::TypeInfoStack::new()
    ) };
}

I saw this happen:

warning: initializer for `thread_local` value can be made `const`
  --> crates\bevy_reflect\src\serde\de\error_utils.rs:5:1
   |
5  | / thread_local! {
6  | |     /// The thread-local [`TypeInfoStack`] used for debugging.
7  | |     ///
8  | |     /// [`TypeInfoStack`]: crate::type_info_stack::TypeInfoStack
...  |
11 | |     ) };
12 | | }
   | |_^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#missing_const_for_thread_local
   = note: `#[warn(clippy::missing_const_for_thread_local)]` on by default
   = note: this warning originates in the macro `$crate::thread::local_impl::thread_local_inner` which comes from the expansion of the macro `thread_local` (in Nightly builds, run with -Z macro-backtrace for more info)


I expected to see this happen:
No lint

Version

rustc stable-x86_64-pc-windows-gnu

Additional Labels

No response

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.

Research direction

Start by reproducing the warning with the provided missing_const_for_thread_local example, then locate the lint implementation and its existing regression tests by searching for the lint name. Trace how the thread_local! expansion is analyzed, and add coverage showing that an initializer already using const produces no warning.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.