Skip to content

Warn of ..Default::default() #14382

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

Open
dorschw opened this issue Mar 9, 2025 · 1 comment · May be fixed by #14412
Open

Warn of ..Default::default() #14382

dorschw opened this issue Mar 9, 2025 · 1 comment · May be fixed by #14412
Assignees
Labels
A-lint Area: New lints

Comments

@dorschw
Copy link

dorschw commented Mar 9, 2025

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,
};
@dorschw dorschw added the A-lint Area: New lints label Mar 9, 2025
@dorschw dorschw changed the title Prevent ..Default::default() Warn of ..Default::default() Mar 9, 2025
@WeiTheShinobi
Copy link
Contributor

WeiTheShinobi commented Mar 14, 2025

We have the same idea.

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants