Clippy lints / Restriction
Clippy lint · Restriction · checked with Clippy 0.1.99
clippy::mutex_atomic: Checks for usage of Mutex<X> where an atomic will do
What it meansChecks for usage of Mutex<X> where an atomic will do. It is in the restriction group and off by default.
Does it matter?
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.
Lints that forbid language features for specific codebases (no unwrap, no indexing...). Off by default; pick them one by one, never the whole group.
How to fix it
Clippy does not fix this one automatically (its suggestion may change behaviour, so it only shows it). Change the code by hand:
Flagged
let x = Mutex::new(&y);
Write this instead
let x = AtomicBool::new(y);
Turn it off, or on
It is off by default. To use it, set it to warn or deny:
Whole crate (Cargo.toml)
[lints.clippy]
mutex_atomic = "warn"In code
#![warn(clippy::mutex_atomic)]One run
cargo clippy -- -W clippy::mutex-atomicThe [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.
Clippy's own documentation
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);
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- unwrap_usedChecks for .unwrap() or .unwrap_err() calls on Results and .unwrap() call on Options
- module_name_repetitionsDetects public item names that are prefixed or suffixed by the containing public module’s name
- expect_usedChecks for .expect() or .expect_err() calls on Results and .expect() call on Options
- modulo_arithmeticyou are using modulo operator on constants with different signs: `-17 % 3`
- multiple_inherent_implmultiple implementations of this structure
- multiple_unsafe_ops_per_blockthis `unsafe` block contains 2 unsafe operations, expected only one
- mutex_integerChecks for usage of Mutex<X> where X is an integral type
- needless_raw_stringsunnecessary raw string literal
- non_ascii_literalliteral non-ASCII character detected
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::unwrap_usedChecks for .unwrap() or .unwrap_err() calls on Results and .unwrap() call on OptionsRead it next →Did this answer your question?