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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions clippy_lints/src/matches/collapsible_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 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.

// 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

&& outer_guard.is_none_or(|e| !is_local_used(cx, e, inner_binding_id))
&& !inner_arms
.iter()
.any(|arm| is_local_used(cx, arm.body, inner_binding_id))
{
let msg = "this match can be collapsed into the outer match";
span_lint_and_then(cx, COLLAPSIBLE_MATCH, inner_expr.span, msg, |diag| {
let mut help_span = MultiSpan::from_spans(vec![outer_pat.span, inner_scrutinee.span]);
help_span.push_span_label(outer_pat.span, "outer pattern");
help_span.push_span_label(inner_scrutinee.span, "inner pattern");
diag.span_help(
help_span,
"the outer pattern can be modified to include the inner pattern",
);
});
}
}

Expand Down
12 changes: 12 additions & 0 deletions tests/ui/collapsible_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ fn lint_cases(opt_opt: Option<Option<u32>>, res_opt: Result<Option<u32>, String>
},
None => return,
}

match res_opt {
Ok(val) => match res_opt {
Ok(val) => match val {
//~^ ERROR: this `match` can be collapsed into the outer `match`
Some(n) => foo(n),
_ => return,
},
_ => return,
},
_ => return,
}
}

fn negative_cases(res_opt: Result<Option<u32>, String>, res_res: Result<Result<u32, String>, String>) {
Expand Down
34 changes: 27 additions & 7 deletions tests/ui/collapsible_match.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,27 @@ LL | Some(n) => foo(n),
| ^^^^^^^ with this pattern

error: this `match` can be collapsed into the outer `match`
--> tests/ui/collapsible_match.rs:252:22
--> tests/ui/collapsible_match.rs:118:24
|
LL | Ok(val) => match val {
| ________________________^
LL | |
LL | | Some(n) => foo(n),
LL | | _ => return,
LL | | },
| |_____________^
|
help: the outer pattern can be modified to include the inner pattern
--> tests/ui/collapsible_match.rs:118:16
|
LL | Ok(val) => match val {
| ^^^ replace this binding
LL |
LL | Some(n) => foo(n),
| ^^^^^^^ with this pattern

error: this `match` can be collapsed into the outer `match`
--> tests/ui/collapsible_match.rs:264:22
|
LL | Some(val) => match val {
| ______________________^
Expand All @@ -201,15 +221,15 @@ LL | | },
| |_________^
|
help: the outer pattern can be modified to include the inner pattern
--> tests/ui/collapsible_match.rs:252:14
--> tests/ui/collapsible_match.rs:264:14
|
LL | Some(val) => match val {
| ^^^ replace this binding
LL | E::A(val) | E::B(val) => foo(val),
| ^^^^^^^^^^^^^^^^^^^^^ with this pattern

error: this `if let` can be collapsed into the outer `if let`
--> tests/ui/collapsible_match.rs:283:9
--> tests/ui/collapsible_match.rs:295:9
|
LL | / if let Some(u) = a {
LL | |
Expand All @@ -218,15 +238,15 @@ LL | | }
| |_________^
|
help: the outer pattern can be modified to include the inner pattern
--> tests/ui/collapsible_match.rs:282:27
--> tests/ui/collapsible_match.rs:294:27
|
LL | if let Issue9647::A { a, .. } = x {
| ^ replace this binding
LL | if let Some(u) = a {
| ^^^^^^^ with this pattern, prefixed by `a`:

error: this `if let` can be collapsed into the outer `if let`
--> tests/ui/collapsible_match.rs:292:9
--> tests/ui/collapsible_match.rs:304:9
|
LL | / if let Some(u) = a {
LL | |
Expand All @@ -235,12 +255,12 @@ LL | | }
| |_________^
|
help: the outer pattern can be modified to include the inner pattern
--> tests/ui/collapsible_match.rs:291:35
--> tests/ui/collapsible_match.rs:303:35
|
LL | if let Issue9647::A { a: Some(a), .. } = x {
| ^ replace this binding
LL | if let Some(u) = a {
| ^^^^^^^ with this pattern

error: aborting due to 13 previous errors
error: aborting due to 14 previous errors

Loading