Skip to content

Commit bfdf0fa

Browse files
committed
Describe the order of fields in struct ctor doesn't affect the resulted instance
1 parent d646aa2 commit bfdf0fa

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

clippy_lints/src/inconsistent_struct_constructor.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,23 @@ declare_clippy_lint! {
1313
/// **What it does:** Checks for struct constructors where the order of the field init
1414
/// shorthand in the constructor is inconsistent with the order in the struct definition.
1515
///
16-
/// **Why is this bad?** It decreases readability and consistency.
16+
/// **Why is this bad?** Since the order of fields in a constructor doesn't affect the
17+
/// resulted instance as the below example indicates,
18+
///
19+
/// ```rust
20+
/// #[derive(Debug, PartialEq, Eq)]
21+
/// struct Foo {
22+
/// x: i32,
23+
/// y: i32,
24+
/// }
25+
/// let x = 1;
26+
/// let y = 2;
27+
///
28+
/// // This assertion never fails.
29+
/// assert_eq!(Foo { x, y }, Foo { y, x });
30+
/// ```
31+
///
32+
/// inconsistent order means nothing and just decreases readability and consistency.
1733
///
1834
/// **Known problems:** None.
1935
///
@@ -74,12 +90,12 @@ impl LateLintPass<'_> for InconsistentStructConstructor {
7490
for ident in idents {
7591
fields_snippet.push_str(&format!("{}, ", ident));
7692
}
77-
fields_snippet.push_str(&format!("{}", last_ident));
93+
fields_snippet.push_str(&last_ident.to_string());
7894

7995
let base_snippet = if let Some(base) = base {
8096
format!(", ..{}", snippet(cx, base.span, ".."))
8197
} else {
82-
"".to_string()
98+
String::new()
8399
};
84100

85101
let sugg = format!("{} {{ {}{} }}",

0 commit comments

Comments
 (0)