-
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
detect redundant nested match #13798
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 @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 (
|
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.
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) |
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.
The conditional branch added this time could have been written by nesting if
s 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 |
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.
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)) |
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.
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
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 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.
fix #13773
match
statements that refer to the same binding inside and outside can be combined.changelog: [
collapsible_match
]: detect redundant nested match