rust-lang / rust-lang/rust-clippy

FP explicit_deref_methods mutability

Open
#14,933 2 comments 0 reactions 1 assignee View on GitHub

@profetia is already working on this.

Since May 31, 2025.

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

Description

Summary

.

Lint Name

explicit_deref_methods

Reproducer

I tried this code:

#![warn(clippy::explicit_deref_methods)]
use std::pin::Pin;
use std::sync::Mutex;
use std::ops::Deref;

pub struct Stderr(Mutex<()>);

pub fn poll_write(x: Pin<&mut Stderr>) {
    let _ = &mut *Deref::deref(&x).0.lock().unwrap();
}

pub fn main() {}

I saw this happen:

warning: explicit `deref` method call
 --> src/main.rs:9:19
  |
9 |     let _ = &mut *Deref::deref(&x).0.lock().unwrap();
  |                   ^^^^^^^^^^^^^^^^ help: try: `&**&x`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#explicit_deref_methods
note: the lint level is defined here
 --> src/main.rs:1:9
  |
1 | #![warn(clippy::explicit_deref_methods)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

the suggested code.

#![warn(clippy::explicit_deref_methods)]
use std::pin::Pin;
use std::sync::Mutex;
use std::ops::Deref;

pub struct Stderr(Mutex<()>);

pub fn poll_write(x: Pin<&mut Stderr>) {
    let _ = &mut *&**&x.0.lock().unwrap();
}

pub fn main() {}

does not compile:

error[E0596]: cannot borrow data in a `&` reference as mutable
 --> src/main.rs:9:13
  |
9 |     let _ = &mut *&**&x.0.lock().unwrap();
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot borrow as mutable

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

Additional Labels

No response

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.