Clippy lints / Suspicious
Clippy lint · Suspicious · checked with Clippy 0.1.99
clippy::declare_interior_mutable_const: named constant with interior mutability
What it meansChecks for the declaration of named constant which contain interior mutability. It is in the suspicious group and warns by default.
What you see
warning: named constant with interior mutability --> main.rs:4:7 | 4 | const ATOMIC: AtomicUsize = AtomicUsize::new(0); | ^^^^^^ | = help: did you mean to make this a `static` item = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#declare_interior_mutable_const = note: `#[warn(clippy::declare_interior_mutable_const)]` on by default
Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.
Does it matter?
Named constants are copied at every use site which means any change to their value will be lost after the newly created value is dropped. e.g. use core::sync::atomic::{AtomicUsize, Ordering}; const ATOMIC: AtomicUsize = AtomicUsize::new(0); fn add_one() -> usize { // This will always return `0` since `ATOMIC` is copied before it's used. ATOMIC.fetch_add(1, Ordering::AcqRel) } If shared modification of the value is desired, a static item is needed instead. If that is not desired, a const fn constructor should be used to make it obvious at the use site that a new value is created.
Code that is most likely wrong or useless. Warns by default.
How to fix it
Clippy does not fix this one automatically. Change the code by hand:
Flagged
use core::sync::atomic::{AtomicUsize, Ordering};
const ATOMIC: AtomicUsize = AtomicUsize::new(0);
fn add_one() -> usize {
// This will always return `0` since `ATOMIC` is copied before it's used.
ATOMIC.fetch_add(1, Ordering::AcqRel)
}
Write this instead
use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
const CONST_ATOM: AtomicUsize = AtomicUsize::new(12);
CONST_ATOM.store(6, SeqCst); // the content of the atomic is unchanged
assert_eq!(CONST_ATOM.load(SeqCst), 12); // because the CONST_ATOM in these lines are distinct
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::declare_interior_mutable_const)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
declare_interior_mutable_const = "allow"One run
cargo clippy -- -A clippy::declare-interior-mutable-constThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Settings in clippy.toml
ignore-interior-mutability: A list of paths to types that should be treated as if they do not contain interior mutability
Clippy's own documentation
What it does
Checks for the declaration of named constant which contain interior mutability.
Why is this bad?
Named constants are copied at every use site which means any change to their value will be lost after the newly created value is dropped. e.g.
use core::sync::atomic::{AtomicUsize, Ordering};
const ATOMIC: AtomicUsize = AtomicUsize::new(0);
fn add_one() -> usize {
// This will always return `0` since `ATOMIC` is copied before it's used.
ATOMIC.fetch_add(1, Ordering::AcqRel)
}
If shared modification of the value is desired, a static item is needed instead.
If that is not desired, a const fn constructor should be used to make it obvious
at the use site that a new value is created.
Known problems
Prior to const fn stabilization this was the only way to provide a value which
could initialize a static item (e.g. the std::sync::ONCE_INIT constant). In
this case the use of const is required and this lint should be suppressed.
There also exists types which contain private fields with interior mutability, but
no way to both create a value as a constant and modify any mutable field using the
type’s public interface (e.g. bytes::Bytes). As there is no reasonable way to
scan a crate’s interface to see if this is the case, all such types will be linted.
If this happens use the ignore-interior-mutability configuration option to allow
the type.
Example
use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
const CONST_ATOM: AtomicUsize = AtomicUsize::new(12);
CONST_ATOM.store(6, SeqCst); // the content of the atomic is unchanged
assert_eq!(CONST_ATOM.load(SeqCst), 12); // because the CONST_ATOM in these lines are distinct
Use instead:
static STATIC_ATOM: AtomicUsize = AtomicUsize::new(15);
STATIC_ATOM.store(9, SeqCst);
assert_eq!(STATIC_ATOM.load(SeqCst), 9); // use a `static` item to refer to the same instance
Configuration
-
ignore-interior-mutability: A list of paths to types that should be treated as if they do not contain interior mutability(default:
["bytes::Bytes"])
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- const_is_emptythis expression always evaluates to true
- crate_in_macro_def`crate` references the macro call's crate
- crosspointer_transmuteChecks for transmutes between a type T and *T
- deprecated_clippy_cfg_attr`feature = "cargo-clippy"` was replaced by `clippy`
- doc_nested_refdefslink reference defined in list item
- doc_suspicious_footnoteslooks like a footnote ref, but has no matching footnote
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::const_is_emptythis expression always evaluates to trueRead it next →Did this answer your question?