Skip to content

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Erk-
Copy link

@Erk- Erk- commented Jun 6, 2025

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

@rustbot
Copy link
Collaborator

rustbot commented Jun 6, 2025

r? @Alexendoo

rustbot has assigned @Alexendoo.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jun 6, 2025
@Erk- Erk- force-pushed the lint/rest_when_destructuring_struct branch from 1504461 to 4cdbec3 Compare June 6, 2025 12:47
pat.span,
"struct destructuring with rest (..)",
None,
"consider explicitly ignoring remaining fields with wildcard patterns (x: _)",
Copy link
Member

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.

Copy link
Author

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.

Copy link
Member

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.

Copy link
Author

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 {
Copy link
Member

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.

Copy link
Author

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.

Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants