Skip to content

unnecessary-struct-initialization changes code to require type annotations (which are not provided) #10548

Open
@matthiaskrgr

Description

@matthiaskrgr

Summary

.

Reproducer

I tried this code:

#[derive(Default)]
struct NonGeneric {
    field1: usize,
}

fn main() {
    let default1 = NonGeneric { ..Default::default() };
}

clippy suggestion:

warning: unnecessary struct building
 --> src/main.rs:9:20
  |
9 |     let default1 = NonGeneric { ..Default::default() };
  |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `Default::default()`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_struct_initialization
```

This gets auto applied with `--fix` but it does not compile since we are lacking type annotations for `Default`

```
The following errors were reported:
error[E0282]: type annotations needed
 --> src/main.rs:9:9
  |
9 |     let _default1 = Default::default();
  |         ^^^^^^^^^
  |
help: consider giving `_default1` an explicit type
  |
9 |     let _default1: /* Type */ = Default::default();
  |                  ++++++++++++

error: aborting due to previous error
```

### Version

```text
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_

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions