Skip to content

Commit 8cd122d

Browse files
committed
[match_same_arms]: don't lint if non_exhaustive_omitted_patterns
formatting :/
1 parent 72332b2 commit 8cd122d

File tree

3 files changed

+86
-53
lines changed

3 files changed

+86
-53
lines changed

clippy_lints/src/matches/match_same_arms.rs

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::source::snippet;
3-
use clippy_utils::{path_to_local, search_same, SpanlessEq, SpanlessHash};
3+
use clippy_utils::{is_lint_allowed, path_to_local, search_same, SpanlessEq, SpanlessHash};
44
use core::cmp::Ordering;
55
use core::iter;
66
use core::slice;
@@ -9,6 +9,7 @@ use rustc_ast::ast::LitKind;
99
use rustc_errors::Applicability;
1010
use rustc_hir::def_id::DefId;
1111
use rustc_hir::{Arm, Expr, ExprKind, HirId, HirIdMap, HirIdMapEntry, HirIdSet, Pat, PatKind, RangeEnd};
12+
use rustc_lint::builtin::NON_EXHAUSTIVE_OMITTED_PATTERNS;
1213
use rustc_lint::LateContext;
1314
use rustc_middle::ty;
1415
use rustc_span::Symbol;
@@ -102,45 +103,47 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>]) {
102103

103104
let indexed_arms: Vec<(usize, &Arm<'_>)> = arms.iter().enumerate().collect();
104105
for (&(i, arm1), &(j, arm2)) in search_same(&indexed_arms, hash, eq) {
105-
if matches!(arm2.pat.kind, PatKind::Wild) {
106-
span_lint_and_then(
107-
cx,
108-
MATCH_SAME_ARMS,
109-
arm1.span,
110-
"this match arm has an identical body to the `_` wildcard arm",
111-
|diag| {
112-
diag.span_suggestion(arm1.span, "try removing the arm", "", Applicability::MaybeIncorrect)
113-
.help("or try changing either arm body")
114-
.span_note(arm2.span, "`_` wildcard arm here");
115-
},
116-
);
117-
} else {
118-
let back_block = backwards_blocking_idxs[j];
119-
let (keep_arm, move_arm) = if back_block < i || (back_block == 0 && forwards_blocking_idxs[i] <= j) {
120-
(arm1, arm2)
106+
if is_lint_allowed(cx, NON_EXHAUSTIVE_OMITTED_PATTERNS, arm2.hir_id) {
107+
if matches!(arm2.pat.kind, PatKind::Wild) {
108+
span_lint_and_then(
109+
cx,
110+
MATCH_SAME_ARMS,
111+
arm1.span,
112+
"this match arm has an identical body to the `_` wildcard arm",
113+
|diag| {
114+
diag.span_suggestion(arm1.span, "try removing the arm", "", Applicability::MaybeIncorrect)
115+
.help("or try changing either arm body")
116+
.span_note(arm2.span, "`_` wildcard arm here");
117+
},
118+
);
121119
} else {
122-
(arm2, arm1)
123-
};
120+
let back_block = backwards_blocking_idxs[j];
121+
let (keep_arm, move_arm) = if back_block < i || (back_block == 0 && forwards_blocking_idxs[i] <= j) {
122+
(arm1, arm2)
123+
} else {
124+
(arm2, arm1)
125+
};
124126

125-
span_lint_and_then(
126-
cx,
127-
MATCH_SAME_ARMS,
128-
keep_arm.span,
129-
"this match arm has an identical body to another arm",
130-
|diag| {
131-
let move_pat_snip = snippet(cx, move_arm.pat.span, "<pat2>");
132-
let keep_pat_snip = snippet(cx, keep_arm.pat.span, "<pat1>");
127+
span_lint_and_then(
128+
cx,
129+
MATCH_SAME_ARMS,
130+
keep_arm.span,
131+
"this match arm has an identical body to another arm",
132+
|diag| {
133+
let move_pat_snip = snippet(cx, move_arm.pat.span, "<pat2>");
134+
let keep_pat_snip = snippet(cx, keep_arm.pat.span, "<pat1>");
133135

134-
diag.span_suggestion(
135-
keep_arm.pat.span,
136-
"try merging the arm patterns",
137-
format!("{keep_pat_snip} | {move_pat_snip}"),
138-
Applicability::MaybeIncorrect,
139-
)
140-
.help("or try changing either arm body")
141-
.span_note(move_arm.span, "other arm here");
142-
},
143-
);
136+
diag.span_suggestion(
137+
keep_arm.pat.span,
138+
"try merging the arm patterns",
139+
format!("{keep_pat_snip} | {move_pat_snip}"),
140+
Applicability::MaybeIncorrect,
141+
)
142+
.help("or try changing either arm body")
143+
.span_note(move_arm.span, "other arm here");
144+
},
145+
);
146+
}
144147
}
145148
}
146149
}

tests/ui/match_same_arms.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
1+
#![feature(non_exhaustive_omitted_patterns_lint)]
12
#![warn(clippy::match_same_arms)]
23

4+
use std::sync::atomic::Ordering; // #[non_exhaustive] enum
5+
6+
pub fn f(x: Ordering) {
7+
match x {
8+
Ordering::Relaxed => println!("relaxed"),
9+
Ordering::Release => println!("release"),
10+
Ordering::Acquire => println!("acquire"),
11+
Ordering::AcqRel | Ordering::SeqCst => panic!(),
12+
#[deny(non_exhaustive_omitted_patterns)]
13+
_ => panic!(),
14+
}
15+
}
16+
17+
mod f {
18+
#![deny(non_exhaustive_omitted_patterns)]
19+
20+
use super::*;
21+
22+
pub fn f(x: Ordering) {
23+
match x {
24+
Ordering::Relaxed => println!("relaxed"),
25+
Ordering::Release => println!("release"),
26+
Ordering::Acquire => println!("acquire"),
27+
Ordering::AcqRel | Ordering::SeqCst => panic!(),
28+
_ => panic!(),
29+
}
30+
}
31+
}
32+
333
pub enum Abc {
434
A,
535
B,

tests/ui/match_same_arms.stderr

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
error: this match arm has an identical body to the `_` wildcard arm
2-
--> $DIR/match_same_arms.rs:11:9
2+
--> $DIR/match_same_arms.rs:41:9
33
|
44
LL | Abc::A => 0,
55
| ^^^^^^^^^^^ help: try removing the arm
66
|
77
= help: or try changing either arm body
88
note: `_` wildcard arm here
9-
--> $DIR/match_same_arms.rs:13:9
9+
--> $DIR/match_same_arms.rs:43:9
1010
|
1111
LL | _ => 0, //~ ERROR match arms have same body
1212
| ^^^^^^
1313
= note: `-D clippy::match-same-arms` implied by `-D warnings`
1414

1515
error: this match arm has an identical body to another arm
16-
--> $DIR/match_same_arms.rs:17:9
16+
--> $DIR/match_same_arms.rs:47:9
1717
|
1818
LL | (1, .., 3) => 42,
1919
| ----------^^^^^^
@@ -22,13 +22,13 @@ LL | (1, .., 3) => 42,
2222
|
2323
= help: or try changing either arm body
2424
note: other arm here
25-
--> $DIR/match_same_arms.rs:18:9
25+
--> $DIR/match_same_arms.rs:48:9
2626
|
2727
LL | (.., 3) => 42, //~ ERROR match arms have same body
2828
| ^^^^^^^^^^^^^
2929

3030
error: this match arm has an identical body to another arm
31-
--> $DIR/match_same_arms.rs:24:9
31+
--> $DIR/match_same_arms.rs:54:9
3232
|
3333
LL | 51 => 1, //~ ERROR match arms have same body
3434
| --^^^^^
@@ -37,13 +37,13 @@ LL | 51 => 1, //~ ERROR match arms have same body
3737
|
3838
= help: or try changing either arm body
3939
note: other arm here
40-
--> $DIR/match_same_arms.rs:23:9
40+
--> $DIR/match_same_arms.rs:53:9
4141
|
4242
LL | 42 => 1,
4343
| ^^^^^^^
4444

4545
error: this match arm has an identical body to another arm
46-
--> $DIR/match_same_arms.rs:25:9
46+
--> $DIR/match_same_arms.rs:55:9
4747
|
4848
LL | 41 => 2,
4949
| --^^^^^
@@ -52,13 +52,13 @@ LL | 41 => 2,
5252
|
5353
= help: or try changing either arm body
5454
note: other arm here
55-
--> $DIR/match_same_arms.rs:26:9
55+
--> $DIR/match_same_arms.rs:56:9
5656
|
5757
LL | 52 => 2, //~ ERROR match arms have same body
5858
| ^^^^^^^
5959

6060
error: this match arm has an identical body to another arm
61-
--> $DIR/match_same_arms.rs:32:9
61+
--> $DIR/match_same_arms.rs:62:9
6262
|
6363
LL | 2 => 2, //~ ERROR 2nd matched arms have same body
6464
| -^^^^^
@@ -67,13 +67,13 @@ LL | 2 => 2, //~ ERROR 2nd matched arms have same body
6767
|
6868
= help: or try changing either arm body
6969
note: other arm here
70-
--> $DIR/match_same_arms.rs:31:9
70+
--> $DIR/match_same_arms.rs:61:9
7171
|
7272
LL | 1 => 2,
7373
| ^^^^^^
7474

7575
error: this match arm has an identical body to another arm
76-
--> $DIR/match_same_arms.rs:33:9
76+
--> $DIR/match_same_arms.rs:63:9
7777
|
7878
LL | 3 => 2, //~ ERROR 3rd matched arms have same body
7979
| -^^^^^
@@ -82,13 +82,13 @@ LL | 3 => 2, //~ ERROR 3rd matched arms have same body
8282
|
8383
= help: or try changing either arm body
8484
note: other arm here
85-
--> $DIR/match_same_arms.rs:31:9
85+
--> $DIR/match_same_arms.rs:61:9
8686
|
8787
LL | 1 => 2,
8888
| ^^^^^^
8989

9090
error: this match arm has an identical body to another arm
91-
--> $DIR/match_same_arms.rs:32:9
91+
--> $DIR/match_same_arms.rs:62:9
9292
|
9393
LL | 2 => 2, //~ ERROR 2nd matched arms have same body
9494
| -^^^^^
@@ -97,13 +97,13 @@ LL | 2 => 2, //~ ERROR 2nd matched arms have same body
9797
|
9898
= help: or try changing either arm body
9999
note: other arm here
100-
--> $DIR/match_same_arms.rs:33:9
100+
--> $DIR/match_same_arms.rs:63:9
101101
|
102102
LL | 3 => 2, //~ ERROR 3rd matched arms have same body
103103
| ^^^^^^
104104

105105
error: this match arm has an identical body to another arm
106-
--> $DIR/match_same_arms.rs:50:17
106+
--> $DIR/match_same_arms.rs:80:17
107107
|
108108
LL | CommandInfo::External { name, .. } => name.to_string(),
109109
| ----------------------------------^^^^^^^^^^^^^^^^^^^^
@@ -112,7 +112,7 @@ LL | CommandInfo::External { name, .. } => name.to_string(),
112112
|
113113
= help: or try changing either arm body
114114
note: other arm here
115-
--> $DIR/match_same_arms.rs:49:17
115+
--> $DIR/match_same_arms.rs:79:17
116116
|
117117
LL | CommandInfo::BuiltIn { name, .. } => name.to_string(),
118118
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)