-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Open
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedIssue: The suggestions provided by this Lint cause an ICE/error when applied
Description
Summary
(Extracted from #9854 (comment))
Given a struct like:
struct Wrapper<T>(Mutex<T>);the lint fires in the following situation:
fn foo<T>(w: &Wrapper<T>) {
let t = w.0.lock();
}even though replacing that with get_mut() isn't actually possible, as w is behind &.
Reproducer (playground)
Lint Name
mut_mutex_lock
Reproducer
I tried this code:
use std::sync::Mutex;
struct Struct<'a> {
ref_mut_mutex: &'a mut Mutex<i32>,
}
impl PartialEq for Struct<'_> {
fn eq(&self, other: &Self) -> bool {
let x = self.ref_mut_mutex.lock().unwrap();
// The following suggested line doesn't compile:
// let x = self.ref_mut_mutex.get_mut().unwrap();
let y = other.ref_mut_mutex.lock().unwrap();
*x == *y
}
}I saw this happen:
warning: calling `&mut Mutex::lock` unnecessarily locks an exclusive (mutable) reference
--> src/lib.rs:9:36
|
9 | let x = self.ref_mut_mutex.lock().unwrap();
| ^^^^ help: change this to: `get_mut`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#mut_mutex_lock
= note: `#[warn(clippy::mut_mutex_lock)]` on by default
warning: calling `&mut Mutex::lock` unnecessarily locks an exclusive (mutable) reference
--> src/lib.rs:12:37
|
12 | let y = other.ref_mut_mutex.lock().unwrap();
| ^^^^ help: change this to: `get_mut`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#mut_mutex_lock
I expected to see this happen:
no warning, as self, and therefore ref_mut_mutex, is behind an immutable reference.
Version
rustc 1.92.0 (ded5c06cf 2025-12-08)
binary: rustc
commit-hash: ded5c06cf21d2b93bffd5d884aa6e96934ee4234
commit-date: 2025-12-08
host: x86_64-unknown-linux-gnu
release: 1.92.0
LLVM version: 21.1.3
Additional Labels
@rustbot label I-suggestion-causes-error
Metadata
Metadata
Assignees
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedIssue: The suggestions provided by this Lint cause an ICE/error when applied