Skip to content

declare_interior_mutable_const shouldn't fire for const thread-local initializers #8493

@goffrie

Description

@goffrie

Summary

thread_local! has an optimization for const initializers. Also, thread-locals often have interior mutability. When using these two features together, the declare_interior_mutable_const lint fires, as it detects an interior-mutable const item. Which is true, but it is not being used in an incorrect manner; the use happens inside of the thread_local! macro.

Lint Name

declare_interior_mutable_const

Reproducer

use std::cell::Cell; thread_local! { static THREAD_LOCAL: Cell<i32> = const { Cell::new(0) }; }

I saw this happen:

warning: a `const` item should never be interior mutable --> src/lib.rs:2:1 | 2 | / thread_local! { 3 | | static THREAD_LOCAL: Cell<i32> = const { Cell::new(0) }; 4 | | } | |_^ | = note: `#[warn(clippy::declare_interior_mutable_const)]` on by default = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#declare_interior_mutable_const = note: this warning originates in the macro `$crate::__thread_local_inner` (in Nightly builds, run with -Z macro-backtrace for more info) 

Version

rustc 1.60.0-nightly (bd3cb5256 2022-01-16) binary: rustc commit-hash: bd3cb52565faab2755ff1bdb54d88bc91f47b4b9 commit-date: 2022-01-16 host: x86_64-apple-darwin release: 1.60.0-nightly LLVM version: 13.0.0 

Additional Labels

No response

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions