Struct field initialized with a variable of the same name
ID |
rust.maintainability.redundant_field_names |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Redundant Code |
Language |
Rust |
Description
Reports a struct literal field written as name: name, where the initializer is a variable with exactly the field’s name. Field-init shorthand covers that case with the name alone.
Rationale
Repeating the name doubles the length of every field and buries the fields that are actually computed among the ones that are not, so the interesting part of a literal is harder to find. In a wide literal it also means a rename has to be applied twice per field.
The following code illustrates the pattern detected by this rule:
pub struct Point {
x: i32,
y: i32,
}
pub fn make_point(x: i32, y: i32) -> Point {
// FLAGGED: Struct field initialized with a variable of the same name
Point { x: x, y: y }
}