2020-04-17 13:53:13 +03:00
|
|
|
use crate::utils::{match_qpath, match_trait_method, paths, snippet, span_lint_and_then};
|
|
|
|
use if_chain::if_chain;
|
2020-03-01 12:23:33 +09:00
|
|
|
use rustc_ast::ast::LitKind;
|
2018-12-29 16:04:45 +01:00
|
|
|
use rustc_errors::Applicability;
|
2020-02-21 09:39:38 +01:00
|
|
|
use rustc_hir::{Arm, Expr, ExprKind, MatchSource, PatKind, QPath};
|
2020-01-12 15:08:41 +09:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-01-11 20:37:08 +09:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2016-10-29 18:56:12 +02:00
|
|
|
|
2018-03-28 15:24:26 +02:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 11:50:33 -05:00
|
|
|
/// **What it does:** Lint for redundant pattern matching over `Result` or
|
|
|
|
/// `Option`
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** It's more concise and clear to just use the proper
|
|
|
|
/// utility function
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// if let Ok(_) = Ok::<i32, i32>(42) {}
|
|
|
|
/// if let Err(_) = Err::<i32, i32>(42) {}
|
|
|
|
/// if let None = None::<()> {}
|
|
|
|
/// if let Some(_) = Some(42) {}
|
|
|
|
/// match Ok::<i32, i32>(42) {
|
|
|
|
/// Ok(_) => true,
|
|
|
|
/// Err(_) => false,
|
|
|
|
/// };
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// The more idiomatic use would be:
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// if Ok::<i32, i32>(42).is_ok() {}
|
|
|
|
/// if Err::<i32, i32>(42).is_err() {}
|
|
|
|
/// if None::<()>.is_none() {}
|
|
|
|
/// if Some(42).is_some() {}
|
|
|
|
/// Ok::<i32, i32>(42).is_ok();
|
|
|
|
/// ```
|
2018-10-10 23:13:53 +08:00
|
|
|
pub REDUNDANT_PATTERN_MATCHING,
|
2018-03-28 15:24:26 +02:00
|
|
|
style,
|
2016-10-29 18:56:12 +02:00
|
|
|
"use the proper utility function avoiding an `if let`"
|
|
|
|
}
|
|
|
|
|
2019-04-08 13:43:55 -07:00
|
|
|
declare_lint_pass!(RedundantPatternMatching => [REDUNDANT_PATTERN_MATCHING]);
|
2016-10-29 18:56:12 +02:00
|
|
|
|
2019-04-08 13:43:55 -07:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantPatternMatching {
|
2019-12-27 16:12:26 +09:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
|
|
|
|
if let ExprKind::Match(op, arms, ref match_source) = &expr.kind {
|
2018-10-07 21:36:42 +08:00
|
|
|
match match_source {
|
|
|
|
MatchSource::Normal => find_sugg_for_match(cx, expr, op, arms),
|
2020-04-17 13:53:13 +03:00
|
|
|
MatchSource::IfLetDesugar { .. } => find_sugg_for_if_let(cx, expr, op, arms, "if"),
|
|
|
|
MatchSource::WhileLetDesugar => find_sugg_for_if_let(cx, expr, op, arms, "while"),
|
2018-10-07 21:36:42 +08:00
|
|
|
_ => return,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-29 18:56:12 +02:00
|
|
|
|
2019-09-25 10:01:21 -07:00
|
|
|
fn find_sugg_for_if_let<'a, 'tcx>(
|
|
|
|
cx: &LateContext<'a, 'tcx>,
|
2019-12-27 16:12:26 +09:00
|
|
|
expr: &'tcx Expr<'_>,
|
|
|
|
op: &Expr<'_>,
|
|
|
|
arms: &[Arm<'_>],
|
2020-04-17 13:53:13 +03:00
|
|
|
keyword: &'static str,
|
2019-09-25 10:01:21 -07:00
|
|
|
) {
|
2019-09-27 17:16:06 +02:00
|
|
|
let good_method = match arms[0].pat.kind {
|
2019-09-25 12:00:17 -07:00
|
|
|
PatKind::TupleStruct(ref path, ref patterns, _) if patterns.len() == 1 => {
|
2019-09-27 17:16:06 +02:00
|
|
|
if let PatKind::Wild = patterns[0].kind {
|
2019-09-25 12:00:17 -07:00
|
|
|
if match_qpath(path, &paths::RESULT_OK) {
|
|
|
|
"is_ok()"
|
|
|
|
} else if match_qpath(path, &paths::RESULT_ERR) {
|
|
|
|
"is_err()"
|
|
|
|
} else if match_qpath(path, &paths::OPTION_SOME) {
|
|
|
|
"is_some()"
|
2018-10-07 21:36:42 +08:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2019-09-25 12:00:17 -07:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
},
|
2016-10-29 18:56:12 +02:00
|
|
|
|
2019-09-25 12:00:17 -07:00
|
|
|
PatKind::Path(ref path) if match_qpath(path, &paths::OPTION_NONE) => "is_none()",
|
2016-10-29 18:56:12 +02:00
|
|
|
|
2019-09-25 12:00:17 -07:00
|
|
|
_ => return,
|
|
|
|
};
|
2018-10-07 21:36:42 +08:00
|
|
|
|
2020-04-17 13:53:13 +03:00
|
|
|
// check that `while_let_on_iterator` lint does not trigger
|
|
|
|
if_chain! {
|
|
|
|
if keyword == "while";
|
|
|
|
if let ExprKind::MethodCall(method_path, _, _) = op.kind;
|
|
|
|
if method_path.ident.name == sym!(next);
|
|
|
|
if match_trait_method(cx, op, &paths::ITERATOR);
|
|
|
|
then {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-09-25 10:01:21 -07:00
|
|
|
|
2019-09-25 12:00:17 -07:00
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
|
|
|
REDUNDANT_PATTERN_MATCHING,
|
|
|
|
arms[0].pat.span,
|
|
|
|
&format!("redundant pattern matching, consider using `{}`", good_method),
|
2020-04-17 08:08:00 +02:00
|
|
|
|diag| {
|
2020-04-23 11:40:16 +03:00
|
|
|
// while let ... = ... { ... }
|
|
|
|
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
let expr_span = expr.span;
|
|
|
|
|
|
|
|
// while let ... = ... { ... }
|
|
|
|
// ^^^
|
|
|
|
let op_span = op.span.source_callsite();
|
|
|
|
|
|
|
|
// while let ... = ... { ... }
|
|
|
|
// ^^^^^^^^^^^^^^^^^^^
|
|
|
|
let span = expr_span.until(op_span.shrink_to_hi());
|
2020-04-17 08:08:00 +02:00
|
|
|
diag.span_suggestion(
|
2019-09-25 12:00:17 -07:00
|
|
|
span,
|
|
|
|
"try this",
|
2020-04-23 11:40:16 +03:00
|
|
|
format!("{} {}.{}", keyword, snippet(cx, op_span, "_"), good_method),
|
2020-04-17 13:53:13 +03:00
|
|
|
Applicability::MachineApplicable, // snippet
|
2019-09-25 12:00:17 -07:00
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
2018-10-07 21:36:42 +08:00
|
|
|
}
|
|
|
|
|
2019-12-27 16:12:26 +09:00
|
|
|
fn find_sugg_for_match<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>, op: &Expr<'_>, arms: &[Arm<'_>]) {
|
2018-10-07 21:36:42 +08:00
|
|
|
if arms.len() == 2 {
|
2019-09-27 17:16:06 +02:00
|
|
|
let node_pair = (&arms[0].pat.kind, &arms[1].pat.kind);
|
2018-10-07 21:36:42 +08:00
|
|
|
|
|
|
|
let found_good_method = match node_pair {
|
|
|
|
(
|
2018-11-02 12:12:14 +01:00
|
|
|
PatKind::TupleStruct(ref path_left, ref patterns_left, _),
|
2018-11-27 21:14:15 +01:00
|
|
|
PatKind::TupleStruct(ref path_right, ref patterns_right, _),
|
2018-11-02 12:12:14 +01:00
|
|
|
) if patterns_left.len() == 1 && patterns_right.len() == 1 => {
|
2019-09-27 17:16:06 +02:00
|
|
|
if let (PatKind::Wild, PatKind::Wild) = (&patterns_left[0].kind, &patterns_right[0].kind) {
|
2018-10-07 21:36:42 +08:00
|
|
|
find_good_method_for_match(
|
|
|
|
arms,
|
|
|
|
path_left,
|
|
|
|
path_right,
|
2019-05-17 23:53:54 +02:00
|
|
|
&paths::RESULT_OK,
|
|
|
|
&paths::RESULT_ERR,
|
2018-10-07 21:36:42 +08:00
|
|
|
"is_ok()",
|
2018-11-27 21:14:15 +01:00
|
|
|
"is_err()",
|
2018-10-07 21:36:42 +08:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
},
|
2018-11-27 21:14:15 +01:00
|
|
|
(PatKind::TupleStruct(ref path_left, ref patterns, _), PatKind::Path(ref path_right))
|
|
|
|
| (PatKind::Path(ref path_left), PatKind::TupleStruct(ref path_right, ref patterns, _))
|
|
|
|
if patterns.len() == 1 =>
|
|
|
|
{
|
2019-09-27 17:16:06 +02:00
|
|
|
if let PatKind::Wild = patterns[0].kind {
|
2018-10-07 21:36:42 +08:00
|
|
|
find_good_method_for_match(
|
|
|
|
arms,
|
|
|
|
path_left,
|
|
|
|
path_right,
|
2019-05-17 23:53:54 +02:00
|
|
|
&paths::OPTION_SOME,
|
|
|
|
&paths::OPTION_NONE,
|
2018-10-07 21:36:42 +08:00
|
|
|
"is_some()",
|
2018-11-27 21:14:15 +01:00
|
|
|
"is_none()",
|
2018-10-07 21:36:42 +08:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(good_method) = found_good_method {
|
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
2018-10-10 23:13:53 +08:00
|
|
|
REDUNDANT_PATTERN_MATCHING,
|
2018-10-07 21:36:42 +08:00
|
|
|
expr.span,
|
|
|
|
&format!("redundant pattern matching, consider using `{}`", good_method),
|
2020-04-17 08:08:00 +02:00
|
|
|
|diag| {
|
2018-10-07 21:36:42 +08:00
|
|
|
let span = expr.span.to(op.span);
|
2020-04-17 08:08:00 +02:00
|
|
|
diag.span_suggestion(
|
2018-10-07 21:36:42 +08:00
|
|
|
span,
|
|
|
|
"try this",
|
|
|
|
format!("{}.{}", snippet(cx, op.span, "_"), good_method),
|
2019-08-07 20:34:23 +02:00
|
|
|
Applicability::MaybeIncorrect, // snippet
|
2018-10-07 21:36:42 +08:00
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
2016-10-29 18:56:12 +02:00
|
|
|
}
|
2018-10-07 21:36:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn find_good_method_for_match<'a>(
|
2019-12-27 16:12:26 +09:00
|
|
|
arms: &[Arm<'_>],
|
2019-12-30 13:02:10 +09:00
|
|
|
path_left: &QPath<'_>,
|
|
|
|
path_right: &QPath<'_>,
|
2019-05-17 23:53:54 +02:00
|
|
|
expected_left: &[&str],
|
|
|
|
expected_right: &[&str],
|
2018-10-07 21:36:42 +08:00
|
|
|
should_be_left: &'a str,
|
2018-11-27 21:14:15 +01:00
|
|
|
should_be_right: &'a str,
|
2018-10-07 21:36:42 +08:00
|
|
|
) -> Option<&'a str> {
|
|
|
|
let body_node_pair = if match_qpath(path_left, expected_left) && match_qpath(path_right, expected_right) {
|
2019-09-27 17:16:06 +02:00
|
|
|
(&(*arms[0].body).kind, &(*arms[1].body).kind)
|
2018-10-07 21:36:42 +08:00
|
|
|
} else if match_qpath(path_right, expected_left) && match_qpath(path_left, expected_right) {
|
2019-09-27 17:16:06 +02:00
|
|
|
(&(*arms[1].body).kind, &(*arms[0].body).kind)
|
2018-10-07 21:36:42 +08:00
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
|
|
|
|
match body_node_pair {
|
2018-11-27 21:14:15 +01:00
|
|
|
(ExprKind::Lit(ref lit_left), ExprKind::Lit(ref lit_right)) => match (&lit_left.node, &lit_right.node) {
|
|
|
|
(LitKind::Bool(true), LitKind::Bool(false)) => Some(should_be_left),
|
|
|
|
(LitKind::Bool(false), LitKind::Bool(true)) => Some(should_be_right),
|
|
|
|
_ => None,
|
2018-10-07 21:36:42 +08:00
|
|
|
},
|
|
|
|
_ => None,
|
2016-10-29 18:56:12 +02:00
|
|
|
}
|
|
|
|
}
|