Skip to content

Commit 6ff4e71

Browse files
committed
Auto merge of #12448 - WeiTheShinobi:fix_single_match, r=dswij
[`single_match`]: Fix duplicate diagnostics Relates to #12379 edit two test file `tests/ui/single_match_else.rs` `tests/ui/single_match.rs` those two test file point to the same lint --- changelog: [`single_match`] Fix duplicate diagnostics
2 parents 02156dc + 8e55bbf commit 6ff4e71

7 files changed

+34
-57
lines changed

clippy_lints/src/matches/single_match.rs

+7-24
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,15 @@ pub(crate) fn check(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], expr:
5555
};
5656

5757
let ty = cx.typeck_results().expr_ty(ex);
58-
if *ty.kind() != ty::Bool || is_lint_allowed(cx, MATCH_BOOL, ex.hir_id) {
59-
check_single_pattern(cx, ex, arms, expr, els);
60-
check_opt_like(cx, ex, arms, expr, ty, els);
58+
if (*ty.kind() != ty::Bool || is_lint_allowed(cx, MATCH_BOOL, ex.hir_id)) &&
59+
(check_single_pattern(arms) || check_opt_like(cx, arms, ty)) {
60+
report_single_pattern(cx, ex, arms, expr, els);
6161
}
6262
}
6363
}
6464

65-
fn check_single_pattern(
66-
cx: &LateContext<'_>,
67-
ex: &Expr<'_>,
68-
arms: &[Arm<'_>],
69-
expr: &Expr<'_>,
70-
els: Option<&Expr<'_>>,
71-
) {
72-
if is_wild(arms[1].pat) {
73-
report_single_pattern(cx, ex, arms, expr, els);
74-
}
65+
fn check_single_pattern(arms: &[Arm<'_>]) -> bool {
66+
is_wild(arms[1].pat)
7567
}
7668

7769
fn report_single_pattern(
@@ -140,19 +132,10 @@ fn report_single_pattern(
140132
span_lint_and_sugg(cx, lint, expr.span, msg, "try", sugg, app);
141133
}
142134

143-
fn check_opt_like<'a>(
144-
cx: &LateContext<'a>,
145-
ex: &Expr<'_>,
146-
arms: &[Arm<'_>],
147-
expr: &Expr<'_>,
148-
ty: Ty<'a>,
149-
els: Option<&Expr<'_>>,
150-
) {
135+
fn check_opt_like<'a>(cx: &LateContext<'a>, arms: &[Arm<'_>], ty: Ty<'a>) -> bool {
151136
// We don't want to lint if the second arm contains an enum which could
152137
// have more variants in the future.
153-
if form_exhaustive_matches(cx, ty, arms[0].pat, arms[1].pat) {
154-
report_single_pattern(cx, ex, arms, expr, els);
155-
}
138+
form_exhaustive_matches(cx, ty, arms[0].pat, arms[1].pat)
156139
}
157140

158141
/// Returns `true` if all of the types in the pattern are enums which we know

tests/ui/single_match.fixed

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//@compile-flags: -Zdeduplicate-diagnostics=yes
2-
31
#![warn(clippy::single_match)]
42
#![allow(
53
unused,

tests/ui/single_match.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//@compile-flags: -Zdeduplicate-diagnostics=yes
2-
31
#![warn(clippy::single_match)]
42
#![allow(
53
unused,

tests/ui/single_match.stderr

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
2-
--> tests/ui/single_match.rs:17:5
2+
--> tests/ui/single_match.rs:15:5
33
|
44
LL | / match x {
55
LL | | Some(y) => {
@@ -19,7 +19,7 @@ LL ~ };
1919
|
2020

2121
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
22-
--> tests/ui/single_match.rs:25:5
22+
--> tests/ui/single_match.rs:23:5
2323
|
2424
LL | / match x {
2525
LL | | // Note the missing block braces.
@@ -31,7 +31,7 @@ LL | | }
3131
| |_____^ help: try: `if let Some(y) = x { println!("{:?}", y) }`
3232

3333
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
34-
--> tests/ui/single_match.rs:34:5
34+
--> tests/ui/single_match.rs:32:5
3535
|
3636
LL | / match z {
3737
LL | | (2..=3, 7..=9) => dummy(),
@@ -40,7 +40,7 @@ LL | | };
4040
| |_____^ help: try: `if let (2..=3, 7..=9) = z { dummy() }`
4141

4242
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
43-
--> tests/ui/single_match.rs:63:5
43+
--> tests/ui/single_match.rs:61:5
4444
|
4545
LL | / match x {
4646
LL | | Some(y) => dummy(),
@@ -49,7 +49,7 @@ LL | | };
4949
| |_____^ help: try: `if let Some(y) = x { dummy() }`
5050

5151
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
52-
--> tests/ui/single_match.rs:68:5
52+
--> tests/ui/single_match.rs:66:5
5353
|
5454
LL | / match y {
5555
LL | | Ok(y) => dummy(),
@@ -58,7 +58,7 @@ LL | | };
5858
| |_____^ help: try: `if let Ok(y) = y { dummy() }`
5959

6060
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
61-
--> tests/ui/single_match.rs:75:5
61+
--> tests/ui/single_match.rs:73:5
6262
|
6363
LL | / match c {
6464
LL | | Cow::Borrowed(..) => dummy(),
@@ -67,7 +67,7 @@ LL | | };
6767
| |_____^ help: try: `if let Cow::Borrowed(..) = c { dummy() }`
6868

6969
error: you seem to be trying to use `match` for an equality check. Consider using `if`
70-
--> tests/ui/single_match.rs:96:5
70+
--> tests/ui/single_match.rs:94:5
7171
|
7272
LL | / match x {
7373
LL | | "test" => println!(),
@@ -76,7 +76,7 @@ LL | | }
7676
| |_____^ help: try: `if x == "test" { println!() }`
7777

7878
error: you seem to be trying to use `match` for an equality check. Consider using `if`
79-
--> tests/ui/single_match.rs:109:5
79+
--> tests/ui/single_match.rs:107:5
8080
|
8181
LL | / match x {
8282
LL | | Foo::A => println!(),
@@ -85,7 +85,7 @@ LL | | }
8585
| |_____^ help: try: `if x == Foo::A { println!() }`
8686

8787
error: you seem to be trying to use `match` for an equality check. Consider using `if`
88-
--> tests/ui/single_match.rs:115:5
88+
--> tests/ui/single_match.rs:113:5
8989
|
9090
LL | / match x {
9191
LL | | FOO_C => println!(),
@@ -94,7 +94,7 @@ LL | | }
9494
| |_____^ help: try: `if x == FOO_C { println!() }`
9595

9696
error: you seem to be trying to use `match` for an equality check. Consider using `if`
97-
--> tests/ui/single_match.rs:120:5
97+
--> tests/ui/single_match.rs:118:5
9898
|
9999
LL | / match &&x {
100100
LL | | Foo::A => println!(),
@@ -103,7 +103,7 @@ LL | | }
103103
| |_____^ help: try: `if x == Foo::A { println!() }`
104104

105105
error: you seem to be trying to use `match` for an equality check. Consider using `if`
106-
--> tests/ui/single_match.rs:126:5
106+
--> tests/ui/single_match.rs:124:5
107107
|
108108
LL | / match &x {
109109
LL | | Foo::A => println!(),
@@ -112,7 +112,7 @@ LL | | }
112112
| |_____^ help: try: `if x == &Foo::A { println!() }`
113113

114114
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
115-
--> tests/ui/single_match.rs:143:5
115+
--> tests/ui/single_match.rs:141:5
116116
|
117117
LL | / match x {
118118
LL | | Bar::A => println!(),
@@ -121,7 +121,7 @@ LL | | }
121121
| |_____^ help: try: `if let Bar::A = x { println!() }`
122122

123123
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
124-
--> tests/ui/single_match.rs:151:5
124+
--> tests/ui/single_match.rs:149:5
125125
|
126126
LL | / match x {
127127
LL | | None => println!(),
@@ -130,7 +130,7 @@ LL | | };
130130
| |_____^ help: try: `if let None = x { println!() }`
131131

132132
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
133-
--> tests/ui/single_match.rs:173:5
133+
--> tests/ui/single_match.rs:171:5
134134
|
135135
LL | / match x {
136136
LL | | (Some(_), _) => {},
@@ -139,7 +139,7 @@ LL | | }
139139
| |_____^ help: try: `if let (Some(_), _) = x {}`
140140

141141
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
142-
--> tests/ui/single_match.rs:179:5
142+
--> tests/ui/single_match.rs:177:5
143143
|
144144
LL | / match x {
145145
LL | | (Some(E::V), _) => todo!(),
@@ -148,7 +148,7 @@ LL | | }
148148
| |_____^ help: try: `if let (Some(E::V), _) = x { todo!() }`
149149

150150
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
151-
--> tests/ui/single_match.rs:185:5
151+
--> tests/ui/single_match.rs:183:5
152152
|
153153
LL | / match (Some(42), Some(E::V), Some(42)) {
154154
LL | | (.., Some(E::V), _) => {},
@@ -157,7 +157,7 @@ LL | | }
157157
| |_____^ help: try: `if let (.., Some(E::V), _) = (Some(42), Some(E::V), Some(42)) {}`
158158

159159
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
160-
--> tests/ui/single_match.rs:257:5
160+
--> tests/ui/single_match.rs:255:5
161161
|
162162
LL | / match bar {
163163
LL | | Some(v) => unsafe {
@@ -177,7 +177,7 @@ LL + } }
177177
|
178178

179179
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
180-
--> tests/ui/single_match.rs:265:5
180+
--> tests/ui/single_match.rs:263:5
181181
|
182182
LL | / match bar {
183183
LL | | #[rustfmt::skip]

tests/ui/single_match_else.fixed

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//@aux-build: proc_macros.rs
2-
//@compile-flags: -Zdeduplicate-diagnostics=yes
32

43
#![warn(clippy::single_match_else)]
54
#![allow(unused, clippy::needless_return, clippy::no_effect, clippy::uninlined_format_args)]

tests/ui/single_match_else.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//@aux-build: proc_macros.rs
2-
//@compile-flags: -Zdeduplicate-diagnostics=yes
32

43
#![warn(clippy::single_match_else)]
54
#![allow(unused, clippy::needless_return, clippy::no_effect, clippy::uninlined_format_args)]

tests/ui/single_match_else.stderr

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
2-
--> tests/ui/single_match_else.rs:18:13
2+
--> tests/ui/single_match_else.rs:17:13
33
|
44
LL | let _ = match ExprNode::Butterflies {
55
| _____________^
@@ -22,7 +22,7 @@ LL ~ };
2222
|
2323

2424
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
25-
--> tests/ui/single_match_else.rs:83:5
25+
--> tests/ui/single_match_else.rs:82:5
2626
|
2727
LL | / match Some(1) {
2828
LL | | Some(a) => println!("${:?}", a),
@@ -42,7 +42,7 @@ LL + }
4242
|
4343

4444
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
45-
--> tests/ui/single_match_else.rs:92:5
45+
--> tests/ui/single_match_else.rs:91:5
4646
|
4747
LL | / match Some(1) {
4848
LL | | Some(a) => println!("${:?}", a),
@@ -62,7 +62,7 @@ LL + }
6262
|
6363

6464
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
65-
--> tests/ui/single_match_else.rs:102:5
65+
--> tests/ui/single_match_else.rs:101:5
6666
|
6767
LL | / match Result::<i32, Infallible>::Ok(1) {
6868
LL | | Ok(a) => println!("${:?}", a),
@@ -82,7 +82,7 @@ LL + }
8282
|
8383

8484
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
85-
--> tests/ui/single_match_else.rs:111:5
85+
--> tests/ui/single_match_else.rs:110:5
8686
|
8787
LL | / match Cow::from("moo") {
8888
LL | | Cow::Owned(a) => println!("${:?}", a),
@@ -102,7 +102,7 @@ LL + }
102102
|
103103

104104
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
105-
--> tests/ui/single_match_else.rs:121:5
105+
--> tests/ui/single_match_else.rs:120:5
106106
|
107107
LL | / match bar {
108108
LL | | Some(v) => unsafe {
@@ -125,7 +125,7 @@ LL + }
125125
|
126126

127127
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
128-
--> tests/ui/single_match_else.rs:132:5
128+
--> tests/ui/single_match_else.rs:131:5
129129
|
130130
LL | / match bar {
131131
LL | | Some(v) => {
@@ -149,7 +149,7 @@ LL + } }
149149
|
150150

151151
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
152-
--> tests/ui/single_match_else.rs:144:5
152+
--> tests/ui/single_match_else.rs:143:5
153153
|
154154
LL | / match bar {
155155
LL | | Some(v) => unsafe {
@@ -173,7 +173,7 @@ LL + } }
173173
|
174174

175175
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
176-
--> tests/ui/single_match_else.rs:156:5
176+
--> tests/ui/single_match_else.rs:155:5
177177
|
178178
LL | / match bar {
179179
LL | | #[rustfmt::skip]

0 commit comments

Comments
 (0)