rust-lang / rust-lang/rust-clippy
Warn of `..Default::default()`
Open
@WeiTheShinobi is already working on this.
Since Mar 14, 2025.
A-lint
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
Warns when struct initialization uses ..Default::default() pattern, to prevent silent initialization of newly added fields (with unwanted values)
Using ..Default::default() can hide field initialization when new fields are added to structs, potentially leading to bugs where developers forget to explicitly set values for new fields.
Advantages
- Prevents accidental use of default values when new fields are added
- Forces developers to make conscious decisions about field values
- Makes code changes more visible during review
Drawbacks
- More boilerplate code required
- Longer struct initializations
- Slows down initial development
- Could be difficult to implement in large existing codebases
Example
struct Config {
host: String,
port: u16,
}
let config = Config {
host: "localhost".to_string(),
..Default::default()
};
Could be written as:
let config = Config {
host: "localhost".to_string(),
port: 8080,
};
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.
Assessment
This issue has not been assessed yet.