Skip to content

Commit aa0706b

Browse files
committed
Auto merge of #9178 - alex-semenyuk:match_like_matches_macro_fix, r=Jarcho
match_like_matches_macro does not trigger when one arm contains conta… Close #9163 changelog: none
2 parents fe68145 + 2894df3 commit aa0706b

File tree

4 files changed

+43
-12
lines changed

4 files changed

+43
-12
lines changed

clippy_lints/src/matches/match_like_matches.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,14 @@ where
8181
if let Some((_, last_pat_opt, last_expr, _)) = iter.next_back();
8282
let iter_without_last = iter.clone();
8383
if let Some((first_attrs, _, first_expr, first_guard)) = iter.next();
84-
if let Some(b0) = find_bool_lit(&first_expr.kind, is_if_let);
85-
if let Some(b1) = find_bool_lit(&last_expr.kind, is_if_let);
84+
if let Some(b0) = find_bool_lit(&first_expr.kind);
85+
if let Some(b1) = find_bool_lit(&last_expr.kind);
8686
if b0 != b1;
8787
if first_guard.is_none() || iter.len() == 0;
8888
if first_attrs.is_empty();
8989
if iter
9090
.all(|arm| {
91-
find_bool_lit(&arm.2.kind, is_if_let).map_or(false, |b| b == b0) && arm.3.is_none() && arm.0.is_empty()
91+
find_bool_lit(&arm.2.kind).map_or(false, |b| b == b0) && arm.3.is_none() && arm.0.is_empty()
9292
});
9393
then {
9494
if let Some(last_pat) = last_pat_opt {
@@ -144,7 +144,7 @@ where
144144
}
145145

146146
/// Extract a `bool` or `{ bool }`
147-
fn find_bool_lit(ex: &ExprKind<'_>, is_if_let: bool) -> Option<bool> {
147+
fn find_bool_lit(ex: &ExprKind<'_>) -> Option<bool> {
148148
match ex {
149149
ExprKind::Lit(Spanned {
150150
node: LitKind::Bool(b), ..
@@ -156,7 +156,7 @@ fn find_bool_lit(ex: &ExprKind<'_>, is_if_let: bool) -> Option<bool> {
156156
..
157157
},
158158
_,
159-
) if is_if_let => {
159+
) => {
160160
if let ExprKind::Lit(Spanned {
161161
node: LitKind::Bool(b), ..
162162
}) = exp.kind

tests/ui/match_expr_like_matches_macro.fixed

+6
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ fn main() {
4545
// lint
4646
let _ans = matches!(x, E::A(_) | E::B(_));
4747
}
48+
{
49+
// lint
50+
// skip rustfmt to prevent removing block for first pattern
51+
#[rustfmt::skip]
52+
let _ans = matches!(x, E::A(_) | E::B(_));
53+
}
4854
{
4955
// lint
5056
let _ans = !matches!(x, E::B(_) | E::C);

tests/ui/match_expr_like_matches_macro.rs

+12
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ fn main() {
6161
_ => false,
6262
};
6363
}
64+
{
65+
// lint
66+
// skip rustfmt to prevent removing block for first pattern
67+
#[rustfmt::skip]
68+
let _ans = match x {
69+
E::A(_) => {
70+
true
71+
}
72+
E::B(_) => true,
73+
_ => false,
74+
};
75+
}
6476
{
6577
// lint
6678
let _ans = match x {

tests/ui/match_expr_like_matches_macro.stderr

+20-7
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,20 @@ LL | | };
6060
| |_________^ help: try this: `matches!(x, E::A(_) | E::B(_))`
6161

6262
error: match expression looks like `matches!` macro
63-
--> $DIR/match_expr_like_matches_macro.rs:66:20
63+
--> $DIR/match_expr_like_matches_macro.rs:68:20
64+
|
65+
LL | let _ans = match x {
66+
| ____________________^
67+
LL | | E::A(_) => {
68+
LL | | true
69+
LL | | }
70+
LL | | E::B(_) => true,
71+
LL | | _ => false,
72+
LL | | };
73+
| |_________^ help: try this: `matches!(x, E::A(_) | E::B(_))`
74+
75+
error: match expression looks like `matches!` macro
76+
--> $DIR/match_expr_like_matches_macro.rs:78:20
6477
|
6578
LL | let _ans = match x {
6679
| ____________________^
@@ -71,7 +84,7 @@ LL | | };
7184
| |_________^ help: try this: `!matches!(x, E::B(_) | E::C)`
7285

7386
error: match expression looks like `matches!` macro
74-
--> $DIR/match_expr_like_matches_macro.rs:126:18
87+
--> $DIR/match_expr_like_matches_macro.rs:138:18
7588
|
7689
LL | let _z = match &z {
7790
| __________________^
@@ -81,7 +94,7 @@ LL | | };
8194
| |_________^ help: try this: `matches!(z, Some(3))`
8295

8396
error: match expression looks like `matches!` macro
84-
--> $DIR/match_expr_like_matches_macro.rs:135:18
97+
--> $DIR/match_expr_like_matches_macro.rs:147:18
8598
|
8699
LL | let _z = match &z {
87100
| __________________^
@@ -91,7 +104,7 @@ LL | | };
91104
| |_________^ help: try this: `matches!(&z, Some(3))`
92105

93106
error: match expression looks like `matches!` macro
94-
--> $DIR/match_expr_like_matches_macro.rs:152:21
107+
--> $DIR/match_expr_like_matches_macro.rs:164:21
95108
|
96109
LL | let _ = match &z {
97110
| _____________________^
@@ -101,7 +114,7 @@ LL | | };
101114
| |_____________^ help: try this: `matches!(&z, AnEnum::X)`
102115

103116
error: match expression looks like `matches!` macro
104-
--> $DIR/match_expr_like_matches_macro.rs:166:20
117+
--> $DIR/match_expr_like_matches_macro.rs:178:20
105118
|
106119
LL | let _res = match &val {
107120
| ____________________^
@@ -111,7 +124,7 @@ LL | | };
111124
| |_________^ help: try this: `matches!(&val, &Some(ref _a))`
112125

113126
error: match expression looks like `matches!` macro
114-
--> $DIR/match_expr_like_matches_macro.rs:178:20
127+
--> $DIR/match_expr_like_matches_macro.rs:190:20
115128
|
116129
LL | let _res = match &val {
117130
| ____________________^
@@ -120,5 +133,5 @@ LL | | _ => false,
120133
LL | | };
121134
| |_________^ help: try this: `matches!(&val, &Some(ref _a))`
122135

123-
error: aborting due to 12 previous errors
136+
error: aborting due to 13 previous errors
124137

0 commit comments

Comments
 (0)