]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/redundant_pattern_matching.rs
Auto merge of #5511 - alex-700:fix-redundant-pattern-matching, r=flip1995
[rust.git] / clippy_lints / src / redundant_pattern_matching.rs
1 use crate::utils::{match_qpath, match_trait_method, paths, snippet, span_lint_and_then};
2 use if_chain::if_chain;
3 use rustc_ast::ast::LitKind;
4 use rustc_errors::Applicability;
5 use rustc_hir::{Arm, Expr, ExprKind, MatchSource, PatKind, QPath};
6 use rustc_lint::{LateContext, LateLintPass};
7 use rustc_session::{declare_lint_pass, declare_tool_lint};
8
9 declare_clippy_lint! {
10     /// **What it does:** Lint for redundant pattern matching over `Result` or
11     /// `Option`
12     ///
13     /// **Why is this bad?** It's more concise and clear to just use the proper
14     /// utility function
15     ///
16     /// **Known problems:** None.
17     ///
18     /// **Example:**
19     ///
20     /// ```rust
21     /// if let Ok(_) = Ok::<i32, i32>(42) {}
22     /// if let Err(_) = Err::<i32, i32>(42) {}
23     /// if let None = None::<()> {}
24     /// if let Some(_) = Some(42) {}
25     /// match Ok::<i32, i32>(42) {
26     ///     Ok(_) => true,
27     ///     Err(_) => false,
28     /// };
29     /// ```
30     ///
31     /// The more idiomatic use would be:
32     ///
33     /// ```rust
34     /// if Ok::<i32, i32>(42).is_ok() {}
35     /// if Err::<i32, i32>(42).is_err() {}
36     /// if None::<()>.is_none() {}
37     /// if Some(42).is_some() {}
38     /// Ok::<i32, i32>(42).is_ok();
39     /// ```
40     pub REDUNDANT_PATTERN_MATCHING,
41     style,
42     "use the proper utility function avoiding an `if let`"
43 }
44
45 declare_lint_pass!(RedundantPatternMatching => [REDUNDANT_PATTERN_MATCHING]);
46
47 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantPatternMatching {
48     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
49         if let ExprKind::Match(op, arms, ref match_source) = &expr.kind {
50             match match_source {
51                 MatchSource::Normal => find_sugg_for_match(cx, expr, op, arms),
52                 MatchSource::IfLetDesugar { .. } => find_sugg_for_if_let(cx, expr, op, arms, "if"),
53                 MatchSource::WhileLetDesugar => find_sugg_for_if_let(cx, expr, op, arms, "while"),
54                 _ => return,
55             }
56         }
57     }
58 }
59
60 fn find_sugg_for_if_let<'a, 'tcx>(
61     cx: &LateContext<'a, 'tcx>,
62     expr: &'tcx Expr<'_>,
63     op: &Expr<'_>,
64     arms: &[Arm<'_>],
65     keyword: &'static str,
66 ) {
67     let good_method = match arms[0].pat.kind {
68         PatKind::TupleStruct(ref path, ref patterns, _) if patterns.len() == 1 => {
69             if let PatKind::Wild = patterns[0].kind {
70                 if match_qpath(path, &paths::RESULT_OK) {
71                     "is_ok()"
72                 } else if match_qpath(path, &paths::RESULT_ERR) {
73                     "is_err()"
74                 } else if match_qpath(path, &paths::OPTION_SOME) {
75                     "is_some()"
76                 } else {
77                     return;
78                 }
79             } else {
80                 return;
81             }
82         },
83
84         PatKind::Path(ref path) if match_qpath(path, &paths::OPTION_NONE) => "is_none()",
85
86         _ => return,
87     };
88
89     // check that `while_let_on_iterator` lint does not trigger
90     if_chain! {
91         if keyword == "while";
92         if let ExprKind::MethodCall(method_path, _, _) = op.kind;
93         if method_path.ident.name == sym!(next);
94         if match_trait_method(cx, op, &paths::ITERATOR);
95         then {
96             return;
97         }
98     }
99
100     span_lint_and_then(
101         cx,
102         REDUNDANT_PATTERN_MATCHING,
103         arms[0].pat.span,
104         &format!("redundant pattern matching, consider using `{}`", good_method),
105         |diag| {
106             // while let ... = ... { ... }
107             // ^^^^^^^^^^^^^^^^^^^^^^^^^^^
108             let expr_span = expr.span;
109
110             // while let ... = ... { ... }
111             //                 ^^^
112             let op_span = op.span.source_callsite();
113
114             // while let ... = ... { ... }
115             // ^^^^^^^^^^^^^^^^^^^
116             let span = expr_span.until(op_span.shrink_to_hi());
117             diag.span_suggestion(
118                 span,
119                 "try this",
120                 format!("{} {}.{}", keyword, snippet(cx, op_span, "_"), good_method),
121                 Applicability::MachineApplicable, // snippet
122             );
123         },
124     );
125 }
126
127 fn find_sugg_for_match<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>, op: &Expr<'_>, arms: &[Arm<'_>]) {
128     if arms.len() == 2 {
129         let node_pair = (&arms[0].pat.kind, &arms[1].pat.kind);
130
131         let found_good_method = match node_pair {
132             (
133                 PatKind::TupleStruct(ref path_left, ref patterns_left, _),
134                 PatKind::TupleStruct(ref path_right, ref patterns_right, _),
135             ) if patterns_left.len() == 1 && patterns_right.len() == 1 => {
136                 if let (PatKind::Wild, PatKind::Wild) = (&patterns_left[0].kind, &patterns_right[0].kind) {
137                     find_good_method_for_match(
138                         arms,
139                         path_left,
140                         path_right,
141                         &paths::RESULT_OK,
142                         &paths::RESULT_ERR,
143                         "is_ok()",
144                         "is_err()",
145                     )
146                 } else {
147                     None
148                 }
149             },
150             (PatKind::TupleStruct(ref path_left, ref patterns, _), PatKind::Path(ref path_right))
151             | (PatKind::Path(ref path_left), PatKind::TupleStruct(ref path_right, ref patterns, _))
152                 if patterns.len() == 1 =>
153             {
154                 if let PatKind::Wild = patterns[0].kind {
155                     find_good_method_for_match(
156                         arms,
157                         path_left,
158                         path_right,
159                         &paths::OPTION_SOME,
160                         &paths::OPTION_NONE,
161                         "is_some()",
162                         "is_none()",
163                     )
164                 } else {
165                     None
166                 }
167             },
168             _ => None,
169         };
170
171         if let Some(good_method) = found_good_method {
172             span_lint_and_then(
173                 cx,
174                 REDUNDANT_PATTERN_MATCHING,
175                 expr.span,
176                 &format!("redundant pattern matching, consider using `{}`", good_method),
177                 |diag| {
178                     let span = expr.span.to(op.span);
179                     diag.span_suggestion(
180                         span,
181                         "try this",
182                         format!("{}.{}", snippet(cx, op.span, "_"), good_method),
183                         Applicability::MaybeIncorrect, // snippet
184                     );
185                 },
186             );
187         }
188     }
189 }
190
191 fn find_good_method_for_match<'a>(
192     arms: &[Arm<'_>],
193     path_left: &QPath<'_>,
194     path_right: &QPath<'_>,
195     expected_left: &[&str],
196     expected_right: &[&str],
197     should_be_left: &'a str,
198     should_be_right: &'a str,
199 ) -> Option<&'a str> {
200     let body_node_pair = if match_qpath(path_left, expected_left) && match_qpath(path_right, expected_right) {
201         (&(*arms[0].body).kind, &(*arms[1].body).kind)
202     } else if match_qpath(path_right, expected_left) && match_qpath(path_left, expected_right) {
203         (&(*arms[1].body).kind, &(*arms[0].body).kind)
204     } else {
205         return None;
206     };
207
208     match body_node_pair {
209         (ExprKind::Lit(ref lit_left), ExprKind::Lit(ref lit_right)) => match (&lit_left.node, &lit_right.node) {
210             (LitKind::Bool(true), LitKind::Bool(false)) => Some(should_be_left),
211             (LitKind::Bool(false), LitKind::Bool(true)) => Some(should_be_right),
212             _ => None,
213         },
214         _ => None,
215     }
216 }