1 use crate::utils::{match_qpath, match_trait_method, paths, snippet, span_lint_and_then};
2 use if_chain::if_chain;
3 use rustc_ast::ast::LitKind;
4 use rustc_errors::Applicability;
5 use rustc_hir::{Arm, Expr, ExprKind, MatchSource, PatKind, QPath};
6 use rustc_lint::{LateContext, LateLintPass};
7 use rustc_session::{declare_lint_pass, declare_tool_lint};
10 /// **What it does:** Lint for redundant pattern matching over `Result` or
13 /// **Why is this bad?** It's more concise and clear to just use the proper
16 /// **Known problems:** None.
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) {
31 /// The more idiomatic use would be:
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();
40 pub REDUNDANT_PATTERN_MATCHING,
42 "use the proper utility function avoiding an `if let`"
45 declare_lint_pass!(RedundantPatternMatching => [REDUNDANT_PATTERN_MATCHING]);
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(op, arms, ref match_source) = &expr.kind {
51 MatchSource::Normal => find_sugg_for_match(cx, expr, op, arms),
52 MatchSource::IfLetDesugar { .. } => find_sugg_for_if_let(cx, expr, op, arms, "if"),
53 MatchSource::WhileLetDesugar => find_sugg_for_if_let(cx, expr, op, arms, "while"),
60 fn find_sugg_for_if_let<'a, 'tcx>(
61 cx: &LateContext<'a, 'tcx>,
65 keyword: &'static str,
67 let good_method = match arms[0].pat.kind {
68 PatKind::TupleStruct(ref path, ref patterns, _) if patterns.len() == 1 => {
69 if let PatKind::Wild = patterns[0].kind {
70 if match_qpath(path, &paths::RESULT_OK) {
72 } else if match_qpath(path, &paths::RESULT_ERR) {
74 } else if match_qpath(path, &paths::OPTION_SOME) {
84 PatKind::Path(ref path) if match_qpath(path, &paths::OPTION_NONE) => "is_none()",
89 // check that `while_let_on_iterator` lint does not trigger
91 if keyword == "while";
92 if let ExprKind::MethodCall(method_path, _, _) = op.kind;
93 if method_path.ident.name == sym!(next);
94 if match_trait_method(cx, op, &paths::ITERATOR);
102 REDUNDANT_PATTERN_MATCHING,
104 &format!("redundant pattern matching, consider using `{}`", good_method),
106 // in the case of WhileLetDesugar expr.span == op.span incorrectly.
107 // this is a workaround to restore true value of expr.span
108 let expr_span = expr.span.to(arms[1].span);
109 let span = expr_span.until(op.span.shrink_to_hi());
110 diag.span_suggestion(
113 format!("{} {}.{}", keyword, snippet(cx, op.span, "_"), good_method),
114 Applicability::MachineApplicable, // snippet
120 fn find_sugg_for_match<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>, op: &Expr<'_>, arms: &[Arm<'_>]) {
122 let node_pair = (&arms[0].pat.kind, &arms[1].pat.kind);
124 let found_good_method = match node_pair {
126 PatKind::TupleStruct(ref path_left, ref patterns_left, _),
127 PatKind::TupleStruct(ref path_right, ref patterns_right, _),
128 ) if patterns_left.len() == 1 && patterns_right.len() == 1 => {
129 if let (PatKind::Wild, PatKind::Wild) = (&patterns_left[0].kind, &patterns_right[0].kind) {
130 find_good_method_for_match(
143 (PatKind::TupleStruct(ref path_left, ref patterns, _), PatKind::Path(ref path_right))
144 | (PatKind::Path(ref path_left), PatKind::TupleStruct(ref path_right, ref patterns, _))
145 if patterns.len() == 1 =>
147 if let PatKind::Wild = patterns[0].kind {
148 find_good_method_for_match(
164 if let Some(good_method) = found_good_method {
167 REDUNDANT_PATTERN_MATCHING,
169 &format!("redundant pattern matching, consider using `{}`", good_method),
171 let span = expr.span.to(op.span);
172 diag.span_suggestion(
175 format!("{}.{}", snippet(cx, op.span, "_"), good_method),
176 Applicability::MaybeIncorrect, // snippet
184 fn find_good_method_for_match<'a>(
186 path_left: &QPath<'_>,
187 path_right: &QPath<'_>,
188 expected_left: &[&str],
189 expected_right: &[&str],
190 should_be_left: &'a str,
191 should_be_right: &'a str,
192 ) -> Option<&'a str> {
193 let body_node_pair = if match_qpath(path_left, expected_left) && match_qpath(path_right, expected_right) {
194 (&(*arms[0].body).kind, &(*arms[1].body).kind)
195 } else if match_qpath(path_right, expected_left) && match_qpath(path_left, expected_right) {
196 (&(*arms[1].body).kind, &(*arms[0].body).kind)
201 match body_node_pair {
202 (ExprKind::Lit(ref lit_left), ExprKind::Lit(ref lit_right)) => match (&lit_left.node, &lit_right.node) {
203 (LitKind::Bool(true), LitKind::Bool(false)) => Some(should_be_left),
204 (LitKind::Bool(false), LitKind::Bool(true)) => Some(should_be_right),