Skip to content

Commit a863780

Browse files
committed
Auto merge of #12794 - J-ZhengLi:issue9251, r=blyxyas
improve [`match_same_arms`] messages, enable rustfix test closes: #9251 don't worry about the commit size, most of them are generated --- changelog: improve [`match_same_arms`] lint messages
2 parents d6991ab + dc5b99b commit a863780

8 files changed

+533
-209
lines changed

clippy_lints/src/matches/match_same_arms.rs

+26-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_hir_and_then;
2-
use clippy_utils::source::snippet;
2+
use clippy_utils::source::snippet_with_applicability;
33
use clippy_utils::{is_lint_allowed, path_to_local, search_same, SpanlessEq, SpanlessHash};
44
use core::cmp::Ordering;
55
use core::{iter, slice};
@@ -9,9 +9,9 @@ use rustc_errors::Applicability;
99
use rustc_hir::def_id::DefId;
1010
use rustc_hir::{Arm, Expr, ExprKind, HirId, HirIdMap, HirIdMapEntry, HirIdSet, Pat, PatKind, RangeEnd};
1111
use rustc_lint::builtin::NON_EXHAUSTIVE_OMITTED_PATTERNS;
12-
use rustc_lint::LateContext;
12+
use rustc_lint::{LateContext, LintContext};
1313
use rustc_middle::ty;
14-
use rustc_span::{ErrorGuaranteed, Symbol};
14+
use rustc_span::{ErrorGuaranteed, Span, Symbol};
1515

1616
use super::MATCH_SAME_ARMS;
1717

@@ -110,20 +110,22 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>]) {
110110
&& check_same_body()
111111
};
112112

113+
let mut appl = Applicability::MaybeIncorrect;
113114
let indexed_arms: Vec<(usize, &Arm<'_>)> = arms.iter().enumerate().collect();
114115
for (&(i, arm1), &(j, arm2)) in search_same(&indexed_arms, hash, eq) {
115116
if matches!(arm2.pat.kind, PatKind::Wild) {
116117
if !cx.tcx.features().non_exhaustive_omitted_patterns_lint
117118
|| is_lint_allowed(cx, NON_EXHAUSTIVE_OMITTED_PATTERNS, arm2.hir_id)
118119
{
120+
let arm_span = adjusted_arm_span(cx, arm1.span);
119121
span_lint_hir_and_then(
120122
cx,
121123
MATCH_SAME_ARMS,
122124
arm1.hir_id,
123-
arm1.span,
125+
arm_span,
124126
"this match arm has an identical body to the `_` wildcard arm",
125127
|diag| {
126-
diag.span_suggestion(arm1.span, "try removing the arm", "", Applicability::MaybeIncorrect)
128+
diag.span_suggestion(arm_span, "try removing the arm", "", appl)
127129
.help("or try changing either arm body")
128130
.span_note(arm2.span, "`_` wildcard arm here");
129131
},
@@ -144,23 +146,36 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>]) {
144146
keep_arm.span,
145147
"this match arm has an identical body to another arm",
146148
|diag| {
147-
let move_pat_snip = snippet(cx, move_arm.pat.span, "<pat2>");
148-
let keep_pat_snip = snippet(cx, keep_arm.pat.span, "<pat1>");
149+
let move_pat_snip = snippet_with_applicability(cx, move_arm.pat.span, "<pat2>", &mut appl);
150+
let keep_pat_snip = snippet_with_applicability(cx, keep_arm.pat.span, "<pat1>", &mut appl);
149151

150152
diag.span_suggestion(
151153
keep_arm.pat.span,
152-
"try merging the arm patterns",
154+
"or try merging the arm patterns",
153155
format!("{keep_pat_snip} | {move_pat_snip}"),
154-
Applicability::MaybeIncorrect,
156+
appl,
155157
)
156-
.help("or try changing either arm body")
157-
.span_note(move_arm.span, "other arm here");
158+
.span_suggestion(
159+
adjusted_arm_span(cx, move_arm.span),
160+
"and remove this obsolete arm",
161+
"",
162+
appl,
163+
)
164+
.help("try changing either arm body");
158165
},
159166
);
160167
}
161168
}
162169
}
163170

171+
/// Extend arm's span to include the comma and whitespaces after it.
172+
fn adjusted_arm_span(cx: &LateContext<'_>, span: Span) -> Span {
173+
let source_map = cx.sess().source_map();
174+
source_map
175+
.span_extend_while(span, |c| c == ',' || c.is_ascii_whitespace())
176+
.unwrap_or(span)
177+
}
178+
164179
#[derive(Clone, Copy)]
165180
enum NormalizedPat<'a> {
166181
Wild,

tests/ui/match_same_arms.stderr

+65-57
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: this match arm has an identical body to the `_` wildcard arm
22
--> tests/ui/match_same_arms.rs:12:9
33
|
44
LL | Abc::A => 0,
5-
| ^^^^^^^^^^^ help: try removing the arm
5+
| ^^^^^^^^^^^^^ help: try removing the arm
66
|
77
= help: or try changing either arm body
88
note: `_` wildcard arm here
@@ -17,106 +17,114 @@ error: this match arm has an identical body to another arm
1717
--> tests/ui/match_same_arms.rs:18:9
1818
|
1919
LL | (1, .., 3) => 42,
20-
| ----------^^^^^^
21-
| |
22-
| help: try merging the arm patterns: `(1, .., 3) | (.., 3)`
20+
| ^^^^^^^^^^^^^^^^
2321
|
24-
= help: or try changing either arm body
25-
note: other arm here
26-
--> tests/ui/match_same_arms.rs:19:9
22+
= help: try changing either arm body
23+
help: or try merging the arm patterns
24+
|
25+
LL | (1, .., 3) | (.., 3) => 42,
26+
| ~~~~~~~~~~~~~~~~~~~~
27+
help: and remove this obsolete arm
28+
|
29+
LL - (.., 3) => 42,
2730
|
28-
LL | (.., 3) => 42,
29-
| ^^^^^^^^^^^^^
3031

3132
error: this match arm has an identical body to another arm
3233
--> tests/ui/match_same_arms.rs:25:9
3334
|
3435
LL | 51 => 1,
35-
| --^^^^^
36-
| |
37-
| help: try merging the arm patterns: `51 | 42`
36+
| ^^^^^^^
3837
|
39-
= help: or try changing either arm body
40-
note: other arm here
41-
--> tests/ui/match_same_arms.rs:24:9
38+
= 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
44+
|
45+
LL - 42 => 1,
4246
|
43-
LL | 42 => 1,
44-
| ^^^^^^^
4547

4648
error: this match arm has an identical body to another arm
4749
--> tests/ui/match_same_arms.rs:26:9
4850
|
4951
LL | 41 => 2,
50-
| --^^^^^
51-
| |
52-
| help: try merging the arm patterns: `41 | 52`
52+
| ^^^^^^^
5353
|
54-
= help: or try changing either arm body
55-
note: other arm here
56-
--> tests/ui/match_same_arms.rs:27:9
54+
= help: try changing either arm body
55+
help: or try merging the arm patterns
56+
|
57+
LL | 41 | 52 => 2,
58+
| ~~~~~~~
59+
help: and remove this obsolete arm
60+
|
61+
LL - 52 => 2,
5762
|
58-
LL | 52 => 2,
59-
| ^^^^^^^
6063

6164
error: this match arm has an identical body to another arm
6265
--> tests/ui/match_same_arms.rs:33:9
6366
|
6467
LL | 2 => 2,
65-
| -^^^^^
66-
| |
67-
| help: try merging the arm patterns: `2 | 1`
68+
| ^^^^^^
6869
|
69-
= help: or try changing either arm body
70-
note: other arm here
71-
--> tests/ui/match_same_arms.rs:32:9
70+
= 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
76+
|
77+
LL - 1 => 2,
7278
|
73-
LL | 1 => 2,
74-
| ^^^^^^
7579

7680
error: this match arm has an identical body to another arm
7781
--> tests/ui/match_same_arms.rs:35:9
7882
|
7983
LL | 3 => 2,
80-
| -^^^^^
81-
| |
82-
| help: try merging the arm patterns: `3 | 1`
84+
| ^^^^^^
8385
|
84-
= help: or try changing either arm body
85-
note: other arm here
86-
--> tests/ui/match_same_arms.rs:32:9
86+
= help: try changing either arm body
87+
help: or try merging the arm patterns
88+
|
89+
LL | 3 | 1 => 2,
90+
| ~~~~~
91+
help: and remove this obsolete arm
92+
|
93+
LL - 1 => 2,
8794
|
88-
LL | 1 => 2,
89-
| ^^^^^^
9095

9196
error: this match arm has an identical body to another arm
9297
--> tests/ui/match_same_arms.rs:33:9
9398
|
9499
LL | 2 => 2,
95-
| -^^^^^
96-
| |
97-
| help: try merging the arm patterns: `2 | 3`
100+
| ^^^^^^
98101
|
99-
= help: or try changing either arm body
100-
note: other arm here
101-
--> tests/ui/match_same_arms.rs:35:9
102+
= help: try changing either arm body
103+
help: or try merging the arm patterns
104+
|
105+
LL | 2 | 3 => 2,
106+
| ~~~~~
107+
help: and remove this obsolete arm
108+
|
109+
LL - 3 => 2,
110+
LL +
102111
|
103-
LL | 3 => 2,
104-
| ^^^^^^
105112

106113
error: this match arm has an identical body to another arm
107114
--> tests/ui/match_same_arms.rs:52:17
108115
|
109116
LL | CommandInfo::External { name, .. } => name.to_string(),
110-
| ----------------------------------^^^^^^^^^^^^^^^^^^^^
111-
| |
112-
| help: try merging the arm patterns: `CommandInfo::External { name, .. } | CommandInfo::BuiltIn { name, .. }`
117+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
113118
|
114-
= help: or try changing either arm body
115-
note: other arm here
116-
--> tests/ui/match_same_arms.rs:51:17
119+
= 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
125+
|
126+
LL - CommandInfo::BuiltIn { name, .. } => name.to_string(),
117127
|
118-
LL | CommandInfo::BuiltIn { name, .. } => name.to_string(),
119-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
120128

121129
error: aborting due to 8 previous errors
122130

0 commit comments

Comments
 (0)