Struct field overwritten immediately after Default::default()

ID

rust.maintainability.field_reassign_with_default

Severity

info

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Redundant Code

Language

Rust

Description

Reports a binding initialised from Default::default() whose field is overwritten on the very next statement, so the default value for that field is computed and then thrown away.

Rationale

The value the struct really carries is spread across several statements, so the reader has to replay them to find out what it ends up as, and the binding has to be mut for the length of the function even though it is only being built. Building it in one expression states the final shape in one place and lets the compiler check that nothing is missed.

The following code illustrates the pattern detected by this rule:

#[derive(Default)]
pub struct Config {
    pub port: u16,
    pub host: String,
    pub debug: bool,
}

pub fn listener() -> Config {
    // FLAGGED: Struct field overwritten immediately after Default::default()
    let mut config = Config::default();
    config.port = 8080;
    config
}

Remediation

Use struct update syntax - let config = Config { port: 8080, ..Default::default() }; - or list every field explicitly. Both drop the mut and put the whole value on one expression.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved