RS.CLIPPY.READONLY_WRITE_LOCK

Acquiring a write lock when a read lock would work

This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: readonly_write_lock. Copyright ©2025 The Rust Team. All rights reserved.

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);
}