Skip to content

Commit

Permalink
Describe the order of fields in struct ctor doesn't affect the result…
Browse files Browse the repository at this point in the history
…ed instance
  • Loading branch information
Y-Nak committed Feb 22, 2021
1 parent d646aa2 commit bfdf0fa
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions clippy_lints/src/inconsistent_struct_constructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,23 @@ declare_clippy_lint! {
/// **What it does:** Checks for struct constructors where the order of the field init
/// shorthand in the constructor is inconsistent with the order in the struct definition.
///
/// **Why is this bad?** It decreases readability and consistency.
/// **Why is this bad?** Since the order of fields in a constructor doesn't affect the
/// resulted instance as the below example indicates,
///
/// ```rust
/// #[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 means nothing and just decreases readability and consistency.
///
/// **Known problems:** None.
///
Expand Down Expand Up @@ -74,12 +90,12 @@ impl LateLintPass<'_> for InconsistentStructConstructor {
for ident in idents {
fields_snippet.push_str(&format!("{}, ", ident));
}
fields_snippet.push_str(&format!("{}", last_ident));
fields_snippet.push_str(&last_ident.to_string());

let base_snippet = if let Some(base) = base {
format!(", ..{}", snippet(cx, base.span, ".."))
} else {
"".to_string()
String::new()
};

let sugg = format!("{} {{ {}{} }}",
Expand Down

0 comments on commit bfdf0fa

Please sign in to comment.