Skip to content

Commit 2193494

Browse files
committed
clean-up
1 parent abc17b3 commit 2193494

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::is_in_const_context;
33
use clippy_utils::source::snippet_with_context;
44
use clippy_utils::ty::implements_trait;
@@ -41,9 +41,9 @@ declare_clippy_lint! {
4141
declare_lint_pass!(PatternEquality => [EQUATABLE_IF_LET]);
4242

4343
/// detects if pattern matches just one thing
44-
fn unary_pattern(pat: &Pat<'_>) -> bool {
44+
fn is_unary_pattern(pat: &Pat<'_>) -> bool {
4545
fn array_rec(pats: &[Pat<'_>]) -> bool {
46-
pats.iter().all(unary_pattern)
46+
pats.iter().all(is_unary_pattern)
4747
}
4848
match &pat.kind {
4949
PatKind::Missing => unreachable!(),
@@ -54,9 +54,9 @@ fn unary_pattern(pat: &Pat<'_>) -> bool {
5454
| PatKind::Never
5555
| PatKind::Or(_)
5656
| PatKind::Err(_) => false,
57-
PatKind::Struct(_, a, etc) => etc.is_none() && a.iter().all(|x| unary_pattern(x.pat)),
57+
PatKind::Struct(_, a, etc) => etc.is_none() && a.iter().all(|x| is_unary_pattern(x.pat)),
5858
PatKind::Tuple(a, etc) | PatKind::TupleStruct(_, a, etc) => etc.as_opt_usize().is_none() && array_rec(a),
59-
PatKind::Ref(x, _, _) | PatKind::Box(x) | PatKind::Deref(x) | PatKind::Guard(x, _) => unary_pattern(x),
59+
PatKind::Ref(x, _, _) | PatKind::Box(x) | PatKind::Deref(x) | PatKind::Guard(x, _) => is_unary_pattern(x),
6060
PatKind::Expr(_) => true,
6161
}
6262
}
@@ -104,12 +104,14 @@ fn contains_type_mismatch(cx: &LateContext<'_>, pat: &Pat<'_>) -> bool {
104104
impl<'tcx> LateLintPass<'tcx> for PatternEquality {
105105
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
106106
if let ExprKind::Let(let_expr) = expr.kind
107-
&& unary_pattern(let_expr.pat)
107+
&& is_unary_pattern(let_expr.pat)
108108
&& !expr.span.in_external_macro(cx.sess().source_map())
109109
{
110110
let exp_ty = cx.typeck_results().expr_ty(let_expr.init);
111111
let pat_ty = cx.typeck_results().pat_ty(let_expr.pat);
112-
let mut applicability = Applicability::MachineApplicable;
112+
113+
let mut app = Applicability::MachineApplicable;
114+
let ctxt = expr.span.ctxt();
113115

114116
if is_structural_partial_eq(cx, exp_ty, pat_ty)
115117
&& !contains_type_mismatch(cx, let_expr.pat)
@@ -121,40 +123,42 @@ impl<'tcx> LateLintPass<'tcx> for PatternEquality {
121123
// but that didn't quite work out (see #15482), so we just reject outright in this case
122124
&& !is_in_const_context(cx)
123125
{
124-
let pat_str = match let_expr.pat.kind {
125-
PatKind::Struct(..) => format!(
126-
"({})",
127-
snippet_with_context(cx, let_expr.pat.span, expr.span.ctxt(), "..", &mut applicability).0,
128-
),
129-
_ => snippet_with_context(cx, let_expr.pat.span, expr.span.ctxt(), "..", &mut applicability)
130-
.0
131-
.to_string(),
132-
};
133-
span_lint_and_sugg(
126+
span_lint_and_then(
134127
cx,
135128
EQUATABLE_IF_LET,
136129
expr.span,
137130
"this pattern matching can be expressed using equality",
138-
"try",
139-
format!(
140-
"{} == {pat_str}",
141-
snippet_with_context(cx, let_expr.init.span, expr.span.ctxt(), "..", &mut applicability).0,
142-
),
143-
applicability,
131+
|diag| {
132+
let pat_str = {
133+
let str = snippet_with_context(cx, let_expr.pat.span, ctxt, "..", &mut app).0;
134+
if let PatKind::Struct(..) = let_expr.pat.kind {
135+
format!("({str})").into()
136+
} else {
137+
str
138+
}
139+
};
140+
141+
let sugg = format!(
142+
"{} == {pat_str}",
143+
snippet_with_context(cx, let_expr.init.span, ctxt, "..", &mut app).0,
144+
);
145+
diag.span_suggestion(expr.span, "try", sugg, app);
146+
},
144147
);
145148
} else {
146-
span_lint_and_sugg(
149+
span_lint_and_then(
147150
cx,
148151
EQUATABLE_IF_LET,
149152
expr.span,
150153
"this pattern matching can be expressed using `matches!`",
151-
"try",
152-
format!(
153-
"matches!({}, {})",
154-
snippet_with_context(cx, let_expr.init.span, expr.span.ctxt(), "..", &mut applicability).0,
155-
snippet_with_context(cx, let_expr.pat.span, expr.span.ctxt(), "..", &mut applicability).0,
156-
),
157-
applicability,
154+
|diag| {
155+
let sugg = format!(
156+
"matches!({}, {})",
157+
snippet_with_context(cx, let_expr.init.span, ctxt, "..", &mut app).0,
158+
snippet_with_context(cx, let_expr.pat.span, ctxt, "..", &mut app).0,
159+
);
160+
diag.span_suggestion(expr.span, "try", sugg, app);
161+
},
158162
);
159163
}
160164
}

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)