RS.CLIPPY.MUTEX_INTEGER

Using a mutex for an integer type

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_integer. Copyright ©2025 The Rust Team. All rights reserved.

What it does

Checks for usage of Mutex<X> where X is an integral type.

Why restrict this?

Using a mutex just to make access to a plain integer sequential is shooting flies with cannons. std::sync::atomic::AtomicUsize is 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 Mutex is used together with Condvar.
  • This lint suggest using AtomicU64 instead of Mutex<u64>, but AtomicU64 is not available on some 32-bit platforms.

Example

let x = Mutex::new(0usize);

Use instead:

let x = AtomicUsize::new(0usize);