Skip to content

Commit b8e569e

Browse files
authored
chore: use multipart_suggestions for match_same_arms (#13803)
This addresses #13099 for the match_same_arms lint. changelog: [match_same_arms]: Updated match_same_arms to use multipart_suggestions where appropriate
2 parents f1f1165 + 7d1e0da commit b8e569e

File tree

5 files changed

+346
-139
lines changed

5 files changed

+346
-139
lines changed

clippy_lints/src/matches/match_same_arms.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>]) {
7474
// check if using the same bindings as before
7575
HirIdMapEntry::Occupied(entry) => return *entry.get() == b_id,
7676
}
77-
// the names technically don't have to match; this makes the lint more conservative
78-
&& cx.tcx.hir().name(a_id) == cx.tcx.hir().name(b_id)
77+
// the names technically don't have to match; this makes the lint more conservative
78+
&& cx.tcx.hir().name(a_id) == cx.tcx.hir().name(b_id)
7979
&& cx.typeck_results().expr_ty(a) == cx.typeck_results().expr_ty(b)
8080
&& pat_contains_local(lhs.pat, a_id)
8181
&& pat_contains_local(rhs.pat, b_id)
@@ -149,16 +149,12 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>]) {
149149
let move_pat_snip = snippet_with_applicability(cx, move_arm.pat.span, "<pat2>", &mut appl);
150150
let keep_pat_snip = snippet_with_applicability(cx, keep_arm.pat.span, "<pat1>", &mut appl);
151151

152-
diag.span_suggestion(
153-
keep_arm.pat.span,
154-
"or try merging the arm patterns",
155-
format!("{keep_pat_snip} | {move_pat_snip}"),
156-
appl,
157-
)
158-
.span_suggestion(
159-
adjusted_arm_span(cx, move_arm.span),
160-
"and remove this obsolete arm",
161-
"",
152+
diag.multipart_suggestion(
153+
"or try merging the arm patterns and removing the obsolete arm",
154+
vec![
155+
(keep_arm.pat.span, format!("{keep_pat_snip} | {move_pat_snip}")),
156+
(adjusted_arm_span(cx, move_arm.span), String::new()),
157+
],
162158
appl,
163159
)
164160
.help("try changing either arm body");

tests/ui/match_same_arms.stderr

+23-40
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,10 @@ LL | (1, .., 3) => 42,
2020
| ^^^^^^^^^^^^^^^^
2121
|
2222
= help: try changing either arm body
23-
help: or try merging the arm patterns
23+
help: or try merging the arm patterns and removing the obsolete arm
2424
|
25-
LL | (1, .., 3) | (.., 3) => 42,
26-
| ~~~~~~~~~~~~~~~~~~~~
27-
help: and remove this obsolete arm
28-
|
29-
LL - (.., 3) => 42,
25+
LL ~ (1, .., 3) | (.., 3) => 42,
26+
LL ~ _ => 0,
3027
|
3128

3229
error: this match arm has an identical body to another arm
@@ -36,13 +33,11 @@ LL | 51 => 1,
3633
| ^^^^^^^
3734
|
3835
= help: try changing either arm body
39-
help: or try merging the arm patterns
40-
|
41-
LL | 51 | 42 => 1,
42-
| ~~~~~~~
43-
help: and remove this obsolete arm
36+
help: or try merging the arm patterns and removing the obsolete arm
4437
|
4538
LL - 42 => 1,
39+
LL - 51 => 1,
40+
LL + 51 | 42 => 1,
4641
|
4742

4843
error: this match arm has an identical body to another arm
@@ -52,13 +47,10 @@ LL | 41 => 2,
5247
| ^^^^^^^
5348
|
5449
= help: try changing either arm body
55-
help: or try merging the arm patterns
50+
help: or try merging the arm patterns and removing the obsolete arm
5651
|
57-
LL | 41 | 52 => 2,
58-
| ~~~~~~~
59-
help: and remove this obsolete arm
60-
|
61-
LL - 52 => 2,
52+
LL ~ 41 | 52 => 2,
53+
LL ~ _ => 0,
6254
|
6355

6456
error: this match arm has an identical body to another arm
@@ -68,13 +60,11 @@ LL | 2 => 2,
6860
| ^^^^^^
6961
|
7062
= help: try changing either arm body
71-
help: or try merging the arm patterns
72-
|
73-
LL | 2 | 1 => 2,
74-
| ~~~~~
75-
help: and remove this obsolete arm
63+
help: or try merging the arm patterns and removing the obsolete arm
7664
|
7765
LL - 1 => 2,
66+
LL - 2 => 2,
67+
LL + 2 | 1 => 2,
7868
|
7969

8070
error: this match arm has an identical body to another arm
@@ -84,13 +74,11 @@ LL | 3 => 2,
8474
| ^^^^^^
8575
|
8676
= help: try changing either arm body
87-
help: or try merging the arm patterns
77+
help: or try merging the arm patterns and removing the obsolete arm
8878
|
89-
LL | 3 | 1 => 2,
90-
| ~~~~~
91-
help: and remove this obsolete arm
92-
|
93-
LL - 1 => 2,
79+
LL ~ 2 => 2,
80+
LL |
81+
LL ~ 3 | 1 => 2,
9482
|
9583

9684
error: this match arm has an identical body to another arm
@@ -100,14 +88,11 @@ LL | 2 => 2,
10088
| ^^^^^^
10189
|
10290
= help: try changing either arm body
103-
help: or try merging the arm patterns
91+
help: or try merging the arm patterns and removing the obsolete arm
10492
|
105-
LL | 2 | 3 => 2,
106-
| ~~~~~
107-
help: and remove this obsolete arm
108-
|
109-
LL - 3 => 2,
110-
LL +
93+
LL ~ 2 | 3 => 2,
94+
LL |
95+
LL ~
11196
|
11297

11398
error: this match arm has an identical body to another arm
@@ -117,13 +102,11 @@ LL | CommandInfo::External { name, .. } => name.to_string(),
117102
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
118103
|
119104
= help: try changing either arm body
120-
help: or try merging the arm patterns
121-
|
122-
LL | CommandInfo::External { name, .. } | CommandInfo::BuiltIn { name, .. } => name.to_string(),
123-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
124-
help: and remove this obsolete arm
105+
help: or try merging the arm patterns and removing the obsolete arm
125106
|
126107
LL - CommandInfo::BuiltIn { name, .. } => name.to_string(),
108+
LL - CommandInfo::External { name, .. } => name.to_string(),
109+
LL + CommandInfo::External { name, .. } | CommandInfo::BuiltIn { name, .. } => name.to_string(),
127110
|
128111

129112
error: aborting due to 8 previous errors

0 commit comments

Comments
 (0)