1 use crate::utils::{match_qpath, paths, snippet, span_lint_and_then};
2 use rustc::declare_lint_pass;
3 use rustc::hir::ptr::P;
5 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
6 use rustc_errors::Applicability;
7 use rustc_session::declare_tool_lint;
8 use syntax::ast::LitKind;
10 declare_clippy_lint! {
11 /// **What it does:** Lint for redundant pattern matching over `Result` or
14 /// **Why is this bad?** It's more concise and clear to just use the proper
17 /// **Known problems:** None.
22 /// if let Ok(_) = Ok::<i32, i32>(42) {}
23 /// if let Err(_) = Err::<i32, i32>(42) {}
24 /// if let None = None::<()> {}
25 /// if let Some(_) = Some(42) {}
26 /// match Ok::<i32, i32>(42) {
32 /// The more idiomatic use would be:
35 /// if Ok::<i32, i32>(42).is_ok() {}
36 /// if Err::<i32, i32>(42).is_err() {}
37 /// if None::<()>.is_none() {}
38 /// if Some(42).is_some() {}
39 /// Ok::<i32, i32>(42).is_ok();
41 pub REDUNDANT_PATTERN_MATCHING,
43 "use the proper utility function avoiding an `if let`"
46 declare_lint_pass!(RedundantPatternMatching => [REDUNDANT_PATTERN_MATCHING]);
48 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantPatternMatching {
49 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
50 if let ExprKind::Match(ref op, ref arms, ref match_source) = expr.kind {
52 MatchSource::Normal => find_sugg_for_match(cx, expr, op, arms),
53 MatchSource::IfLetDesugar { contains_else_clause } => {
54 find_sugg_for_if_let(cx, expr, op, arms, *contains_else_clause)
62 fn find_sugg_for_if_let<'a, 'tcx>(
63 cx: &LateContext<'a, 'tcx>,
69 let good_method = match arms[0].pat.kind {
70 PatKind::TupleStruct(ref path, ref patterns, _) if patterns.len() == 1 => {
71 if let PatKind::Wild = patterns[0].kind {
72 if match_qpath(path, &paths::RESULT_OK) {
74 } else if match_qpath(path, &paths::RESULT_ERR) {
76 } else if match_qpath(path, &paths::OPTION_SOME) {
86 PatKind::Path(ref path) if match_qpath(path, &paths::OPTION_NONE) => "is_none()",
91 let maybe_semi = if has_else { "" } else { ";" };
95 REDUNDANT_PATTERN_MATCHING,
97 &format!("redundant pattern matching, consider using `{}`", good_method),
99 let span = expr.span.to(op.span);
103 format!("{}.{}{}", snippet(cx, op.span, "_"), good_method, maybe_semi),
104 Applicability::MaybeIncorrect, // snippet
110 fn find_sugg_for_match<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, op: &P<Expr>, arms: &HirVec<Arm>) {
112 let node_pair = (&arms[0].pat.kind, &arms[1].pat.kind);
114 let found_good_method = match node_pair {
116 PatKind::TupleStruct(ref path_left, ref patterns_left, _),
117 PatKind::TupleStruct(ref path_right, ref patterns_right, _),
118 ) if patterns_left.len() == 1 && patterns_right.len() == 1 => {
119 if let (PatKind::Wild, PatKind::Wild) = (&patterns_left[0].kind, &patterns_right[0].kind) {
120 find_good_method_for_match(
133 (PatKind::TupleStruct(ref path_left, ref patterns, _), PatKind::Path(ref path_right))
134 | (PatKind::Path(ref path_left), PatKind::TupleStruct(ref path_right, ref patterns, _))
135 if patterns.len() == 1 =>
137 if let PatKind::Wild = patterns[0].kind {
138 find_good_method_for_match(
154 if let Some(good_method) = found_good_method {
157 REDUNDANT_PATTERN_MATCHING,
159 &format!("redundant pattern matching, consider using `{}`", good_method),
161 let span = expr.span.to(op.span);
165 format!("{}.{}", snippet(cx, op.span, "_"), good_method),
166 Applicability::MaybeIncorrect, // snippet
174 fn find_good_method_for_match<'a>(
178 expected_left: &[&str],
179 expected_right: &[&str],
180 should_be_left: &'a str,
181 should_be_right: &'a str,
182 ) -> Option<&'a str> {
183 let body_node_pair = if match_qpath(path_left, expected_left) && match_qpath(path_right, expected_right) {
184 (&(*arms[0].body).kind, &(*arms[1].body).kind)
185 } else if match_qpath(path_right, expected_left) && match_qpath(path_left, expected_right) {
186 (&(*arms[1].body).kind, &(*arms[0].body).kind)
191 match body_node_pair {
192 (ExprKind::Lit(ref lit_left), ExprKind::Lit(ref lit_right)) => match (&lit_left.node, &lit_right.node) {
193 (LitKind::Bool(true), LitKind::Bool(false)) => Some(should_be_left),
194 (LitKind::Bool(false), LitKind::Bool(true)) => Some(should_be_right),