]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/redundant_pattern_matching.rs
Auto merge of #3511 - phansch:remove_allow_doc_markdown, r=phansch
[rust.git] / clippy_lints / src / redundant_pattern_matching.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 use crate::rustc::hir::*;
11 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
12 use crate::rustc::{declare_tool_lint, lint_array};
13 use crate::rustc_errors::Applicability;
14 use crate::syntax::ast::LitKind;
15 use crate::syntax::ptr::P;
16 use crate::utils::{match_qpath, paths, snippet, span_lint_and_then};
17
18 /// **What it does:** Lint for redundant pattern matching over `Result` or
19 /// `Option`
20 ///
21 /// **Why is this bad?** It's more concise and clear to just use the proper
22 /// utility function
23 ///
24 /// **Known problems:** None.
25 ///
26 /// **Example:**
27 ///
28 /// ```rust
29 /// if let Ok(_) = Ok::<i32, i32>(42) {}
30 /// if let Err(_) = Err::<i32, i32>(42) {}
31 /// if let None = None::<()> {}
32 /// if let Some(_) = Some(42) {}
33 /// match Ok::<i32, i32>(42) {
34 ///     Ok(_) => true,
35 ///     Err(_) => false,
36 /// };
37 /// ```
38 ///
39 /// The more idiomatic use would be:
40 ///
41 /// ```rust
42 /// if Ok::<i32, i32>(42).is_ok() {}
43 /// if Err::<i32, i32>(42).is_err() {}
44 /// if None::<()>.is_none() {}
45 /// if Some(42).is_some() {}
46 /// Ok::<i32, i32>(42).is_ok();
47 /// ```
48 declare_clippy_lint! {
49     pub REDUNDANT_PATTERN_MATCHING,
50     style,
51     "use the proper utility function avoiding an `if let`"
52 }
53
54 #[derive(Copy, Clone)]
55 pub struct Pass;
56
57 impl LintPass for Pass {
58     fn get_lints(&self) -> LintArray {
59         lint_array!(REDUNDANT_PATTERN_MATCHING)
60     }
61 }
62
63 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
64     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
65         if let ExprKind::Match(ref op, ref arms, ref match_source) = expr.node {
66             match match_source {
67                 MatchSource::Normal => find_sugg_for_match(cx, expr, op, arms),
68                 MatchSource::IfLetDesugar { .. } => find_sugg_for_if_let(cx, expr, op, arms),
69                 _ => return,
70             }
71         }
72     }
73 }
74
75 fn find_sugg_for_if_let<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, op: &P<Expr>, arms: &HirVec<Arm>) {
76     if arms[0].pats.len() == 1 {
77         let good_method = match arms[0].pats[0].node {
78             PatKind::TupleStruct(ref path, ref patterns, _) if patterns.len() == 1 => {
79                 if let PatKind::Wild = patterns[0].node {
80                     if match_qpath(path, &paths::RESULT_OK) {
81                         "is_ok()"
82                     } else if match_qpath(path, &paths::RESULT_ERR) {
83                         "is_err()"
84                     } else if match_qpath(path, &paths::OPTION_SOME) {
85                         "is_some()"
86                     } else {
87                         return;
88                     }
89                 } else {
90                     return;
91                 }
92             },
93
94             PatKind::Path(ref path) if match_qpath(path, &paths::OPTION_NONE) => "is_none()",
95
96             _ => return,
97         };
98
99         span_lint_and_then(
100             cx,
101             REDUNDANT_PATTERN_MATCHING,
102             arms[0].pats[0].span,
103             &format!("redundant pattern matching, consider using `{}`", good_method),
104             |db| {
105                 let span = expr.span.to(op.span);
106                 db.span_suggestion_with_applicability(
107                     span,
108                     "try this",
109                     format!("if {}.{}", snippet(cx, op.span, "_"), good_method),
110                     Applicability::MachineApplicable, // snippet
111                 );
112             },
113         );
114     } else {
115         return;
116     }
117 }
118
119 fn find_sugg_for_match<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, op: &P<Expr>, arms: &HirVec<Arm>) {
120     if arms.len() == 2 {
121         let node_pair = (&arms[0].pats[0].node, &arms[1].pats[0].node);
122
123         let found_good_method = match node_pair {
124             (
125                 PatKind::TupleStruct(ref path_left, ref patterns_left, _),
126                 PatKind::TupleStruct(ref path_right, ref patterns_right, _),
127             ) if patterns_left.len() == 1 && patterns_right.len() == 1 => {
128                 if let (PatKind::Wild, PatKind::Wild) = (&patterns_left[0].node, &patterns_right[0].node) {
129                     find_good_method_for_match(
130                         arms,
131                         path_left,
132                         path_right,
133                         &paths::RESULT_OK,
134                         &paths::RESULT_ERR,
135                         "is_ok()",
136                         "is_err()",
137                     )
138                 } else {
139                     None
140                 }
141             },
142             (PatKind::TupleStruct(ref path_left, ref patterns, _), PatKind::Path(ref path_right))
143             | (PatKind::Path(ref path_left), PatKind::TupleStruct(ref path_right, ref patterns, _))
144                 if patterns.len() == 1 =>
145             {
146                 if let PatKind::Wild = patterns[0].node {
147                     find_good_method_for_match(
148                         arms,
149                         path_left,
150                         path_right,
151                         &paths::OPTION_SOME,
152                         &paths::OPTION_NONE,
153                         "is_some()",
154                         "is_none()",
155                     )
156                 } else {
157                     None
158                 }
159             },
160             _ => None,
161         };
162
163         if let Some(good_method) = found_good_method {
164             span_lint_and_then(
165                 cx,
166                 REDUNDANT_PATTERN_MATCHING,
167                 expr.span,
168                 &format!("redundant pattern matching, consider using `{}`", good_method),
169                 |db| {
170                     let span = expr.span.to(op.span);
171                     db.span_suggestion_with_applicability(
172                         span,
173                         "try this",
174                         format!("{}.{}", snippet(cx, op.span, "_"), good_method),
175                         Applicability::MachineApplicable, // snippet
176                     );
177                 },
178             );
179         }
180     } else {
181         return;
182     }
183 }
184
185 fn find_good_method_for_match<'a>(
186     arms: &HirVec<Arm>,
187     path_left: &QPath,
188     path_right: &QPath,
189     expected_left: &[&str],
190     expected_right: &[&str],
191     should_be_left: &'a str,
192     should_be_right: &'a str,
193 ) -> Option<&'a str> {
194     let body_node_pair = if match_qpath(path_left, expected_left) && match_qpath(path_right, expected_right) {
195         (&(*arms[0].body).node, &(*arms[1].body).node)
196     } else if match_qpath(path_right, expected_left) && match_qpath(path_left, expected_right) {
197         (&(*arms[1].body).node, &(*arms[0].body).node)
198     } else {
199         return None;
200     };
201
202     match body_node_pair {
203         (ExprKind::Lit(ref lit_left), ExprKind::Lit(ref lit_right)) => match (&lit_left.node, &lit_right.node) {
204             (LitKind::Bool(true), LitKind::Bool(false)) => Some(should_be_left),
205             (LitKind::Bool(false), LitKind::Bool(true)) => Some(should_be_right),
206             _ => None,
207         },
208         _ => None,
209     }
210 }