]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/redundant_pattern_matching.rs
Auto merge of #4809 - iankronquist:patch-1, r=flip1995
[rust.git] / clippy_lints / src / redundant_pattern_matching.rs
1 use crate::utils::{match_qpath, paths, snippet, span_lint_and_then};
2 use rustc_errors::Applicability;
3 use rustc_hir::*;
4 use rustc_lint::{LateContext, LateLintPass};
5 use rustc_session::{declare_lint_pass, declare_tool_lint};
6 use syntax::ast::LitKind;
7
8 declare_clippy_lint! {
9     /// **What it does:** Lint for redundant pattern matching over `Result` or
10     /// `Option`
11     ///
12     /// **Why is this bad?** It's more concise and clear to just use the proper
13     /// utility function
14     ///
15     /// **Known problems:** None.
16     ///
17     /// **Example:**
18     ///
19     /// ```rust
20     /// if let Ok(_) = Ok::<i32, i32>(42) {}
21     /// if let Err(_) = Err::<i32, i32>(42) {}
22     /// if let None = None::<()> {}
23     /// if let Some(_) = Some(42) {}
24     /// match Ok::<i32, i32>(42) {
25     ///     Ok(_) => true,
26     ///     Err(_) => false,
27     /// };
28     /// ```
29     ///
30     /// The more idiomatic use would be:
31     ///
32     /// ```rust
33     /// if Ok::<i32, i32>(42).is_ok() {}
34     /// if Err::<i32, i32>(42).is_err() {}
35     /// if None::<()>.is_none() {}
36     /// if Some(42).is_some() {}
37     /// Ok::<i32, i32>(42).is_ok();
38     /// ```
39     pub REDUNDANT_PATTERN_MATCHING,
40     style,
41     "use the proper utility function avoiding an `if let`"
42 }
43
44 declare_lint_pass!(RedundantPatternMatching => [REDUNDANT_PATTERN_MATCHING]);
45
46 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantPatternMatching {
47     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
48         if let ExprKind::Match(op, arms, ref match_source) = &expr.kind {
49             match match_source {
50                 MatchSource::Normal => find_sugg_for_match(cx, expr, op, arms),
51                 MatchSource::IfLetDesugar { contains_else_clause } => {
52                     find_sugg_for_if_let(cx, expr, op, arms, *contains_else_clause)
53                 },
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     has_else: bool,
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     let maybe_semi = if has_else { "" } else { ";" };
90
91     span_lint_and_then(
92         cx,
93         REDUNDANT_PATTERN_MATCHING,
94         arms[0].pat.span,
95         &format!("redundant pattern matching, consider using `{}`", good_method),
96         |db| {
97             let span = expr.span.to(op.span);
98             db.span_suggestion(
99                 span,
100                 "try this",
101                 format!("{}.{}{}", snippet(cx, op.span, "_"), good_method, maybe_semi),
102                 Applicability::MaybeIncorrect, // snippet
103             );
104         },
105     );
106 }
107
108 fn find_sugg_for_match<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>, op: &Expr<'_>, arms: &[Arm<'_>]) {
109     if arms.len() == 2 {
110         let node_pair = (&arms[0].pat.kind, &arms[1].pat.kind);
111
112         let found_good_method = match node_pair {
113             (
114                 PatKind::TupleStruct(ref path_left, ref patterns_left, _),
115                 PatKind::TupleStruct(ref path_right, ref patterns_right, _),
116             ) if patterns_left.len() == 1 && patterns_right.len() == 1 => {
117                 if let (PatKind::Wild, PatKind::Wild) = (&patterns_left[0].kind, &patterns_right[0].kind) {
118                     find_good_method_for_match(
119                         arms,
120                         path_left,
121                         path_right,
122                         &paths::RESULT_OK,
123                         &paths::RESULT_ERR,
124                         "is_ok()",
125                         "is_err()",
126                     )
127                 } else {
128                     None
129                 }
130             },
131             (PatKind::TupleStruct(ref path_left, ref patterns, _), PatKind::Path(ref path_right))
132             | (PatKind::Path(ref path_left), PatKind::TupleStruct(ref path_right, ref patterns, _))
133                 if patterns.len() == 1 =>
134             {
135                 if let PatKind::Wild = patterns[0].kind {
136                     find_good_method_for_match(
137                         arms,
138                         path_left,
139                         path_right,
140                         &paths::OPTION_SOME,
141                         &paths::OPTION_NONE,
142                         "is_some()",
143                         "is_none()",
144                     )
145                 } else {
146                     None
147                 }
148             },
149             _ => None,
150         };
151
152         if let Some(good_method) = found_good_method {
153             span_lint_and_then(
154                 cx,
155                 REDUNDANT_PATTERN_MATCHING,
156                 expr.span,
157                 &format!("redundant pattern matching, consider using `{}`", good_method),
158                 |db| {
159                     let span = expr.span.to(op.span);
160                     db.span_suggestion(
161                         span,
162                         "try this",
163                         format!("{}.{}", snippet(cx, op.span, "_"), good_method),
164                         Applicability::MaybeIncorrect, // snippet
165                     );
166                 },
167             );
168         }
169     }
170 }
171
172 fn find_good_method_for_match<'a>(
173     arms: &[Arm<'_>],
174     path_left: &QPath<'_>,
175     path_right: &QPath<'_>,
176     expected_left: &[&str],
177     expected_right: &[&str],
178     should_be_left: &'a str,
179     should_be_right: &'a str,
180 ) -> Option<&'a str> {
181     let body_node_pair = if match_qpath(path_left, expected_left) && match_qpath(path_right, expected_right) {
182         (&(*arms[0].body).kind, &(*arms[1].body).kind)
183     } else if match_qpath(path_right, expected_left) && match_qpath(path_left, expected_right) {
184         (&(*arms[1].body).kind, &(*arms[0].body).kind)
185     } else {
186         return None;
187     };
188
189     match body_node_pair {
190         (ExprKind::Lit(ref lit_left), ExprKind::Lit(ref lit_right)) => match (&lit_left.node, &lit_right.node) {
191             (LitKind::Bool(true), LitKind::Bool(false)) => Some(should_be_left),
192             (LitKind::Bool(false), LitKind::Bool(true)) => Some(should_be_right),
193             _ => None,
194         },
195         _ => None,
196     }
197 }