-
Notifications
You must be signed in to change notification settings - Fork 1.7k
add new lint: rest_when_destructuring_struct
#15000
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
base: master
Are you sure you want to change the base?
Conversation
r? @Alexendoo rustbot has assigned @Alexendoo. Use |
1504461
to
4cdbec3
Compare
pat.span, | ||
"struct destructuring with rest (..)", | ||
None, | ||
"consider explicitly ignoring remaining fields with wildcard patterns (x: _)", |
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 say we should add a suggestion. It'll probably need to be converted to a late pass if you want to.
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.
There is seemingly no way to get the span of ..
in a late pass so I have to play a bit around with this to make it work I think.
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 that's unfortunate. It's probably adequate enough to shrink the lo
portion of the pattern's span to the end of the last PatField
. Otherwise, maybe just replace the entire pattern? It might look ok in suggestions, but it often can be finicky...
I suggest we open the FCP now and get ideas on this. It's also ok to leave this as an early lint, so long as procedural macros are 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.
I made a bit of a hack to make something that seems to work.
|
||
impl EarlyLintPass for RestWhenDestructuringStruct { | ||
fn check_pat(&mut self, cx: &EarlyContext<'_>, pat: &Pat) { | ||
if let PatKind::Struct(_, _, _, PatFieldsRest::Rest) = pat.kind { |
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 should check if it's from macro expansion and also call is_from_proc_macro
, the latter requires a late pass. I see this coming up a lot in proc macro code.
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 am not sure on what to call this on as WithSearchPat
is not implemented on patterns as far as I can tell.
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.
Is it ok to do it on the containing expression? How would that work?
WithSearchPat
is pretty easy to implement if you feel like it's worth doing so (I've needed this many times). Maybe there's a reason we shouldn't, but I dunno.
We can also leave a fixme, but this is quite common in proc macros so I think it's worthwhile doing it right.
Add a new lint that warns when using a rest pattern when destructuring a struct. This lint was requested in this comment #10666 (comment) by @xxchan.
changelog: [
rest_when_destructuring_struct
] new lint