Open
Description
Summary
.
Lint Name
unnecessary_struct_initialization
Reproducer
I tried this code:
struct S(i32);
// We should evaluate `x[2]` and copy the value out *before* evaluating the LHS
// and changing its value.
fn _fun() {
let mut x = &[S(0), S(1), S(2)][..];
let y = &mut S(7);
*{
x = &[S(3), S(4), S(5)];
&mut *y
} = S { ..x[2] };
assert_eq!(2, y.0);
assert_eq!(5, x[2].0);
}
pub fn main() {}
I saw this happen:
warning: unnecessary struct building
--> src/main.rs:11:9
|
11 | } = S { ..x[2] };
| ^^^^^^^^^^^^ help: replace with: `x[2]`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_struct_initialization
= note: `#[warn(clippy::unnecessary_struct_initialization)]` on by default
The suggested code does not compile:
The following errors were reported:
error[E0508]: cannot move out of type `[S]`, a non-copy slice
--> src/main.rs:11:9
|
11 | } = x[2];
| ^^^^
| |
| cannot move out of here
| move occurs because `x[_]` has type `S`, which does not implement the `Copy` trait
error: aborting due to previous error
For more information about this error, try `rustc --explain E0508`.
Original diagnostics will follow.
Version
rustc 1.70.0-nightly (8be3c2bda 2023-03-24)
binary: rustc
commit-hash: 8be3c2bda6b683f87b24714ba595e8b04faef54c
commit-date: 2023-03-24
host: x86_64-unknown-linux-gnu
release: 1.70.0-nightly
LLVM version: 15.0.7
Additional Labels
No response