Skip to content

Commit 6d0fafd

Browse files
committed
clean-up
1 parent 99b8106 commit 6d0fafd

File tree

4 files changed

+41
-40
lines changed

4 files changed

+41
-40
lines changed

clippy_lints/src/matches/match_like_matches.rs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Lint a `match` or `if let .. { .. } else { .. }` expr that could be replaced by `matches!`
2+
13
use super::REDUNDANT_PATTERN_MATCHING;
24
use clippy_utils::diagnostics::span_lint_and_sugg;
35
use clippy_utils::source::snippet_with_applicability;
@@ -11,7 +13,6 @@ use rustc_span::source_map::Spanned;
1113

1214
use super::MATCH_LIKE_MATCHES_MACRO;
1315

14-
/// Lint a `match` or `if let .. { .. } else { .. }` expr that could be replaced by `matches!`
1516
pub(crate) fn check_if_let<'tcx>(
1617
cx: &LateContext<'tcx>,
1718
expr: &'tcx Expr<'_>,
@@ -23,10 +24,11 @@ pub(crate) fn check_if_let<'tcx>(
2324
find_matches_sugg(
2425
cx,
2526
let_expr,
26-
IntoIterator::into_iter([
27+
[
2728
(&[][..], Some(let_pat), then_expr, None),
2829
(&[][..], None, else_expr, None),
29-
]),
30+
]
31+
.into_iter(),
3032
expr,
3133
true,
3234
);
@@ -52,7 +54,7 @@ pub(super) fn check_match<'tcx>(
5254
fn find_matches_sugg<'a, 'b, I>(
5355
cx: &LateContext<'_>,
5456
ex: &Expr<'_>,
55-
mut iter: I,
57+
mut arms: I,
5658
expr: &Expr<'_>,
5759
is_if_let: bool,
5860
) -> bool
@@ -64,28 +66,28 @@ where
6466
+ Iterator<Item = (&'a [Attribute], Option<&'a Pat<'b>>, &'a Expr<'b>, Option<&'a Expr<'b>>)>,
6567
{
6668
if !span_contains_comment(cx.sess().source_map(), expr.span)
67-
&& iter.len() >= 2
69+
&& arms.len() >= 2
6870
&& cx.typeck_results().expr_ty(expr).is_bool()
69-
&& let Some((_, last_pat_opt, last_expr, _)) = iter.next_back()
70-
&& let iter_without_last = iter.clone()
71-
&& let Some((first_attrs, _, first_expr, first_guard)) = iter.next()
72-
&& let Some(b0) = find_bool_lit(&first_expr.kind)
73-
&& let Some(b1) = find_bool_lit(&last_expr.kind)
71+
&& let Some((_, last_pat_opt, last_expr, _)) = arms.next_back()
72+
&& let arms_without_last = arms.clone()
73+
&& let Some((first_attrs, _, first_expr, first_guard)) = arms.next()
74+
&& let Some(b0) = find_bool_lit(first_expr)
75+
&& let Some(b1) = find_bool_lit(last_expr)
7476
&& b0 != b1
75-
&& (first_guard.is_none() || iter.len() == 0)
77+
&& (first_guard.is_none() || arms.len() == 0)
7678
&& first_attrs.is_empty()
77-
&& iter.all(|arm| find_bool_lit(&arm.2.kind).is_some_and(|b| b == b0) && arm.3.is_none() && arm.0.is_empty())
79+
&& arms.all(|(attrs, _, expr, guard)| attrs.is_empty() && guard.is_none() && find_bool_lit(expr) == Some(b0))
7880
{
7981
if let Some(last_pat) = last_pat_opt
8082
&& !is_wild(last_pat)
8183
{
8284
return false;
8385
}
8486

85-
for arm in iter_without_last.clone() {
87+
for arm in arms_without_last.clone() {
8688
if let Some(pat) = arm.1
8789
&& !is_lint_allowed(cx, REDUNDANT_PATTERN_MATCHING, pat.hir_id)
88-
&& is_some(pat.kind)
90+
&& is_some_wild(pat.kind)
8991
{
9092
return false;
9193
}
@@ -96,7 +98,7 @@ where
9698
let mut applicability = Applicability::MaybeIncorrect;
9799
let pat = {
98100
use itertools::Itertools as _;
99-
iter_without_last
101+
arms_without_last
100102
.filter_map(|arm| {
101103
let pat_span = arm.1?.span;
102104
Some(snippet_with_applicability(cx, pat_span, "..", &mut applicability))
@@ -142,11 +144,11 @@ where
142144
}
143145

144146
/// Extract a `bool` or `{ bool }`
145-
fn find_bool_lit(ex: &ExprKind<'_>) -> Option<bool> {
146-
match ex {
147+
fn find_bool_lit(ex: &Expr<'_>) -> Option<bool> {
148+
match ex.kind {
147149
ExprKind::Lit(Spanned {
148150
node: LitKind::Bool(b), ..
149-
}) => Some(*b),
151+
}) => Some(b),
150152
ExprKind::Block(
151153
rustc_hir::Block {
152154
stmts: [],
@@ -168,8 +170,9 @@ fn find_bool_lit(ex: &ExprKind<'_>) -> Option<bool> {
168170
}
169171
}
170172

171-
fn is_some(path_kind: PatKind<'_>) -> bool {
172-
match path_kind {
173+
/// Checks whether a pattern is `Some(_)`
174+
fn is_some_wild(pat_kind: PatKind<'_>) -> bool {
175+
match pat_kind {
173176
PatKind::TupleStruct(QPath::Resolved(_, path), [first, ..], _) if is_wild(first) => {
174177
let name = path.segments[0].ident;
175178
name.name == rustc_span::sym::Some

tests/ui/match_expr_like_matches_macro.fixed renamed to tests/ui/match_like_matches_macro.fixed

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#![warn(clippy::match_like_matches_macro)]
22
#![allow(
33
unreachable_patterns,
4-
dead_code,
54
clippy::equatable_if_let,
65
clippy::needless_borrowed_reference,
76
clippy::redundant_guards
@@ -14,11 +13,11 @@ fn main() {
1413
let _y = matches!(x, Some(0));
1514
//~^^^^ match_like_matches_macro
1615

17-
// Lint
16+
// No lint: covered by `redundant_pattern_matching`
1817
let _w = x.is_some();
1918
//~^^^^ redundant_pattern_matching
2019

21-
// Turn into is_none
20+
// No lint: covered by `redundant_pattern_matching`
2221
let _z = x.is_none();
2322
//~^^^^ redundant_pattern_matching
2423

tests/ui/match_expr_like_matches_macro.rs renamed to tests/ui/match_like_matches_macro.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#![warn(clippy::match_like_matches_macro)]
22
#![allow(
33
unreachable_patterns,
4-
dead_code,
54
clippy::equatable_if_let,
65
clippy::needless_borrowed_reference,
76
clippy::redundant_guards
@@ -17,14 +16,14 @@ fn main() {
1716
};
1817
//~^^^^ match_like_matches_macro
1918

20-
// Lint
19+
// No lint: covered by `redundant_pattern_matching`
2120
let _w = match x {
2221
Some(_) => true,
2322
_ => false,
2423
};
2524
//~^^^^ redundant_pattern_matching
2625

27-
// Turn into is_none
26+
// No lint: covered by `redundant_pattern_matching`
2827
let _z = match x {
2928
Some(_) => false,
3029
None => true,

tests/ui/match_expr_like_matches_macro.stderr renamed to tests/ui/match_like_matches_macro.stderr

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: match expression looks like `matches!` macro
2-
--> tests/ui/match_expr_like_matches_macro.rs:14:14
2+
--> tests/ui/match_like_matches_macro.rs:13:14
33
|
44
LL | let _y = match x {
55
| ______________^
@@ -12,7 +12,7 @@ LL | | };
1212
= help: to override `-D warnings` add `#[allow(clippy::match_like_matches_macro)]`
1313

1414
error: redundant pattern matching, consider using `is_some()`
15-
--> tests/ui/match_expr_like_matches_macro.rs:21:14
15+
--> tests/ui/match_like_matches_macro.rs:20:14
1616
|
1717
LL | let _w = match x {
1818
| ______________^
@@ -25,7 +25,7 @@ LL | | };
2525
= help: to override `-D warnings` add `#[allow(clippy::redundant_pattern_matching)]`
2626

2727
error: redundant pattern matching, consider using `is_none()`
28-
--> tests/ui/match_expr_like_matches_macro.rs:28:14
28+
--> tests/ui/match_like_matches_macro.rs:27:14
2929
|
3030
LL | let _z = match x {
3131
| ______________^
@@ -35,7 +35,7 @@ LL | | };
3535
| |_____^ help: try: `x.is_none()`
3636

3737
error: match expression looks like `matches!` macro
38-
--> tests/ui/match_expr_like_matches_macro.rs:35:15
38+
--> tests/ui/match_like_matches_macro.rs:34:15
3939
|
4040
LL | let _zz = match x {
4141
| _______________^
@@ -45,13 +45,13 @@ LL | | };
4545
| |_____^ help: try: `!matches!(x, Some(r) if r == 0)`
4646

4747
error: if let .. else expression looks like `matches!` macro
48-
--> tests/ui/match_expr_like_matches_macro.rs:42:16
48+
--> tests/ui/match_like_matches_macro.rs:41:16
4949
|
5050
LL | let _zzz = if let Some(5) = x { true } else { false };
5151
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `matches!(x, Some(5))`
5252

5353
error: match expression looks like `matches!` macro
54-
--> tests/ui/match_expr_like_matches_macro.rs:67:20
54+
--> tests/ui/match_like_matches_macro.rs:66:20
5555
|
5656
LL | let _ans = match x {
5757
| ____________________^
@@ -62,7 +62,7 @@ LL | | };
6262
| |_________^ help: try: `matches!(x, E::A(_) | E::B(_))`
6363

6464
error: match expression looks like `matches!` macro
65-
--> tests/ui/match_expr_like_matches_macro.rs:78:20
65+
--> tests/ui/match_like_matches_macro.rs:77:20
6666
|
6767
LL | let _ans = match x {
6868
| ____________________^
@@ -74,7 +74,7 @@ LL | | };
7474
| |_________^ help: try: `matches!(x, E::A(_) | E::B(_))`
7575

7676
error: match expression looks like `matches!` macro
77-
--> tests/ui/match_expr_like_matches_macro.rs:89:20
77+
--> tests/ui/match_like_matches_macro.rs:88:20
7878
|
7979
LL | let _ans = match x {
8080
| ____________________^
@@ -85,7 +85,7 @@ LL | | };
8585
| |_________^ help: try: `!matches!(x, E::B(_) | E::C)`
8686

8787
error: match expression looks like `matches!` macro
88-
--> tests/ui/match_expr_like_matches_macro.rs:150:18
88+
--> tests/ui/match_like_matches_macro.rs:149:18
8989
|
9090
LL | let _z = match &z {
9191
| __________________^
@@ -95,7 +95,7 @@ LL | | };
9595
| |_________^ help: try: `matches!(z, Some(3))`
9696

9797
error: match expression looks like `matches!` macro
98-
--> tests/ui/match_expr_like_matches_macro.rs:160:18
98+
--> tests/ui/match_like_matches_macro.rs:159:18
9999
|
100100
LL | let _z = match &z {
101101
| __________________^
@@ -105,7 +105,7 @@ LL | | };
105105
| |_________^ help: try: `matches!(&z, Some(3))`
106106

107107
error: match expression looks like `matches!` macro
108-
--> tests/ui/match_expr_like_matches_macro.rs:178:21
108+
--> tests/ui/match_like_matches_macro.rs:177:21
109109
|
110110
LL | let _ = match &z {
111111
| _____________________^
@@ -115,7 +115,7 @@ LL | | };
115115
| |_____________^ help: try: `matches!(&z, AnEnum::X)`
116116

117117
error: match expression looks like `matches!` macro
118-
--> tests/ui/match_expr_like_matches_macro.rs:193:20
118+
--> tests/ui/match_like_matches_macro.rs:192:20
119119
|
120120
LL | let _res = match &val {
121121
| ____________________^
@@ -125,7 +125,7 @@ LL | | };
125125
| |_________^ help: try: `matches!(&val, &Some(ref _a))`
126126

127127
error: match expression looks like `matches!` macro
128-
--> tests/ui/match_expr_like_matches_macro.rs:206:20
128+
--> tests/ui/match_like_matches_macro.rs:205:20
129129
|
130130
LL | let _res = match &val {
131131
| ____________________^
@@ -135,7 +135,7 @@ LL | | };
135135
| |_________^ help: try: `matches!(&val, &Some(ref _a))`
136136

137137
error: match expression looks like `matches!` macro
138-
--> tests/ui/match_expr_like_matches_macro.rs:265:14
138+
--> tests/ui/match_like_matches_macro.rs:264:14
139139
|
140140
LL | let _y = match Some(5) {
141141
| ______________^

0 commit comments

Comments
 (0)