Skip to content
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

detect redundant nested match #13798

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lapla-cogito
Copy link
Contributor

@lapla-cogito lapla-cogito commented Dec 9, 2024

fix #13773

match statements that refer to the same binding inside and outside can be combined.

changelog: [collapsible_match]: detect redundant nested match

@rustbot
Copy link
Collaborator

rustbot commented Dec 9, 2024

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @y21 (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 (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Dec 9, 2024
Copy link
Contributor Author

@lapla-cogito lapla-cogito left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

notes for reviewers

@@ -108,6 +108,28 @@ fn check_arm<'tcx>(
"the outer pattern can be modified to include the inner pattern",
);
});
} else if let Some(inner) = IfLetOrMatch::parse(cx, inner_expr)
Copy link
Contributor Author

@lapla-cogito lapla-cogito Dec 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The conditional branch added this time could have been written by nesting ifs together with the conditional branch above, but I have not done so for the sake of readability (the former condition is long).

&& let Some(inner_binding_id) = path_to_local(peel_ref_operators(cx, inner_scrutinee))
&& let Some(outer_binding_id) = path_to_local(peel_ref_operators(cx, outer_then_body))
// check if the inner match can be collapsed into the outer match
&& inner_binding_id == outer_binding_id
Copy link
Contributor Author

@lapla-cogito lapla-cogito Dec 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checks if the outer match and inner match refer to the same binding

&& let IfLetOrMatch::Match(inner_scrutinee, inner_arms, ..) = inner
&& outer_pat.span.eq_ctxt(inner_scrutinee.span)
&& let Some(inner_binding_id) = path_to_local(peel_ref_operators(cx, inner_scrutinee))
&& let Some(outer_binding_id) = path_to_local(peel_ref_operators(cx, outer_then_body))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this pattern here unreachable, considering that outer_then_body will be the match expression and never a path to a local variable? The test that was added already gets a warning today and the linked issue doesn't have a warning with this change, so it would be nice to see a test case for what this changes/one that didn't warn before

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better if this lint rewrote the nested match into a simply simpler match... I will update this explanation with code examples later.

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.

needless nested match
3 participants