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

FP unnecessary_struct_initialization, suggestion does not compile #10547

Open
matthiaskrgr opened this issue Mar 25, 2023 · 1 comment
Open
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

@matthiaskrgr
Copy link
Member

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

@matthiaskrgr matthiaskrgr added 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 I-false-positive Issue: The lint was triggered on code it shouldn't have labels Mar 25, 2023
@Alexendoo
Copy link
Member

More generally it doesn't handle place expressions where the value cannot be moved out of, or where moving it would cause another error

#![allow(unused, clippy::no_effect)]

struct A {
    x: i32
}

fn borrowed(a: A) {
    let b = &a;
    A { ..a };
    b;
}

fn would_be_moved(a: A) {
    A { ..a };
    A { ..a };
}

struct S {
    a: A
}

fn field(s: &S) {
    A {..s.a}
}

bors added a commit that referenced this issue Mar 27, 2023
…rsery, r=flip1995

Move unnecessary_struct_initialization to nursery

changelog: none, assuming it makes into the same release as #10489

Mostly because of #10547 but there is also #10548
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