Skip to content

Mutex and RwLock borrow suggestions could suggest get_mut if applicable #141058

Open
@zopsicle

Description

@zopsicle

Code

use std::sync::{Mutex, RwLock};

fn f(t: &mut (Mutex<Vec<()>>, RwLock<Vec<()>>))
{
    t.0.len();
    t.1.len();
}

Current output

error[E0599]: no method named `len` found for struct `Mutex` in the current scope
    --> src/lib.rs:5:9
     |
5    |     t.0.len();
     |         ^^^ method not found in `Mutex<Vec<()>>`
     |
note: the method `len` exists on the type `Vec<()>`
    --> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/vec/mod.rs:2705:5
     |
2705 |     pub const fn len(&self) -> usize {
     |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: use `.lock().unwrap()` to borrow the `Vec<()>`, blocking the current thread until it can be acquired
     |
5    |     t.0.lock().unwrap().len();
     |        ++++++++++++++++

error[E0599]: no method named `len` found for struct `RwLock` in the current scope
    --> src/lib.rs:6:9
     |
6    |     t.1.len();
     |         ^^^ method not found in `RwLock<Vec<()>>`
     |
note: the method `len` exists on the type `Vec<()>`
    --> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/vec/mod.rs:2705:5
     |
2705 |     pub const fn len(&self) -> usize {
     |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: use `.read().unwrap()` to borrow the `Vec<()>`, blocking the current thread until it can be acquired
     |
6    |     t.1.read().unwrap().len();
     |        ++++++++++++++++

For more information about this error, try `rustc --explain E0599`.

Desired output

error[E0599]: no method named `len` found for struct `Mutex` in the current scope
    --> src/lib.rs:5:9
     |
5    |     t.0.len();
     |         ^^^ method not found in `Mutex<Vec<()>>`
     |
note: the method `len` exists on the type `Vec<()>`
    --> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/vec/mod.rs:2705:5
     |
2705 |     pub const fn len(&self) -> usize {
     |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: use `.get_mut().unwrap()` to borrow the `Vec<()>`
     |
5    |     t.0.get_mut().unwrap().len();
     |        ++++++++++++++++

error[E0599]: no method named `len` found for struct `RwLock` in the current scope
    --> src/lib.rs:6:9
     |
6    |     t.1.len();
     |         ^^^ method not found in `RwLock<Vec<()>>`
     |
note: the method `len` exists on the type `Vec<()>`
    --> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/vec/mod.rs:2705:5
     |
2705 |     pub const fn len(&self) -> usize {
     |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: use `.get_mut().unwrap()` to borrow the `Vec<()>`
     |
6    |     t.1.get_mut().unwrap().len();
     |        ++++++++++++++++

For more information about this error, try `rustc --explain E0599`.

Rationale and extra context

If it is possible to call get_mut, there is no reason to call lock or read instead, as they just do the same thing* less efficiently in this context.

* Except if a guard is mem::forgetten, this will succeed instead of block indefinitely, but whatever.

Other cases

Rust Version

rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: x86_64-unknown-linux-gnu
release: 1.86.0
LLVM version: 19.1.7
Compiler returned: 0

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions