rust-lang / rust-lang/rust-clippy
unnecessary-struct-initialization changes code to require type annotations (which are not provided)
Open
Nobody has claimed this yet.
C-bug
I-suggestion-causes-error
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
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_
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the unnecessary_struct_initialization lint and the supplied Rust reproducer, then inspect how its suggestion is generated and applied. Done means --fix leaves a compiling program without requiring an unavailable type annotation, with a regression test covering the non-generic struct case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100