rust-lang / rust-lang/rust-clippy

`mut_mutex_lock`: FP on `struct.mutex` where `struct` is behind `&`

Open
#16,253 0 comments 0 reactions 1 assignee View on GitHub

@ada4a is already working on this.

Since Dec 18, 2025.

C-bug I-false-positive I-suggestion-causes-error
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

(Extracted from https://github.com/rust-lang/rust-clippy/issues/9854#issuecomment-2624392084)

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.