Skip to content

FP explicit_deref_methods mutability #14933

Open
@matthiaskrgr

Description

@matthiaskrgr

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

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions