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.
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};
18 /// **What it does:** Lint for redundant pattern matching over `Result` or
21 /// **Why is this bad?** It's more concise and clear to just use the proper
24 /// **Known problems:** None.
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) {
39 /// The more idiomatic use would be:
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();
48 declare_clippy_lint! {
49 pub REDUNDANT_PATTERN_MATCHING,
51 "use the proper utility function avoiding an `if let`"
54 #[derive(Copy, Clone)]
57 impl LintPass for Pass {
58 fn get_lints(&self) -> LintArray {
59 lint_array!(REDUNDANT_PATTERN_MATCHING)
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 {
67 MatchSource::Normal => find_sugg_for_match(cx, expr, op, arms),
68 MatchSource::IfLetDesugar { .. } => find_sugg_for_if_let(cx, expr, op, arms),
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) {
82 } else if match_qpath(path, &paths::RESULT_ERR) {
84 } else if match_qpath(path, &paths::OPTION_SOME) {
94 PatKind::Path(ref path) if match_qpath(path, &paths::OPTION_NONE) => "is_none()",
101 REDUNDANT_PATTERN_MATCHING,
102 arms[0].pats[0].span,
103 &format!("redundant pattern matching, consider using `{}`", good_method),
105 let span = expr.span.to(op.span);
106 db.span_suggestion_with_applicability(
109 format!("if {}.{}", snippet(cx, op.span, "_"), good_method),
110 Applicability::MachineApplicable, // snippet
119 fn find_sugg_for_match<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, op: &P<Expr>, arms: &HirVec<Arm>) {
121 let node_pair = (&arms[0].pats[0].node, &arms[1].pats[0].node);
123 let found_good_method = match node_pair {
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(
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 =>
146 if let PatKind::Wild = patterns[0].node {
147 find_good_method_for_match(
163 if let Some(good_method) = found_good_method {
166 REDUNDANT_PATTERN_MATCHING,
168 &format!("redundant pattern matching, consider using `{}`", good_method),
170 let span = expr.span.to(op.span);
171 db.span_suggestion_with_applicability(
174 format!("{}.{}", snippet(cx, op.span, "_"), good_method),
175 Applicability::MachineApplicable, // snippet
185 fn find_good_method_for_match<'a>(
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)
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),