RS.CLIPPY.MUTEX_ATOMIC
Using a mutex where an atomic value could be used instead.
This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: mutex_atomic. Copyright ©2025 The Rust Team. All rights reserved.
What it does
Checks for usage of Mutex<X> where an atomic will do.
Why restrict this?
Using a mutex just to make access to a plain bool or
reference sequential is shooting flies with cannons.
std::sync::atomic::AtomicBool and std::sync::atomic::AtomicPtr are leaner and
faster.
On the other hand, Mutexes are, in general, easier to
verify correctness. An atomic does not behave the same as
an equivalent mutex. See this issue's
commentary for more details.
Known problems
- This lint cannot detect if the mutex is actually used for waiting before a critical section.
- This lint has a false positive that warns without considering the case
where
Mutexis used together withCondvar.
Example
let x = Mutex::new(&y);
Use instead:
let x = AtomicBool::new(y);