Skip to content

Commit

Permalink
chore: use multipart_suggestions for match_same_arms (#13803)
Browse files Browse the repository at this point in the history
This addresses #13099 for
the match_same_arms lint.

changelog: [match_same_arms]: Updated match_same_arms to use
multipart_suggestions where appropriate
  • Loading branch information
blyxyas authored Dec 15, 2024
2 parents f1f1165 + 7d1e0da commit b8e569e
Show file tree
Hide file tree
Showing 5 changed files with 346 additions and 139 deletions.
20 changes: 8 additions & 12 deletions clippy_lints/src/matches/match_same_arms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>]) {
// check if using the same bindings as before
HirIdMapEntry::Occupied(entry) => return *entry.get() == b_id,
}
// the names technically don't have to match; this makes the lint more conservative
&& cx.tcx.hir().name(a_id) == cx.tcx.hir().name(b_id)
// the names technically don't have to match; this makes the lint more conservative
&& cx.tcx.hir().name(a_id) == cx.tcx.hir().name(b_id)
&& cx.typeck_results().expr_ty(a) == cx.typeck_results().expr_ty(b)
&& pat_contains_local(lhs.pat, a_id)
&& pat_contains_local(rhs.pat, b_id)
Expand Down Expand Up @@ -149,16 +149,12 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>]) {
let move_pat_snip = snippet_with_applicability(cx, move_arm.pat.span, "<pat2>", &mut appl);
let keep_pat_snip = snippet_with_applicability(cx, keep_arm.pat.span, "<pat1>", &mut appl);

diag.span_suggestion(
keep_arm.pat.span,
"or try merging the arm patterns",
format!("{keep_pat_snip} | {move_pat_snip}"),
appl,
)
.span_suggestion(
adjusted_arm_span(cx, move_arm.span),
"and remove this obsolete arm",
"",
diag.multipart_suggestion(
"or try merging the arm patterns and removing the obsolete arm",
vec![
(keep_arm.pat.span, format!("{keep_pat_snip} | {move_pat_snip}")),
(adjusted_arm_span(cx, move_arm.span), String::new()),
],
appl,
)
.help("try changing either arm body");
Expand Down
63 changes: 23 additions & 40 deletions tests/ui/match_same_arms.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,10 @@ LL | (1, .., 3) => 42,
| ^^^^^^^^^^^^^^^^
|
= help: try changing either arm body
help: or try merging the arm patterns
help: or try merging the arm patterns and removing the obsolete arm
|
LL | (1, .., 3) | (.., 3) => 42,
| ~~~~~~~~~~~~~~~~~~~~
help: and remove this obsolete arm
|
LL - (.., 3) => 42,
LL ~ (1, .., 3) | (.., 3) => 42,
LL ~ _ => 0,
|

error: this match arm has an identical body to another arm
Expand All @@ -36,13 +33,11 @@ LL | 51 => 1,
| ^^^^^^^
|
= help: try changing either arm body
help: or try merging the arm patterns
|
LL | 51 | 42 => 1,
| ~~~~~~~
help: and remove this obsolete arm
help: or try merging the arm patterns and removing the obsolete arm
|
LL - 42 => 1,
LL - 51 => 1,
LL + 51 | 42 => 1,
|

error: this match arm has an identical body to another arm
Expand All @@ -52,13 +47,10 @@ LL | 41 => 2,
| ^^^^^^^
|
= help: try changing either arm body
help: or try merging the arm patterns
help: or try merging the arm patterns and removing the obsolete arm
|
LL | 41 | 52 => 2,
| ~~~~~~~
help: and remove this obsolete arm
|
LL - 52 => 2,
LL ~ 41 | 52 => 2,
LL ~ _ => 0,
|

error: this match arm has an identical body to another arm
Expand All @@ -68,13 +60,11 @@ LL | 2 => 2,
| ^^^^^^
|
= help: try changing either arm body
help: or try merging the arm patterns
|
LL | 2 | 1 => 2,
| ~~~~~
help: and remove this obsolete arm
help: or try merging the arm patterns and removing the obsolete arm
|
LL - 1 => 2,
LL - 2 => 2,
LL + 2 | 1 => 2,
|

error: this match arm has an identical body to another arm
Expand All @@ -84,13 +74,11 @@ LL | 3 => 2,
| ^^^^^^
|
= help: try changing either arm body
help: or try merging the arm patterns
help: or try merging the arm patterns and removing the obsolete arm
|
LL | 3 | 1 => 2,
| ~~~~~
help: and remove this obsolete arm
|
LL - 1 => 2,
LL ~ 2 => 2,
LL |
LL ~ 3 | 1 => 2,
|

error: this match arm has an identical body to another arm
Expand All @@ -100,14 +88,11 @@ LL | 2 => 2,
| ^^^^^^
|
= help: try changing either arm body
help: or try merging the arm patterns
help: or try merging the arm patterns and removing the obsolete arm
|
LL | 2 | 3 => 2,
| ~~~~~
help: and remove this obsolete arm
|
LL - 3 => 2,
LL +
LL ~ 2 | 3 => 2,
LL |
LL ~
|

error: this match arm has an identical body to another arm
Expand All @@ -117,13 +102,11 @@ LL | CommandInfo::External { name, .. } => name.to_string(),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: try changing either arm body
help: or try merging the arm patterns
|
LL | CommandInfo::External { name, .. } | CommandInfo::BuiltIn { name, .. } => name.to_string(),
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
help: and remove this obsolete arm
help: or try merging the arm patterns and removing the obsolete arm
|
LL - CommandInfo::BuiltIn { name, .. } => name.to_string(),
LL - CommandInfo::External { name, .. } => name.to_string(),
LL + CommandInfo::External { name, .. } | CommandInfo::BuiltIn { name, .. } => name.to_string(),
|

error: aborting due to 8 previous errors
Expand Down
Loading

0 comments on commit b8e569e

Please sign in to comment.