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

needless_borrow and explicit_auto_deref false positive with unions #9383

Closed
Arc-blroth opened this issue Aug 26, 2022 · 0 comments · Fixed by #9490
Closed

needless_borrow and explicit_auto_deref false positive with unions #9383

Arc-blroth opened this issue Aug 26, 2022 · 0 comments · Fixed by #9490
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

Comments

@Arc-blroth
Copy link

Summary

Clippy suggests removing explicit &mut and * derefs that are required by RFC 2514 when working with union fields.

Lint Name

needless_borrow and explicit_auto_deref

Reproducer

I tried this code:

use std::mem::ManuallyDrop;

union Coral {
    crab: ManuallyDrop<Vec<i32>>,
}

union Ocean {
    coral: ManuallyDrop<Coral>,
}

fn main() {
    let mut ocean = Ocean {
        coral: ManuallyDrop::new(Coral {
            crab: ManuallyDrop::new(vec![1, 2, 3]),
        }),
    };

    unsafe {
        // this expression borrows a value the compiler would automatically borrow
        ManuallyDrop::drop(&mut (&mut ocean.coral).crab);
        
        // deref which would be done by auto-deref
        (*ocean.coral).crab = ManuallyDrop::new(vec![4, 5, 6]);
        ManuallyDrop::drop(&mut (*ocean.coral).crab);
        
        ManuallyDrop::drop(&mut ocean.coral);
    }
}

I saw this happen:

warning: this expression borrows a value the compiler would automatically borrow
  --> src/main.rs:20:33
   |
20 |         ManuallyDrop::drop(&mut (&mut ocean.coral).crab);
   |                                 ^^^^^^^^^^^^^^^^^^ help: change this to: `ocean.coral`
   |
   = 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: deref which would be done by auto-deref
  --> src/main.rs:23:9
   |
23 |         (*ocean.coral).crab = ManuallyDrop::new(vec![4, 5, 6]);
   |         ^^^^^^^^^^^^^^ help: try this: `ocean.coral`
   |
   = note: `#[warn(clippy::explicit_auto_deref)]` on by default
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#explicit_auto_deref

warning: deref which would be done by auto-deref
  --> src/main.rs:24:33
   |
24 |         ManuallyDrop::drop(&mut (*ocean.coral).crab);
   |                                 ^^^^^^^^^^^^^^ help: try this: `ocean.coral`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#explicit_auto_deref

I expected to see this happen:

No warnings, since both derefs are not automatically done by the compiler.

Version

rustc 1.65.0-nightly (86c6ebee8 2022-08-16)
binary: rustc
commit-hash: 86c6ebee8fa0a5ad1e18e375113b06bd2849b634
commit-date: 2022-08-16
host: x86_64-pc-windows-msvc
release: 1.65.0-nightly
LLVM version: 15.0.0

Additional Labels

No response

@Arc-blroth Arc-blroth 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 26, 2022
@bors bors closed this as completed in 8845f82 Sep 28, 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-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant