rust-lang / rust-lang/rust-clippy

Warn of `..Default::default()`

Open
#14,382 1 comment 3 reactions 1 assignee View on GitHub

@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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.