Closed
Description
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