Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

borrow_deref_ref suggestion breaks code #11346

Open
Rimpampa opened this issue Aug 17, 2023 · 1 comment
Open

borrow_deref_ref suggestion breaks code #11346

Rimpampa opened this issue Aug 17, 2023 · 1 comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@Rimpampa
Copy link

Rimpampa commented Aug 17, 2023

Summary

When there is a method (not a freestanding function) that takes self: &mut &Self (or simply &mut self when Self is &T) and such a method is called on an immutable variable of the &Self type, a reborrow is required so that a mutable reference to the copy of the original reference is taken.

The problem is that clippy flags such a reborrow as needless, when in reality, when removed, it causes a compilation error because it is not possible to take a mutable reference to an immutable variable.

Lint Name

borrow_deref_ref

Reproducer

This is the most minimal example I managed to write that reproduces the problem:

struct Struct;

impl Struct {
    fn foo(self: &mut &Self) {}
}

trait Trait {
   fn bar(&mut self) {}
}

impl Trait for &Struct {}

fn main() {
    let s = &Struct;

    (&*s).foo(); // warning: deref on an immutable reference
//  ^^^^^ help: if you would like to reborrow, try removing `&*`: `s`

    (&*s).bar(); // warning: deref on an immutable reference
//  ^^^^^ help: if you would like to reborrow, try removing `&*`: `s`
}

Note that if we remove the need to use the dot operator, the lint doesn't show up:

struct Struct;

fn foo(_: &mut &Struct) {}

fn main() {
    let s = &Struct;
    foo(&mut &*s) // no warning (reborrow still required)
}

Additional Labels

@rustbot label +I-suggestion-causes-error

@Rimpampa Rimpampa added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Aug 17, 2023
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Aug 17, 2023
@Rimpampa
Copy link
Author

I assumed it wasn't the same problem, but #9905 could be related

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

No branches or pull requests

2 participants