Skip to content

Commit a863780

Browse files
committedMay 14, 2024·
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

Lines changed: 26 additions & 11 deletions
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

Lines changed: 65 additions & 57 deletions
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

‎tests/ui/match_same_arms2.fixed

Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
#![warn(clippy::match_same_arms)]
2+
#![allow(
3+
clippy::disallowed_names,
4+
clippy::diverging_sub_expression,
5+
clippy::uninlined_format_args,
6+
clippy::match_single_binding,
7+
clippy::match_like_matches_macro
8+
)]
9+
fn bar<T>(_: T) {}
10+
fn foo() -> bool {
11+
unimplemented!()
12+
}
13+
14+
fn match_same_arms() {
15+
let _ = match 42 {
16+
_ => {
17+
foo();
18+
let mut a = 42 + [23].len() as i32;
19+
if true {
20+
a += 7;
21+
}
22+
a = -31 - a;
23+
a
24+
},
25+
};
26+
//~^^^^^^^^^^^^^^^^^^^ ERROR: this match arm has an identical body to the `_` wildcard arm
27+
28+
let _ = match 42 {
29+
51 | 42 => foo(), //~ ERROR: this match arm has an identical body to another arm
30+
_ => true,
31+
};
32+
33+
let _ = match Some(42) {
34+
None | Some(_) => 24, //~ ERROR: this match arm has an identical body to another arm
35+
};
36+
37+
let _ = match Some(42) {
38+
Some(foo) => 24,
39+
None => 24,
40+
};
41+
42+
let _ = match Some(42) {
43+
Some(42) => 24,
44+
Some(a) => 24, // bindings are different
45+
None => 0,
46+
};
47+
48+
let _ = match Some(42) {
49+
Some(a) if a > 0 => 24,
50+
Some(a) => 24, // one arm has a guard
51+
None => 0,
52+
};
53+
54+
match (Some(42), Some(42)) {
55+
(None, Some(a)) | (Some(a), None) => bar(a), //~ ERROR: this match arm has an identical body to another arm
56+
_ => (),
57+
}
58+
59+
// No warning because guards are different
60+
let _ = match Some(42) {
61+
Some(a) if a == 42 => a,
62+
Some(a) if a == 24 => a,
63+
Some(_) => 24,
64+
None => 0,
65+
};
66+
67+
let _ = match (Some(42), Some(42)) {
68+
(None, Some(a)) | (Some(a), None) if a == 42 => a, //~ ERROR: this match arm has an identical body to another arm
69+
_ => 0,
70+
};
71+
72+
match (Some(42), Some(42)) {
73+
(Some(a), ..) | (.., Some(a)) => bar(a), //~ ERROR: this match arm has an identical body to another arm
74+
_ => (),
75+
}
76+
77+
let _ = match Some(()) {
78+
Some(()) => 0.0,
79+
None => -0.0,
80+
};
81+
82+
match (Some(42), Some("")) {
83+
(Some(a), None) => bar(a),
84+
(None, Some(a)) => bar(a), // bindings have different types
85+
_ => (),
86+
}
87+
88+
let x: Result<i32, &str> = Ok(3);
89+
90+
// No warning because of the guard.
91+
match x {
92+
Ok(x) if x * x == 64 => println!("ok"),
93+
Ok(_) => println!("ok"),
94+
Err(_) => println!("err"),
95+
}
96+
97+
// This used to be a false positive; see issue #1996.
98+
match x {
99+
Ok(3) => println!("ok"),
100+
Ok(x) if x * x == 64 => println!("ok 64"),
101+
Ok(_) => println!("ok"),
102+
Err(_) => println!("err"),
103+
}
104+
105+
match (x, Some(1i32)) {
106+
(Ok(x), Some(_)) | (Ok(_), Some(x)) => println!("ok {}", x), //~ ERROR: this match arm has an identical body to another arm
107+
_ => println!("err"),
108+
}
109+
110+
// No warning; different types for `x`.
111+
match (x, Some(1.0f64)) {
112+
(Ok(x), Some(_)) => println!("ok {}", x),
113+
(Ok(_), Some(x)) => println!("ok {}", x),
114+
_ => println!("err"),
115+
}
116+
117+
// False negative #2251.
118+
match x {
119+
Ok(_tmp) => println!("ok"),
120+
Ok(_) | Ok(3) => println!("ok"), //~ ERROR: this match arm has an identical body to another arm
121+
Err(_) => {
122+
unreachable!();
123+
},
124+
}
125+
126+
// False positive #1390
127+
macro_rules! empty {
128+
($e:expr) => {};
129+
}
130+
match 0 {
131+
0 => {
132+
empty!(0);
133+
},
134+
1 => {
135+
empty!(1);
136+
},
137+
x => {
138+
empty!(x);
139+
},
140+
};
141+
142+
// still lint if the tokens are the same
143+
match 0 {
144+
1 | 0 => {
145+
empty!(0);
146+
},
147+
x => {
148+
empty!(x);
149+
},
150+
}
151+
//~^^^^^^^ ERROR: this match arm has an identical body to another arm
152+
153+
match_expr_like_matches_macro_priority();
154+
}
155+
156+
fn match_expr_like_matches_macro_priority() {
157+
enum E {
158+
A,
159+
B,
160+
C,
161+
}
162+
let x = E::A;
163+
let _ans = match x {
164+
E::A => false,
165+
E::B => false,
166+
_ => true,
167+
};
168+
}
169+
170+
fn main() {
171+
let _ = match Some(0) {
172+
Some(0) => 0,
173+
Some(1) => 1,
174+
#[cfg(feature = "foo")]
175+
Some(2) => 2,
176+
_ => 1,
177+
};
178+
179+
enum Foo {
180+
X(u32),
181+
Y(u32),
182+
Z(u32),
183+
}
184+
185+
// Don't lint. `Foo::X(0)` and `Foo::Z(_)` overlap with the arm in between.
186+
let _ = match Foo::X(0) {
187+
Foo::X(0) => 1,
188+
Foo::X(_) | Foo::Y(_) | Foo::Z(0) => 2,
189+
Foo::Z(_) => 1,
190+
_ => 0,
191+
};
192+
193+
// Suggest moving `Foo::Z(_)` up.
194+
let _ = match Foo::X(0) {
195+
Foo::X(0) | Foo::Z(_) => 1, //~ ERROR: this match arm has an identical body to another arm
196+
Foo::X(_) | Foo::Y(_) => 2,
197+
_ => 0,
198+
};
199+
200+
// Suggest moving `Foo::X(0)` down.
201+
let _ = match Foo::X(0) {
202+
Foo::Y(_) | Foo::Z(0) => 2,
203+
Foo::Z(_) | Foo::X(0) => 1, //~ ERROR: this match arm has an identical body to another arm
204+
_ => 0,
205+
};
206+
207+
// Don't lint.
208+
let _ = match 0 {
209+
-2 => 1,
210+
-5..=50 => 2,
211+
-150..=88 => 1,
212+
_ => 3,
213+
};
214+
215+
struct Bar {
216+
x: u32,
217+
y: u32,
218+
z: u32,
219+
}
220+
221+
// Lint.
222+
let _ = match None {
223+
Some(Bar { y: 10, z: 0, .. }) => 2,
224+
None => 50,
225+
Some(Bar { y: 0, x: 5, .. }) | Some(Bar { x: 0, y: 5, .. }) => 1, //~ ERROR: this match arm has an identical body to another arm
226+
_ => 200,
227+
};
228+
229+
let _ = match 0 {
230+
0 => todo!(),
231+
1 => todo!(),
232+
2 => core::convert::identity::<u32>(todo!()),
233+
3 => core::convert::identity::<u32>(todo!()),
234+
_ => 5,
235+
};
236+
237+
let _ = match 0 {
238+
1 | 0 => cfg!(not_enable),
239+
_ => false,
240+
};
241+
}

‎tests/ui/match_same_arms2.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
#![allow(
33
clippy::disallowed_names,
44
clippy::diverging_sub_expression,
5-
clippy::uninlined_format_args
5+
clippy::uninlined_format_args,
6+
clippy::match_single_binding,
7+
clippy::match_like_matches_macro
68
)]
7-
//@no-rustfix
89
fn bar<T>(_: T) {}
910
fn foo() -> bool {
1011
unimplemented!()

‎tests/ui/match_same_arms2.stderr

Lines changed: 127 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
error: this match arm has an identical body to the `_` wildcard arm
2-
--> tests/ui/match_same_arms2.rs:15:9
2+
--> tests/ui/match_same_arms2.rs:16:9
33
|
44
LL | / 42 => {
55
LL | | foo();
66
LL | | let mut a = 42 + [23].len() as i32;
77
LL | | if true {
88
... |
9-
LL | | a
109
LL | | },
11-
| |_________^ help: try removing the arm
10+
LL | | _ => {
11+
| |________^ help: try removing the arm
1212
|
1313
= help: or try changing either arm body
1414
note: `_` wildcard arm here
15-
--> tests/ui/match_same_arms2.rs:24:9
15+
--> tests/ui/match_same_arms2.rs:25:9
1616
|
1717
LL | / _ => {
1818
LL | | foo();
@@ -26,203 +26,200 @@ LL | | },
2626
= help: to override `-D warnings` add `#[allow(clippy::match_same_arms)]`
2727

2828
error: this match arm has an identical body to another arm
29-
--> tests/ui/match_same_arms2.rs:38:9
29+
--> tests/ui/match_same_arms2.rs:39:9
3030
|
3131
LL | 51 => foo(),
32-
| --^^^^^^^^^
33-
| |
34-
| help: try merging the arm patterns: `51 | 42`
32+
| ^^^^^^^^^^^
3533
|
36-
= help: or try changing either arm body
37-
note: other arm here
38-
--> tests/ui/match_same_arms2.rs:37:9
34+
= help: try changing either arm body
35+
help: or try merging the arm patterns
36+
|
37+
LL | 51 | 42 => foo(),
38+
| ~~~~~~~
39+
help: and remove this obsolete arm
40+
|
41+
LL - 42 => foo(),
3942
|
40-
LL | 42 => foo(),
41-
| ^^^^^^^^^^^
4243

4344
error: this match arm has an identical body to another arm
44-
--> tests/ui/match_same_arms2.rs:44:9
45+
--> tests/ui/match_same_arms2.rs:45:9
4546
|
4647
LL | None => 24,
47-
| ----^^^^^^
48-
| |
49-
| help: try merging the arm patterns: `None | Some(_)`
48+
| ^^^^^^^^^^
5049
|
51-
= help: or try changing either arm body
52-
note: other arm here
53-
--> tests/ui/match_same_arms2.rs:43:9
50+
= help: try changing either arm body
51+
help: or try merging the arm patterns
52+
|
53+
LL | None | Some(_) => 24,
54+
| ~~~~~~~~~~~~~~
55+
help: and remove this obsolete arm
56+
|
57+
LL - Some(_) => 24,
5458
|
55-
LL | Some(_) => 24,
56-
| ^^^^^^^^^^^^^
5759

5860
error: this match arm has an identical body to another arm
59-
--> tests/ui/match_same_arms2.rs:66:9
61+
--> tests/ui/match_same_arms2.rs:67:9
6062
|
6163
LL | (None, Some(a)) => bar(a),
62-
| ---------------^^^^^^^^^^
63-
| |
64-
| help: try merging the arm patterns: `(None, Some(a)) | (Some(a), None)`
64+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
6565
|
66-
= help: or try changing either arm body
67-
note: other arm here
68-
--> tests/ui/match_same_arms2.rs:65:9
66+
= help: try changing either arm body
67+
help: or try merging the arm patterns
68+
|
69+
LL | (None, Some(a)) | (Some(a), None) => bar(a),
70+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
71+
help: and remove this obsolete arm
72+
|
73+
LL - (Some(a), None) => bar(a),
6974
|
70-
LL | (Some(a), None) => bar(a),
71-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
7275

7376
error: this match arm has an identical body to another arm
74-
--> tests/ui/match_same_arms2.rs:80:9
77+
--> tests/ui/match_same_arms2.rs:81:9
7578
|
7679
LL | (None, Some(a)) if a == 42 => a,
77-
| ---------------^^^^^^^^^^^^^^^^
78-
| |
79-
| help: try merging the arm patterns: `(None, Some(a)) | (Some(a), None)`
80+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8081
|
81-
= help: or try changing either arm body
82-
note: other arm here
83-
--> tests/ui/match_same_arms2.rs:79:9
82+
= help: try changing either arm body
83+
help: or try merging the arm patterns
84+
|
85+
LL | (None, Some(a)) | (Some(a), None) if a == 42 => a,
86+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
87+
help: and remove this obsolete arm
88+
|
89+
LL - (Some(a), None) if a == 42 => a,
8490
|
85-
LL | (Some(a), None) if a == 42 => a,
86-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8791

8892
error: this match arm has an identical body to another arm
89-
--> tests/ui/match_same_arms2.rs:85:9
93+
--> tests/ui/match_same_arms2.rs:86:9
9094
|
9195
LL | (Some(a), ..) => bar(a),
92-
| -------------^^^^^^^^^^
93-
| |
94-
| help: try merging the arm patterns: `(Some(a), ..) | (.., Some(a))`
96+
| ^^^^^^^^^^^^^^^^^^^^^^^
9597
|
96-
= help: or try changing either arm body
97-
note: other arm here
98-
--> tests/ui/match_same_arms2.rs:86:9
98+
= help: try changing either arm body
99+
help: or try merging the arm patterns
100+
|
101+
LL | (Some(a), ..) | (.., Some(a)) => bar(a),
102+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
103+
help: and remove this obsolete arm
104+
|
105+
LL - (.., Some(a)) => bar(a),
99106
|
100-
LL | (.., Some(a)) => bar(a),
101-
| ^^^^^^^^^^^^^^^^^^^^^^^
102107

103108
error: this match arm has an identical body to another arm
104-
--> tests/ui/match_same_arms2.rs:119:9
109+
--> tests/ui/match_same_arms2.rs:120:9
105110
|
106111
LL | (Ok(x), Some(_)) => println!("ok {}", x),
107-
| ----------------^^^^^^^^^^^^^^^^^^^^^^^^
108-
| |
109-
| help: try merging the arm patterns: `(Ok(x), Some(_)) | (Ok(_), Some(x))`
112+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
110113
|
111-
= help: or try changing either arm body
112-
note: other arm here
113-
--> tests/ui/match_same_arms2.rs:120:9
114+
= help: try changing either arm body
115+
help: or try merging the arm patterns
116+
|
117+
LL | (Ok(x), Some(_)) | (Ok(_), Some(x)) => println!("ok {}", x),
118+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
119+
help: and remove this obsolete arm
120+
|
121+
LL - (Ok(_), Some(x)) => println!("ok {}", x),
114122
|
115-
LL | (Ok(_), Some(x)) => println!("ok {}", x),
116-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
117123

118124
error: this match arm has an identical body to another arm
119-
--> tests/ui/match_same_arms2.rs:135:9
125+
--> tests/ui/match_same_arms2.rs:136:9
120126
|
121127
LL | Ok(_) => println!("ok"),
122-
| -----^^^^^^^^^^^^^^^^^^
123-
| |
124-
| help: try merging the arm patterns: `Ok(_) | Ok(3)`
128+
| ^^^^^^^^^^^^^^^^^^^^^^^
125129
|
126-
= help: or try changing either arm body
127-
note: other arm here
128-
--> tests/ui/match_same_arms2.rs:134:9
130+
= help: try changing either arm body
131+
help: or try merging the arm patterns
132+
|
133+
LL | Ok(_) | Ok(3) => println!("ok"),
134+
| ~~~~~~~~~~~~~
135+
help: and remove this obsolete arm
136+
|
137+
LL - Ok(3) => println!("ok"),
129138
|
130-
LL | Ok(3) => println!("ok"),
131-
| ^^^^^^^^^^^^^^^^^^^^^^^
132139

133140
error: this match arm has an identical body to another arm
134-
--> tests/ui/match_same_arms2.rs:162:9
141+
--> tests/ui/match_same_arms2.rs:163:9
135142
|
136-
LL | 1 => {
137-
| ^ help: try merging the arm patterns: `1 | 0`
138-
| _________|
139-
| |
143+
LL | / 1 => {
140144
LL | | empty!(0);
141145
LL | | },
142146
| |_________^
143147
|
144-
= help: or try changing either arm body
145-
note: other arm here
146-
--> tests/ui/match_same_arms2.rs:159:9
148+
= help: try changing either arm body
149+
help: or try merging the arm patterns
150+
|
151+
LL | 1 | 0 => {
152+
| ~~~~~
153+
help: and remove this obsolete arm
154+
|
155+
LL - 0 => {
156+
LL - empty!(0);
157+
LL - },
147158
|
148-
LL | / 0 => {
149-
LL | | empty!(0);
150-
LL | | },
151-
| |_________^
152-
153-
error: match expression looks like `matches!` macro
154-
--> tests/ui/match_same_arms2.rs:181:16
155-
|
156-
LL | let _ans = match x {
157-
| ________________^
158-
LL | | E::A => false,
159-
LL | | E::B => false,
160-
LL | | _ => true,
161-
LL | | };
162-
| |_____^ help: try: `!matches!(x, E::A | E::B)`
163-
|
164-
= note: `-D clippy::match-like-matches-macro` implied by `-D warnings`
165-
= help: to override `-D warnings` add `#[allow(clippy::match_like_matches_macro)]`
166159

167160
error: this match arm has an identical body to another arm
168-
--> tests/ui/match_same_arms2.rs:213:9
161+
--> tests/ui/match_same_arms2.rs:214:9
169162
|
170163
LL | Foo::X(0) => 1,
171-
| ---------^^^^^
172-
| |
173-
| help: try merging the arm patterns: `Foo::X(0) | Foo::Z(_)`
164+
| ^^^^^^^^^^^^^^
174165
|
175-
= help: or try changing either arm body
176-
note: other arm here
177-
--> tests/ui/match_same_arms2.rs:215:9
166+
= help: try changing either arm body
167+
help: or try merging the arm patterns
168+
|
169+
LL | Foo::X(0) | Foo::Z(_) => 1,
170+
| ~~~~~~~~~~~~~~~~~~~~~
171+
help: and remove this obsolete arm
172+
|
173+
LL - Foo::Z(_) => 1,
178174
|
179-
LL | Foo::Z(_) => 1,
180-
| ^^^^^^^^^^^^^^
181175

182176
error: this match arm has an identical body to another arm
183-
--> tests/ui/match_same_arms2.rs:223:9
177+
--> tests/ui/match_same_arms2.rs:224:9
184178
|
185179
LL | Foo::Z(_) => 1,
186-
| ---------^^^^^
187-
| |
188-
| help: try merging the arm patterns: `Foo::Z(_) | Foo::X(0)`
180+
| ^^^^^^^^^^^^^^
189181
|
190-
= help: or try changing either arm body
191-
note: other arm here
192-
--> tests/ui/match_same_arms2.rs:221:9
182+
= help: try changing either arm body
183+
help: or try merging the arm patterns
184+
|
185+
LL | Foo::Z(_) | Foo::X(0) => 1,
186+
| ~~~~~~~~~~~~~~~~~~~~~
187+
help: and remove this obsolete arm
188+
|
189+
LL - Foo::X(0) => 1,
193190
|
194-
LL | Foo::X(0) => 1,
195-
| ^^^^^^^^^^^^^^
196191

197192
error: this match arm has an identical body to another arm
198-
--> tests/ui/match_same_arms2.rs:246:9
193+
--> tests/ui/match_same_arms2.rs:247:9
199194
|
200195
LL | Some(Bar { y: 0, x: 5, .. }) => 1,
201-
| ----------------------------^^^^^
202-
| |
203-
| help: try merging the arm patterns: `Some(Bar { y: 0, x: 5, .. }) | Some(Bar { x: 0, y: 5, .. })`
196+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
204197
|
205-
= help: or try changing either arm body
206-
note: other arm here
207-
--> tests/ui/match_same_arms2.rs:243:9
198+
= help: try changing either arm body
199+
help: or try merging the arm patterns
200+
|
201+
LL | Some(Bar { y: 0, x: 5, .. }) | Some(Bar { x: 0, y: 5, .. }) => 1,
202+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
203+
help: and remove this obsolete arm
204+
|
205+
LL - Some(Bar { x: 0, y: 5, .. }) => 1,
208206
|
209-
LL | Some(Bar { x: 0, y: 5, .. }) => 1,
210-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
211207

212208
error: this match arm has an identical body to another arm
213-
--> tests/ui/match_same_arms2.rs:260:9
209+
--> tests/ui/match_same_arms2.rs:261:9
214210
|
215211
LL | 1 => cfg!(not_enable),
216-
| -^^^^^^^^^^^^^^^^^^^^
217-
| |
218-
| help: try merging the arm patterns: `1 | 0`
212+
| ^^^^^^^^^^^^^^^^^^^^^
219213
|
220-
= help: or try changing either arm body
221-
note: other arm here
222-
--> tests/ui/match_same_arms2.rs:259:9
214+
= help: try changing either arm body
215+
help: or try merging the arm patterns
216+
|
217+
LL | 1 | 0 => cfg!(not_enable),
218+
| ~~~~~
219+
help: and remove this obsolete arm
220+
|
221+
LL - 0 => cfg!(not_enable),
223222
|
224-
LL | 0 => cfg!(not_enable),
225-
| ^^^^^^^^^^^^^^^^^^^^^
226223

227-
error: aborting due to 14 previous errors
224+
error: aborting due to 13 previous errors
228225

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#![feature(non_exhaustive_omitted_patterns_lint)]
2+
#![warn(clippy::match_same_arms)]
3+
#![no_main]
4+
use std::sync::atomic::Ordering; // #[non_exhaustive] enum
5+
6+
fn repeat() -> ! {
7+
panic!()
8+
}
9+
10+
pub fn f(x: Ordering) {
11+
#[deny(non_exhaustive_omitted_patterns)]
12+
match x {
13+
Ordering::Relaxed => println!("relaxed"),
14+
Ordering::Release => println!("release"),
15+
Ordering::Acquire => println!("acquire"),
16+
Ordering::AcqRel | Ordering::SeqCst => repeat(),
17+
_ => repeat(),
18+
}
19+
}
20+
21+
mod f {
22+
#![deny(non_exhaustive_omitted_patterns)]
23+
24+
use super::*;
25+
26+
pub fn f(x: Ordering) {
27+
match x {
28+
Ordering::Relaxed => println!("relaxed"),
29+
Ordering::Release => println!("release"),
30+
Ordering::Acquire => println!("acquire"),
31+
Ordering::AcqRel | Ordering::SeqCst => repeat(),
32+
_ => repeat(),
33+
}
34+
}
35+
}
36+
37+
// Below should still lint
38+
39+
pub fn g(x: Ordering) {
40+
match x {
41+
Ordering::Relaxed => println!("relaxed"),
42+
Ordering::Release => println!("release"),
43+
Ordering::Acquire => println!("acquire"),
44+
//~^ ERROR: this match arm has an identical body to the `_` wildcard arm
45+
_ => repeat(),
46+
}
47+
}
48+
49+
mod g {
50+
use super::*;
51+
52+
pub fn g(x: Ordering) {
53+
match x {
54+
Ordering::Relaxed => println!("relaxed"),
55+
Ordering::Release => println!("release"),
56+
Ordering::Acquire => println!("acquire"),
57+
//~^ ERROR: this match arm has an identical body to the `_` wildcard arm
58+
_ => repeat(),
59+
}
60+
}
61+
}

‎tests/ui/match_same_arms_non_exhaustive.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#![feature(non_exhaustive_omitted_patterns_lint)]
22
#![warn(clippy::match_same_arms)]
33
#![no_main]
4-
//@no-rustfix
54
use std::sync::atomic::Ordering; // #[non_exhaustive] enum
65

76
fn repeat() -> ! {

‎tests/ui/match_same_arms_non_exhaustive.stderr

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
11
error: this match arm has an identical body to the `_` wildcard arm
2-
--> tests/ui/match_same_arms_non_exhaustive.rs:45:9
2+
--> tests/ui/match_same_arms_non_exhaustive.rs:44:9
33
|
4-
LL | Ordering::AcqRel | Ordering::SeqCst => repeat(),
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try removing the arm
4+
LL | / Ordering::AcqRel | Ordering::SeqCst => repeat(),
5+
LL | |
6+
| |________^ help: try removing the arm
67
|
78
= help: or try changing either arm body
89
note: `_` wildcard arm here
9-
--> tests/ui/match_same_arms_non_exhaustive.rs:47:9
10+
--> tests/ui/match_same_arms_non_exhaustive.rs:46:9
1011
|
1112
LL | _ => repeat(),
1213
| ^^^^^^^^^^^^^
1314
= note: `-D clippy::match-same-arms` implied by `-D warnings`
1415
= help: to override `-D warnings` add `#[allow(clippy::match_same_arms)]`
1516

1617
error: this match arm has an identical body to the `_` wildcard arm
17-
--> tests/ui/match_same_arms_non_exhaustive.rs:59:13
18+
--> tests/ui/match_same_arms_non_exhaustive.rs:58:13
1819
|
19-
LL | Ordering::AcqRel | Ordering::SeqCst => repeat(),
20-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try removing the arm
20+
LL | / Ordering::AcqRel | Ordering::SeqCst => repeat(),
21+
LL | |
22+
| |____________^ help: try removing the arm
2123
|
2224
= help: or try changing either arm body
2325
note: `_` wildcard arm here
24-
--> tests/ui/match_same_arms_non_exhaustive.rs:61:13
26+
--> tests/ui/match_same_arms_non_exhaustive.rs:60:13
2527
|
2628
LL | _ => repeat(),
2729
| ^^^^^^^^^^^^^

0 commit comments

Comments
 (0)
Please sign in to comment.