-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Question re inconsistent_struct_constructor
requirement
#11846
Comments
Something like struct MyStruct {
vector: Vec<u32>,
length: usize
}
fn main() {
let vector = vec![1,2,3];
MyStruct { length: vector.len(), vector};
} won't compile if you switch the order of fields. |
I think maybe I was being unclear. If the example is changed as below, Clippy flags it (playground). I'm suggesting the unmodified example be flagged as well. struct MyStruct {
vector: Vec<u32>,
length: usize
}
fn main() {
let vector = vec![1,2,3];
let length = vector.len();
MyStruct { length, vector};
} |
I would like to remove the "all fields are shorthand" requirement. How should I proceed?
|
Note that the lint is currently MachineApplicable. Do you suggest that the "fixed" version of the example in my previous comment should be the following? struct MyStruct {
vector: Vec<u32>,
length: usize
}
fn main() {
let vector = vec![1,2,3];
let length = vector.len();
MyStruct { vector, length };
} What if there is already a variable |
No, sorry, I did not mean to imply that. But I see you're point. It sounds like 2 may be the best option. |
…uirement configurable (#13737) Fixes #11846. This PR has three commits: - The first commit adds an `initializer-suggestions` configuration to control suggestion applicability when initializers are present. The following are the options: - "none": do not suggest - "maybe-incorrect": suggest, but do not apply suggestions with `--fix` - "machine-applicable": suggest and apply suggestions with `--fix` - The second commit fixes suggestions to handle field attributes (problem [noticed by @samueltardieu](#13737 (comment))). - The third commit adds `initializer-suggestions = "machine-applicable"` to Clippy's `clippy.toml` and applies the suggestions. (Nothing seems to break.) --- changelog: make `inconsistent_struct_constructor` "all fields are shorthand" requirement configurable
The lint's documentation states (emphasis added):
What is the reason for requiring that all fields be shorthand? On the face of it, I could see value in additionally flagging fields with initializers.
The text was updated successfully, but these errors were encountered: