Skip to content

Commit 2b5f831

Browse files
committed
clean-up
1 parent 0029a91 commit 2b5f831

File tree

4 files changed

+54
-66
lines changed

4 files changed

+54
-66
lines changed

clippy_lints/src/equatable_if_let.rs

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::span_lint_and_sugg;
1+
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::source::snippet_with_context;
33
use clippy_utils::ty::implements_trait;
44
use rustc_errors::Applicability;
@@ -40,9 +40,9 @@ declare_clippy_lint! {
4040
declare_lint_pass!(PatternEquality => [EQUATABLE_IF_LET]);
4141

4242
/// detects if pattern matches just one thing
43-
fn unary_pattern(pat: &Pat<'_>) -> bool {
43+
fn is_unary_pattern(pat: &Pat<'_>) -> bool {
4444
fn array_rec(pats: &[Pat<'_>]) -> bool {
45-
pats.iter().all(unary_pattern)
45+
pats.iter().all(is_unary_pattern)
4646
}
4747
match &pat.kind {
4848
PatKind::Missing => unreachable!(),
@@ -53,9 +53,9 @@ fn unary_pattern(pat: &Pat<'_>) -> bool {
5353
| PatKind::Never
5454
| PatKind::Or(_)
5555
| PatKind::Err(_) => false,
56-
PatKind::Struct(_, a, etc) => etc.is_none() && a.iter().all(|x| unary_pattern(x.pat)),
56+
PatKind::Struct(_, a, etc) => etc.is_none() && a.iter().all(|x| is_unary_pattern(x.pat)),
5757
PatKind::Tuple(a, etc) | PatKind::TupleStruct(_, a, etc) => etc.as_opt_usize().is_none() && array_rec(a),
58-
PatKind::Ref(x, _) | PatKind::Box(x) | PatKind::Deref(x) | PatKind::Guard(x, _) => unary_pattern(x),
58+
PatKind::Ref(x, _) | PatKind::Box(x) | PatKind::Deref(x) | PatKind::Guard(x, _) => is_unary_pattern(x),
5959
PatKind::Expr(_) => true,
6060
}
6161
}
@@ -103,48 +103,52 @@ fn contains_type_mismatch(cx: &LateContext<'_>, pat: &Pat<'_>) -> bool {
103103
impl<'tcx> LateLintPass<'tcx> for PatternEquality {
104104
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
105105
if let ExprKind::Let(let_expr) = expr.kind
106-
&& unary_pattern(let_expr.pat)
106+
&& is_unary_pattern(let_expr.pat)
107107
&& !expr.span.in_external_macro(cx.sess().source_map())
108108
{
109109
let exp_ty = cx.typeck_results().expr_ty(let_expr.init);
110110
let pat_ty = cx.typeck_results().pat_ty(let_expr.pat);
111-
let mut applicability = Applicability::MachineApplicable;
111+
112+
let mut app = Applicability::MachineApplicable;
113+
let ctxt = expr.span.ctxt();
112114

113115
if is_structural_partial_eq(cx, exp_ty, pat_ty) && !contains_type_mismatch(cx, let_expr.pat) {
114-
let pat_str = match let_expr.pat.kind {
115-
PatKind::Struct(..) => format!(
116-
"({})",
117-
snippet_with_context(cx, let_expr.pat.span, expr.span.ctxt(), "..", &mut applicability).0,
118-
),
119-
_ => snippet_with_context(cx, let_expr.pat.span, expr.span.ctxt(), "..", &mut applicability)
120-
.0
121-
.to_string(),
122-
};
123-
span_lint_and_sugg(
116+
span_lint_and_then(
124117
cx,
125118
EQUATABLE_IF_LET,
126119
expr.span,
127120
"this pattern matching can be expressed using equality",
128-
"try",
129-
format!(
130-
"{} == {pat_str}",
131-
snippet_with_context(cx, let_expr.init.span, expr.span.ctxt(), "..", &mut applicability).0,
132-
),
133-
applicability,
121+
|diag| {
122+
let pat_str = {
123+
let str = snippet_with_context(cx, let_expr.pat.span, ctxt, "..", &mut app).0;
124+
if let PatKind::Struct(..) = let_expr.pat.kind {
125+
format!("({str})").into()
126+
} else {
127+
str
128+
}
129+
};
130+
131+
let sugg = format!(
132+
"{} == {pat_str}",
133+
snippet_with_context(cx, let_expr.init.span, ctxt, "..", &mut app).0,
134+
);
135+
diag.span_suggestion(expr.span, "try", sugg, app);
136+
},
134137
);
135138
} else {
136-
span_lint_and_sugg(
139+
span_lint_and_then(
137140
cx,
138141
EQUATABLE_IF_LET,
139142
expr.span,
140143
"this pattern matching can be expressed using `matches!`",
141-
"try",
142-
format!(
143-
"matches!({}, {})",
144-
snippet_with_context(cx, let_expr.init.span, expr.span.ctxt(), "..", &mut applicability).0,
145-
snippet_with_context(cx, let_expr.pat.span, expr.span.ctxt(), "..", &mut applicability).0,
146-
),
147-
applicability,
144+
|diag| {
145+
let sugg = format!(
146+
"matches!({}, {})",
147+
snippet_with_context(cx, let_expr.init.span, ctxt, "..", &mut app).0,
148+
snippet_with_context(cx, let_expr.pat.span, ctxt, "..", &mut app).0,
149+
);
150+
diag.span_suggestion(expr.span, "try", sugg, app);
151+
},
148152
);
149153
}
150154
}

tests/ui/equatable_if_let.fixed

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
//@aux-build:proc_macros.rs
2-
3-
#![allow(
4-
unused_variables,
5-
dead_code,
6-
clippy::derive_partial_eq_without_eq,
7-
clippy::needless_ifs
8-
)]
2+
#![allow(clippy::derive_partial_eq_without_eq, clippy::needless_ifs)]
93
#![warn(clippy::equatable_if_let)]
10-
11-
extern crate proc_macros;
124
use proc_macros::{external, inline_macros};
135

146
use std::cmp::Ordering;

tests/ui/equatable_if_let.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
//@aux-build:proc_macros.rs
2-
3-
#![allow(
4-
unused_variables,
5-
dead_code,
6-
clippy::derive_partial_eq_without_eq,
7-
clippy::needless_ifs
8-
)]
2+
#![allow(clippy::derive_partial_eq_without_eq, clippy::needless_ifs)]
93
#![warn(clippy::equatable_if_let)]
10-
11-
extern crate proc_macros;
124
use proc_macros::{external, inline_macros};
135

146
use std::cmp::Ordering;

tests/ui/equatable_if_let.stderr

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this pattern matching can be expressed using equality
2-
--> tests/ui/equatable_if_let.rs:64:8
2+
--> tests/ui/equatable_if_let.rs:56:8
33
|
44
LL | if let 2 = a {}
55
| ^^^^^^^^^ help: try: `a == 2`
@@ -8,97 +8,97 @@ LL | if let 2 = a {}
88
= help: to override `-D warnings` add `#[allow(clippy::equatable_if_let)]`
99

1010
error: this pattern matching can be expressed using equality
11-
--> tests/ui/equatable_if_let.rs:66:8
11+
--> tests/ui/equatable_if_let.rs:58:8
1212
|
1313
LL | if let Ordering::Greater = a.cmp(&b) {}
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `a.cmp(&b) == Ordering::Greater`
1515

1616
error: this pattern matching can be expressed using equality
17-
--> tests/ui/equatable_if_let.rs:68:8
17+
--> tests/ui/equatable_if_let.rs:60:8
1818
|
1919
LL | if let Some(2) = c {}
2020
| ^^^^^^^^^^^^^^^ help: try: `c == Some(2)`
2121

2222
error: this pattern matching can be expressed using equality
23-
--> tests/ui/equatable_if_let.rs:70:8
23+
--> tests/ui/equatable_if_let.rs:62:8
2424
|
2525
LL | if let Struct { a: 2, b: false } = d {}
2626
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `d == (Struct { a: 2, b: false })`
2727

2828
error: this pattern matching can be expressed using equality
29-
--> tests/ui/equatable_if_let.rs:72:8
29+
--> tests/ui/equatable_if_let.rs:64:8
3030
|
3131
LL | if let Enum::TupleVariant(32, 64) = e {}
3232
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `e == Enum::TupleVariant(32, 64)`
3333

3434
error: this pattern matching can be expressed using equality
35-
--> tests/ui/equatable_if_let.rs:74:8
35+
--> tests/ui/equatable_if_let.rs:66:8
3636
|
3737
LL | if let Enum::RecordVariant { a: 64, b: 32 } = e {}
3838
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `e == (Enum::RecordVariant { a: 64, b: 32 })`
3939

4040
error: this pattern matching can be expressed using equality
41-
--> tests/ui/equatable_if_let.rs:76:8
41+
--> tests/ui/equatable_if_let.rs:68:8
4242
|
4343
LL | if let Enum::UnitVariant = e {}
4444
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `e == Enum::UnitVariant`
4545

4646
error: this pattern matching can be expressed using equality
47-
--> tests/ui/equatable_if_let.rs:78:8
47+
--> tests/ui/equatable_if_let.rs:70:8
4848
|
4949
LL | if let (Enum::UnitVariant, &Struct { a: 2, b: false }) = (e, &d) {}
5050
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `(e, &d) == (Enum::UnitVariant, &Struct { a: 2, b: false })`
5151

5252
error: this pattern matching can be expressed using `matches!`
53-
--> tests/ui/equatable_if_let.rs:88:8
53+
--> tests/ui/equatable_if_let.rs:80:8
5454
|
5555
LL | if let NotPartialEq::A = f {}
5656
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `matches!(f, NotPartialEq::A)`
5757

5858
error: this pattern matching can be expressed using equality
59-
--> tests/ui/equatable_if_let.rs:90:8
59+
--> tests/ui/equatable_if_let.rs:82:8
6060
|
6161
LL | if let NotStructuralEq::A = g {}
6262
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `g == NotStructuralEq::A`
6363

6464
error: this pattern matching can be expressed using `matches!`
65-
--> tests/ui/equatable_if_let.rs:92:8
65+
--> tests/ui/equatable_if_let.rs:84:8
6666
|
6767
LL | if let Some(NotPartialEq::A) = Some(f) {}
6868
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `matches!(Some(f), Some(NotPartialEq::A))`
6969

7070
error: this pattern matching can be expressed using equality
71-
--> tests/ui/equatable_if_let.rs:94:8
71+
--> tests/ui/equatable_if_let.rs:86:8
7272
|
7373
LL | if let Some(NotStructuralEq::A) = Some(g) {}
7474
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Some(g) == Some(NotStructuralEq::A)`
7575

7676
error: this pattern matching can be expressed using `matches!`
77-
--> tests/ui/equatable_if_let.rs:96:8
77+
--> tests/ui/equatable_if_let.rs:88:8
7878
|
7979
LL | if let NoPartialEqStruct { a: 2, b: false } = h {}
8080
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `matches!(h, NoPartialEqStruct { a: 2, b: false })`
8181

8282
error: this pattern matching can be expressed using equality
83-
--> tests/ui/equatable_if_let.rs:99:8
83+
--> tests/ui/equatable_if_let.rs:91:8
8484
|
8585
LL | if let inline!("abc") = "abc" {
8686
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `"abc" == inline!("abc")`
8787

8888
error: this pattern matching can be expressed using `matches!`
89-
--> tests/ui/equatable_if_let.rs:109:12
89+
--> tests/ui/equatable_if_let.rs:101:12
9090
|
9191
LL | if let Some('i') = cs.iter().next() {
9292
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `matches!(cs.iter().next(), Some('i'))`
9393

9494
error: this pattern matching can be expressed using `matches!`
95-
--> tests/ui/equatable_if_let.rs:117:12
95+
--> tests/ui/equatable_if_let.rs:109:12
9696
|
9797
LL | if let Some(1) = cs.iter().next() {
9898
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `matches!(cs.iter().next(), Some(1))`
9999

100100
error: this pattern matching can be expressed using `matches!`
101-
--> tests/ui/equatable_if_let.rs:135:12
101+
--> tests/ui/equatable_if_let.rs:127:12
102102
|
103103
LL | if let Some(MyEnum::B) = get_enum() {
104104
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `matches!(get_enum(), Some(MyEnum::B))`

0 commit comments

Comments
 (0)