RS.CLIPPY.MISSING_CONST_FOR_THREAD_LOCAL

Suggest using `const` in `thread_local!` macro

This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: missing_const_for_thread_local. Copyright ©2025 The Rust Team. All rights reserved.

What it does

Suggests to use const in thread_local! macro if possible.

Why is this bad?

The thread_local! macro wraps static declarations and makes them thread-local. It supports using a const keyword that may be used for declarations that can be evaluated as a constant expression. This can enable a more efficient thread local implementation that can avoid lazy initialization. For types that do not need to be dropped, this can enable an even more efficient implementation that does not need to track any additional state.

https://doc.rust-lang.org/std/macro.thread_local.html

Example

thread_local! {
    static BUF: String = String::new();
}

Use instead:

thread_local! {
    static BUF: String = const { String::new() };
}

Past names

  • thread_local_initializer_can_be_made_const