-
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
Add new lint unneeded_struct_pattern
#13465
base: master
Are you sure you want to change the base?
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Centri3 (or someone else) some time within the next two weeks. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
d1a94c7
to
4733775
Compare
"struct pattern is not needed for a unit variant", | ||
"remove the struct pattern", | ||
String::new(), | ||
Applicability::Unspecified, |
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.
Suppose it can be MachineApplicable
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.
Done. 👍
3b45390
to
36b1025
Compare
☔ The latest upstream changes (presumably #13334) made this pull request unmergeable. Please resolve the merge conflicts. |
49f5231
to
d5ae91d
Compare
Rebased. r? @alex-semenyuk |
Failed to set assignee to
|
☔ The latest upstream changes (presumably #13376) made this pull request unmergeable. Please resolve the merge conflicts. |
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 have a couple cases for you to consider, both whether we should lint them (imo yes) and whether it works on them.
Patterns in if let
& let ... else
Patterns in function parameters: fn a(A::Empty {}: A) {}
Patterns in for
: A::Empty {}
Fundamentally these are all likely the same, but they shouldn't be forgotten :) There may be some weird edge cases with them. (Though I don't think your changes will ever do so)
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.
Done, added test cases for them.
let enum_did = cx.tcx.parent(did); | ||
let variant = cx.tcx.adt_def(enum_did).variant_with_id(did); | ||
|
||
let has_fields_brackets = !(variant.ctor.is_some() && variant.fields.is_empty()); |
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'd prefer if the negation is done later, in the if
. This roundabout way did confuse me at first (considering the name).
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.
Done. After re-reading this code I wrote 2 months later, I am also getting confused. 😅 Seems I had misnamed the variable.
/// ``` | ||
#[clippy::version = "1.83.0"] | ||
pub UNNEEDED_STRUCT_PATTERN, | ||
nursery, |
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.
Any specific issues?
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 have changed it to style
.
Hey @SpriteOvO, been almost a month and I'm pinging from triage. Has there been any progress? Is there anything you're stuck on or have any questions? |
@Centri3 Oh, sorry, I totally missed this in my inbox. I'll finish it in the next days. |
1e6f710
to
79bed8a
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.
Just two things to consider, looks fine otherwise.
tests/ui/unneeded_struct_pattern.rs
Outdated
_ => 0, | ||
}; | ||
|
||
if let Custom::HasFields { field: value } = Custom::Init {} // Should be ignored |
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.
This is some very misleading formatting 😅 maybe add a statement within the block to make it clear these brackets are a block and not part of the value?
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.
Makes sense, I have added a noop();
call in the block.
tests/ui/unneeded_struct_pattern.rs
Outdated
let Custom::HasBracketsNoFields {} = Custom::Init else { | ||
panic!() | ||
}; | ||
// Should be ignored |
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: Note that these could be replaced with error annotations but both are used interchangeably in clippy atm
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.
Done.
79bed8a
to
3760e75
Compare
3760e75
to
52abb8f
Compare
Closes #13400.
changelog: [
unneeded_struct_pattern
]: Add new lint