-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inconsistent struct constructor #6769
Conversation
c706d74
to
4adea7d
Compare
4adea7d
to
d646aa2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general this looks good to me.
I ran lintcheck with your changes (cargo dev lintcheck
), the lint only triggered 3 times in our set of crates but all of them looked like true positives to me 👍
/// **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. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// | ||
/// ```rust | ||
/// struct Foo { | ||
/// x: i32, | ||
/// y: i32, | ||
/// } | ||
/// let x = 1; | ||
/// let y = 2; | ||
/// Foo { y, x }; | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```rust | ||
/// # struct Foo { | ||
/// # x: i32, | ||
/// # y: i32, | ||
/// # } | ||
/// # let x = 1; | ||
/// # let y = 2; | ||
/// Foo { x, y }; | ||
/// ``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should add some kind of short demonstration that Foo {x, y}
and Foo {y,x}
are the same?
#[derive(Eq, PartialEq, Debug)]
struct Foo {x: i32, y: i32}
fn main() {
let x = 0;
let y = 1;
assert_eq!(Foo {x, y}, Foo {y, x});
}
} | ||
fields_snippet.push_str(&format!("{}", last_ident)); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm shouldn't this trigger clippy::useless_format
...? 🤔
This can be just
fields_snippet.push_str(&last_ident.to_string());
let base_snippet = if let Some(base) = base { | ||
format!(", ..{}", snippet(cx, base.span, "..")) | ||
} else { | ||
"".to_string() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: String::new()
Changes:
|
Thanks! |
📌 Commit bfdf0fa has been approved by |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
fixes: #6352
r? @matthiaskrgr
I added the lint that checks for the struct constructors where the order of the field init shorthands is inconsistent with that in the struct definition.
changelog: Add style lint:
inconsistent_struct_constructor