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