RS.CLIPPY.MUT_MUTEX_LOCK
`&mut Mutex::lock` does unnecessary locking
This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: mut_mutex_lock. Copyright ©2025 The Rust Team. All rights reserved.
What it does
Checks for &mut Mutex::lock calls
Why is this bad?
Mutex::lock is less efficient than
calling Mutex::get_mut. In addition you also have a statically
guarantee that the mutex isn't locked, instead of just a runtime
guarantee.
Example
use std::sync::{Arc, Mutex};
let mut value_rc = Arc::new(Mutex::new(42_u8));
let value_mutex = Arc::get_mut(&mut value_rc).unwrap();
let mut value = value_mutex.lock().unwrap();
*value += 1;
Use instead:
use std::sync::{Arc, Mutex};
let mut value_rc = Arc::new(Mutex::new(42_u8));
let value_mutex = Arc::get_mut(&mut value_rc).unwrap();
let value = value_mutex.get_mut().unwrap();
*value += 1;