Clippy lints / Performance
Clippy lint · Performance · checked with Clippy 0.1.99
clippy::readonly_write_lock: this write lock is used only for reading
What it meansLooks for calls to RwLock::write where the lock is only used for reading. It is in the performance group and warns by default.
What you see
warning: this write lock is used only for reading --> main.rs:5:15 | 5 | let num = lock.write().unwrap(); | ^^^^^^^^^^^^ help: consider using a read lock instead: `lock.read()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#readonly_write_lock = note: `#[warn(clippy::readonly_write_lock)]` 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?
The write portion of RwLock is exclusive, meaning that no other thread can access the lock while this writer is active.
Code that could be written to run faster. Warns by default.
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
use std::sync::RwLock;
fn assert_is_zero(lock: &RwLock<i32>) {
let num = lock.write().unwrap();
assert_eq!(*num, 0);
}
Write this instead
use std::sync::RwLock;
fn assert_is_zero(lock: &RwLock<i32>) {
let num = lock.read().unwrap();
assert_eq!(*num, 0);
}
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::readonly_write_lock)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
readonly_write_lock = "allow"One run
cargo clippy -- -A clippy::readonly-write-lockThe [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
Looks for calls to RwLock::write where the lock is only used for reading.
Why is this bad?
The write portion of RwLock is exclusive, meaning that no other thread
can access the lock while this writer is active.
Example
use std::sync::RwLock;
fn assert_is_zero(lock: &RwLock<i32>) {
let num = lock.write().unwrap();
assert_eq!(*num, 0);
}
Use instead:
use std::sync::RwLock;
fn assert_is_zero(lock: &RwLock<i32>) {
let num = lock.read().unwrap();
assert_eq!(*num, 0);
}
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- large_enum_variantlarge size difference between variants
- map_entryusage of `contains_key` followed by `insert` on a `HashMap`
- missing_const_for_thread_localinitializer for `thread_local` value can be made `const`
- missing_spin_loopbusy-waiting loop should at least have a spin loop hint
- redundant_allocationChecks for usage of redundant allocations anywhere in the code
- redundant_iter_clonedunneeded cloning of iterator items
- regex_creation_in_loopsChecks for regex compilation inside a loop with a literal
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::large_enum_variantlarge size difference between variantsRead it next →Did this answer your question?