Skip to content

Bad suggestion for &mut *(&mut &mut T) from clippy::needless_borrow #8191

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

Closed
SarthakSingh31 opened this issue Dec 28, 2021 · 0 comments · Fixed by #8217
Closed

Bad suggestion for &mut *(&mut &mut T) from clippy::needless_borrow #8191

SarthakSingh31 opened this issue Dec 28, 2021 · 0 comments · Fixed by #8217
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@SarthakSingh31
Copy link

Summary

clippy::needless_borrow suggested a change that only works if the compiler was going to reborrow the reference or the reference could be moved/copied. Since neither conditions were true the suggested code produced an error.

Reproducer

I originally encounter the issue here
https://github.com/bevyengine/bevy/blob/601cc0cbe3dc73c0db1993184f4ec47783e83884/crates/bevy_ecs/src/bundle.rs#L492

A reduced version:

struct Inner {
    num: u8,
}

enum Outer<'a> {
    InnerBorrowed(&'a mut Inner),
    _InnerOwned(Inner),
}

impl<'a> Outer<'a> {
    fn get_inner_mut(&mut self) -> &mut Inner {
        let inner = match self {
            Outer::InnerBorrowed(inner) => &mut *inner,
            Outer::_InnerOwned(inner) => inner,
        };
        inner.num += 1;
        inner
    }
}

fn main() {
    let mut inner = Inner { num: 0 };
    let mut outer = Outer::InnerBorrowed(&mut inner);
    let _inner_inside_outer = outer.get_inner_mut();
}

I expected to see this happen:

cargo clippy --fix should change Outer::InnerBorrowed(inner) => &mut *inner, into Outer::InnerBorrowed(inner) => inner,

Instead, this happened:

cargo clippy --fix tries to change Outer::InnerBorrowed(inner) => &mut *inner, into Outer::InnerBorrowed(inner) => *inner, and then produces the following console output

Console Output
❯ cargo clippy --fix                                 
    Checking test-234234 v0.1.0 (/extra/Projects/test)
warning: failed to automatically apply fixes suggested by rustc to crate `test_234234`

after fixes were automatically applied the compiler reported errors within these files:

* src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see 
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0507]: cannot move out of `*inner` which is behind a mutable reference
--> src/main.rs:13:44
|
13 |             Outer::InnerBorrowed(inner) => *inner,
|                                            ^^^^^^
|                                            |
|                                            move occurs because `*inner` has type `&mut Inner`, which does not implement the `Copy` trait
|                                            help: consider borrowing here: `&*inner`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0507`.
Original diagnostics will follow.

warning: this expression borrows a reference (`&mut Inner`) that is immediately dereferenced by the compiler
--> src/main.rs:13:44
|
13 |             Outer::InnerBorrowed(inner) => &mut *inner,
|                                            ^^^^^^^^^^^ help: change this to: `*inner`
|
= note: `#[warn(clippy::needless_borrow)]` on by default
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow

warning: `test-234234` (bin "test-234234") generated 1 warning (1 duplicate)
warning: `test-234234` (bin "test-234234" test) generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.44s

Version

rustc 1.59.0-nightly (f8abed9ed 2021-12-26)
binary: rustc
commit-hash: f8abed9ed48bace6be0087bcd44ed534e239b8d8
commit-date: 2021-12-26
host: x86_64-unknown-linux-gnu
release: 1.59.0-nightly
LLVM version: 13.0.0

Additional Labels

@rustbot label +I-suggestion-causes-error

@SarthakSingh31 SarthakSingh31 added the C-bug Category: Clippy is not doing the correct thing label Dec 28, 2021
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Dec 28, 2021
@bors bors closed this as completed in 788a8bc Jan 23, 2022
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-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants