RS.CLIPPY.INCONSISTENT_STRUCT_CONSTRUCTOR
The order of the field init is inconsistent with the order in the struct definition
This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: inconsistent_struct_constructor. Copyright ©2025 The Rust Team. All rights reserved.
What it does
Checks for struct constructors where the order of the field init in the constructor is inconsistent with the order in the struct definition.
Why is this bad?
Since the order of fields in a constructor doesn't affect the resulted instance as the below example indicates,
#[derive(Debug, PartialEq, Eq)]
struct Foo {
x: i32,
y: i32,
}
let x = 1;
let y = 2;
// This assertion never fails:
assert_eq!(Foo { x, y }, Foo { y, x });
inconsistent order can be confusing and decreases readability and consistency.
Example
struct Foo {
x: i32,
y: i32,
}
let x = 1;
let y = 2;
Foo { y, x };
Use instead:
Foo { x, y };
Configuration
check-inconsistent-struct-field-initializers: Whether to suggest reordering constructor fields when initializers are present.
Warnings produced by this configuration aren't necessarily fixed by just reordering the fields. Even if the suggested code would compile, it can change semantics if the initializer expressions have side effects. The following example from rust-clippy#11846 shows how the suggestion can run into borrow check errors:
struct MyStruct {
vector: Vec<u32>,
length: usize
}
fn main() {
let vector = vec![1,2,3];
MyStruct { length: vector.len(), vector};
}
(default: false)