Skip to content

mut_mutex_lock emitted on a &mut &Mutex<T> #13201

@dodomorandi

Description

@dodomorandi

Summary

The mut_mutex_lock is emitted when there is a unique reference to a shared reference of a mutex and the lock method is called. This is obviously a false positive.

Lint Name

mut_mutex_lock

Reproducer

I tried this code:

use std::sync::Mutex;

fn test(mutex: &mut &Mutex<i32>) {
    let _x = mutex.lock().unwrap();
}

I saw this happen:

warning: calling `&mut Mutex::lock` unnecessarily locks an exclusive (mutable) reference
 --> src/lib.rs:4:20
  |
4 |     let _x = 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

I expected to see this happen:

No lints are triggered.

Version

rustc 1.82.0-nightly (28a58f2fa 2024-07-31)
binary: rustc
commit-hash: 28a58f2fa7f0c46b8fab8237c02471a915924fe5
commit-date: 2024-07-31
host: x86_64-unknown-linux-gnu
release: 1.82.0-nightly
LLVM version: 19.1.0

Additional Labels

@rustbot label +I-suggestion-causes-error

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedS-fixedStatus: Issues that got fixed, waiting to be closed as completed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions