]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/redundant_pattern_matching.rs
Auto merge of #4591 - flip1995:rustup, 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::hir::ptr::P;
3 use rustc::hir::*;
4 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5 use rustc::{declare_lint_pass, declare_tool_lint};
6 use rustc_errors::Applicability;
7 use syntax::ast::LitKind;
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(ref op, ref arms, ref match_source) = expr.kind {
50             match match_source {
51                 MatchSource::Normal => find_sugg_for_match(cx, expr, op, arms),
52                 MatchSource::IfLetDesugar { contains_else_clause } => {
53                     find_sugg_for_if_let(cx, expr, op, arms, *contains_else_clause)
54                 },
55                 _ => return,
56             }
57         }
58     }
59 }
60
61 fn find_sugg_for_if_let<'a, 'tcx>(
62     cx: &LateContext<'a, 'tcx>,
63     expr: &'tcx Expr,
64     op: &P<Expr>,
65     arms: &HirVec<Arm>,
66     has_else: bool,
67 ) {
68     let good_method = match arms[0].pat.kind {
69         PatKind::TupleStruct(ref path, ref patterns, _) if patterns.len() == 1 => {
70             if let PatKind::Wild = patterns[0].kind {
71                 if match_qpath(path, &paths::RESULT_OK) {
72                     "is_ok()"
73                 } else if match_qpath(path, &paths::RESULT_ERR) {
74                     "is_err()"
75                 } else if match_qpath(path, &paths::OPTION_SOME) {
76                     "is_some()"
77                 } else {
78                     return;
79                 }
80             } else {
81                 return;
82             }
83         },
84
85         PatKind::Path(ref path) if match_qpath(path, &paths::OPTION_NONE) => "is_none()",
86
87         _ => return,
88     };
89
90     let maybe_semi = if has_else { "" } else { ";" };
91
92     span_lint_and_then(
93         cx,
94         REDUNDANT_PATTERN_MATCHING,
95         arms[0].pat.span,
96         &format!("redundant pattern matching, consider using `{}`", good_method),
97         |db| {
98             let span = expr.span.to(op.span);
99             db.span_suggestion(
100                 span,
101                 "try this",
102                 format!("{}.{}{}", snippet(cx, op.span, "_"), good_method, maybe_semi),
103                 Applicability::MaybeIncorrect, // snippet
104             );
105         },
106     );
107 }
108
109 fn find_sugg_for_match<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, op: &P<Expr>, arms: &HirVec<Arm>) {
110     if arms.len() == 2 {
111         let node_pair = (&arms[0].pat.kind, &arms[1].pat.kind);
112
113         let found_good_method = match node_pair {
114             (
115                 PatKind::TupleStruct(ref path_left, ref patterns_left, _),
116                 PatKind::TupleStruct(ref path_right, ref patterns_right, _),
117             ) if patterns_left.len() == 1 && patterns_right.len() == 1 => {
118                 if let (PatKind::Wild, PatKind::Wild) = (&patterns_left[0].kind, &patterns_right[0].kind) {
119                     find_good_method_for_match(
120                         arms,
121                         path_left,
122                         path_right,
123                         &paths::RESULT_OK,
124                         &paths::RESULT_ERR,
125                         "is_ok()",
126                         "is_err()",
127                     )
128                 } else {
129                     None
130                 }
131             },
132             (PatKind::TupleStruct(ref path_left, ref patterns, _), PatKind::Path(ref path_right))
133             | (PatKind::Path(ref path_left), PatKind::TupleStruct(ref path_right, ref patterns, _))
134                 if patterns.len() == 1 =>
135             {
136                 if let PatKind::Wild = patterns[0].kind {
137                     find_good_method_for_match(
138                         arms,
139                         path_left,
140                         path_right,
141                         &paths::OPTION_SOME,
142                         &paths::OPTION_NONE,
143                         "is_some()",
144                         "is_none()",
145                     )
146                 } else {
147                     None
148                 }
149             },
150             _ => None,
151         };
152
153         if let Some(good_method) = found_good_method {
154             span_lint_and_then(
155                 cx,
156                 REDUNDANT_PATTERN_MATCHING,
157                 expr.span,
158                 &format!("redundant pattern matching, consider using `{}`", good_method),
159                 |db| {
160                     let span = expr.span.to(op.span);
161                     db.span_suggestion(
162                         span,
163                         "try this",
164                         format!("{}.{}", snippet(cx, op.span, "_"), good_method),
165                         Applicability::MaybeIncorrect, // snippet
166                     );
167                 },
168             );
169         }
170     }
171 }
172
173 fn find_good_method_for_match<'a>(
174     arms: &HirVec<Arm>,
175     path_left: &QPath,
176     path_right: &QPath,
177     expected_left: &[&str],
178     expected_right: &[&str],
179     should_be_left: &'a str,
180     should_be_right: &'a str,
181 ) -> Option<&'a str> {
182     let body_node_pair = if match_qpath(path_left, expected_left) && match_qpath(path_right, expected_right) {
183         (&(*arms[0].body).kind, &(*arms[1].body).kind)
184     } else if match_qpath(path_right, expected_left) && match_qpath(path_left, expected_right) {
185         (&(*arms[1].body).kind, &(*arms[0].body).kind)
186     } else {
187         return None;
188     };
189
190     match body_node_pair {
191         (ExprKind::Lit(ref lit_left), ExprKind::Lit(ref lit_right)) => match (&lit_left.node, &lit_right.node) {
192             (LitKind::Bool(true), LitKind::Bool(false)) => Some(should_be_left),
193             (LitKind::Bool(false), LitKind::Bool(true)) => Some(should_be_right),
194             _ => None,
195         },
196         _ => None,
197     }
198 }