1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
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.
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;
19 /// **What it does:** Lint for redundant pattern matching over `Result` or
22 /// **Why is this bad?** It's more concise and clear to just use the proper
25 /// **Known problems:** None.
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) {
40 /// The more idiomatic use would be:
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();
50 declare_clippy_lint! {
51 pub REDUNDANT_PATTERN_MATCHING,
53 "use the proper utility function avoiding an `if let`"
56 #[derive(Copy, Clone)]
59 impl LintPass for Pass {
60 fn get_lints(&self) -> LintArray {
61 lint_array!(REDUNDANT_PATTERN_MATCHING)
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 {
69 MatchSource::Normal => find_sugg_for_match(cx, expr, op, arms),
70 MatchSource::IfLetDesugar { .. } => find_sugg_for_if_let(cx, expr, op, arms),
77 fn find_sugg_for_if_let<'a, 'tcx>(
78 cx: &LateContext<'a, 'tcx>,
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) {
89 } else if match_qpath(path, &paths::RESULT_ERR) {
91 } else if match_qpath(path, &paths::OPTION_SOME) {
101 PatKind::Path(ref path) if match_qpath(path, &paths::OPTION_NONE) => "is_none()",
108 REDUNDANT_PATTERN_MATCHING,
109 arms[0].pats[0].span,
110 &format!("redundant pattern matching, consider using `{}`", good_method),
112 let span = expr.span.to(op.span);
113 db.span_suggestion_with_applicability(
116 format!("if {}.{}", snippet(cx, op.span, "_"), good_method),
117 Applicability::MachineApplicable, // snippet
126 fn find_sugg_for_match<'a, 'tcx>(
127 cx: &LateContext<'a, 'tcx>,
133 let node_pair = (&arms[0].pats[0].node, &arms[1].pats[0].node);
135 let found_good_method = match node_pair {
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(
155 PatKind::TupleStruct(ref path_left, ref patterns, _),
156 PatKind::Path(ref path_right)
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(
178 if let Some(good_method) = found_good_method {
181 REDUNDANT_PATTERN_MATCHING,
183 &format!("redundant pattern matching, consider using `{}`", good_method),
185 let span = expr.span.to(op.span);
186 db.span_suggestion_with_applicability(
189 format!("{}.{}", snippet(cx, op.span, "_"), good_method),
190 Applicability::MachineApplicable, // snippet
200 fn find_good_method_for_match<'a>(
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)
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),