|
1 | 1 | use clippy_utils::diagnostics::span_lint_and_sugg;
|
2 | 2 | use clippy_utils::source::snippet_with_context;
|
3 | 3 | use clippy_utils::ty::implements_trait;
|
4 |
| -use if_chain::if_chain; |
5 | 4 | use rustc_errors::Applicability;
|
6 | 5 | use rustc_hir::{Expr, ExprKind, Pat, PatKind};
|
7 | 6 | use rustc_lint::{LateContext, LateLintPass, LintContext};
|
@@ -67,37 +66,33 @@ fn is_structural_partial_eq<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, other: T
|
67 | 66 |
|
68 | 67 | impl<'tcx> LateLintPass<'tcx> for PatternEquality {
|
69 | 68 | fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
|
70 |
| - if_chain! { |
71 |
| - if !in_external_macro(cx.sess(), expr.span); |
72 |
| - if let ExprKind::Let(let_expr) = expr.kind; |
73 |
| - if unary_pattern(let_expr.pat); |
74 |
| - let exp_ty = cx.typeck_results().expr_ty(let_expr.init); |
75 |
| - let pat_ty = cx.typeck_results().pat_ty(let_expr.pat); |
76 |
| - if is_structural_partial_eq(cx, exp_ty, pat_ty); |
77 |
| - then { |
78 |
| - |
79 |
| - let mut applicability = Applicability::MachineApplicable; |
80 |
| - let pat_str = match let_expr.pat.kind { |
81 |
| - PatKind::Struct(..) => format!( |
82 |
| - "({})", |
83 |
| - snippet_with_context(cx, let_expr.pat.span, expr.span.ctxt(), "..", &mut applicability).0, |
84 |
| - ), |
85 |
| - _ => snippet_with_context(cx, let_expr.pat.span, expr.span.ctxt(), "..", &mut applicability).0.to_string(), |
86 |
| - }; |
87 |
| - span_lint_and_sugg( |
88 |
| - cx, |
89 |
| - EQUATABLE_IF_LET, |
90 |
| - expr.span, |
91 |
| - "this pattern matching can be expressed using equality", |
92 |
| - "try", |
93 |
| - format!( |
94 |
| - "{} == {}", |
95 |
| - snippet_with_context(cx, let_expr.init.span, expr.span.ctxt(), "..", &mut applicability).0, |
96 |
| - pat_str, |
97 |
| - ), |
98 |
| - applicability, |
99 |
| - ); |
100 |
| - } |
| 69 | + if !in_external_macro(cx.sess(), expr.span) |
| 70 | + && let ExprKind::Let(let_expr) = expr.kind |
| 71 | + && unary_pattern(let_expr.pat) |
| 72 | + && let exp_ty = cx.typeck_results().expr_ty(let_expr.init) |
| 73 | + && let pat_ty = cx.typeck_results().pat_ty(let_expr.pat) |
| 74 | + && is_structural_partial_eq(cx, exp_ty, pat_ty) { |
| 75 | + let mut applicability = Applicability::MachineApplicable; |
| 76 | + let pat_str = match let_expr.pat.kind { |
| 77 | + PatKind::Struct(..) => format!( |
| 78 | + "({})", |
| 79 | + snippet_with_context(cx, let_expr.pat.span, expr.span.ctxt(), "..", &mut applicability).0, |
| 80 | + ), |
| 81 | + _ => snippet_with_context(cx, let_expr.pat.span, expr.span.ctxt(), "..", &mut applicability).0.to_string(), |
| 82 | + }; |
| 83 | + span_lint_and_sugg( |
| 84 | + cx, |
| 85 | + EQUATABLE_IF_LET, |
| 86 | + expr.span, |
| 87 | + "this pattern matching can be expressed using equality", |
| 88 | + "try", |
| 89 | + format!( |
| 90 | + "{} == {}", |
| 91 | + snippet_with_context(cx, let_expr.init.span, expr.span.ctxt(), "..", &mut applicability).0, |
| 92 | + pat_str, |
| 93 | + ), |
| 94 | + applicability, |
| 95 | + ); |
101 | 96 | }
|
102 | 97 | }
|
103 | 98 | }
|
0 commit comments