2021-03-16 00:55:45 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_help;
|
2021-03-13 23:01:03 +00:00
|
|
|
use clippy_utils::ty::implements_trait;
|
2021-04-14 17:40:51 +00:00
|
|
|
use clippy_utils::{get_trait_def_id, if_sequence, is_else_clause, paths, SpanlessEq};
|
2021-04-22 07:52:34 +00:00
|
|
|
use rustc_hir::{BinOpKind, Expr, ExprKind, Node};
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2019-09-24 21:55:05 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:** Checks comparison chains written with `if` that can be
|
|
|
|
/// rewritten with `match` and `cmp`.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** `if` is not guaranteed to be exhaustive and conditionals can get
|
|
|
|
/// repetitive
|
|
|
|
///
|
2020-11-26 20:07:50 +00:00
|
|
|
/// **Known problems:** The match statement may be slower due to the compiler
|
2021-01-13 00:21:26 +00:00
|
|
|
/// not inlining the call to cmp. See issue [#5354](https://github.com/rust-lang/rust-clippy/issues/5354)
|
2019-09-24 21:55:05 +00:00
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// # fn a() {}
|
|
|
|
/// # fn b() {}
|
|
|
|
/// # fn c() {}
|
|
|
|
/// fn f(x: u8, y: u8) {
|
|
|
|
/// if x > y {
|
|
|
|
/// a()
|
|
|
|
/// } else if x < y {
|
|
|
|
/// b()
|
|
|
|
/// } else {
|
|
|
|
/// c()
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Could be written:
|
|
|
|
///
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// use std::cmp::Ordering;
|
|
|
|
/// # fn a() {}
|
|
|
|
/// # fn b() {}
|
|
|
|
/// # fn c() {}
|
|
|
|
/// fn f(x: u8, y: u8) {
|
2019-09-24 22:05:43 +00:00
|
|
|
/// match x.cmp(&y) {
|
2019-09-24 21:55:05 +00:00
|
|
|
/// Ordering::Greater => a(),
|
|
|
|
/// Ordering::Less => b(),
|
|
|
|
/// Ordering::Equal => c()
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub COMPARISON_CHAIN,
|
|
|
|
style,
|
|
|
|
"`if`s that can be rewritten with `match` and `cmp`"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(ComparisonChain => [COMPARISON_CHAIN]);
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for ComparisonChain {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
2019-09-24 21:55:05 +00:00
|
|
|
if expr.span.from_expansion() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We only care about the top-most `if` in the chain
|
2021-04-14 17:40:51 +00:00
|
|
|
if is_else_clause(cx.tcx, expr) {
|
2019-09-24 21:55:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-22 07:52:34 +00:00
|
|
|
if parent_node_is_if_const_fn(cx, expr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-24 21:55:05 +00:00
|
|
|
// Check that there exists at least one explicit else condition
|
|
|
|
let (conds, _) = if_sequence(expr);
|
|
|
|
if conds.len() < 2 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for cond in conds.windows(2) {
|
2021-04-02 21:35:32 +00:00
|
|
|
if let (&ExprKind::Binary(ref kind1, lhs1, rhs1), &ExprKind::Binary(ref kind2, lhs2, rhs2)) =
|
|
|
|
(&cond[0].kind, &cond[1].kind)
|
2019-09-24 21:55:05 +00:00
|
|
|
{
|
|
|
|
if !kind_is_cmp(kind1.node) || !kind_is_cmp(kind2.node) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that both sets of operands are equal
|
|
|
|
let mut spanless_eq = SpanlessEq::new(cx);
|
2020-05-17 15:36:26 +00:00
|
|
|
let same_fixed_operands = spanless_eq.eq_expr(lhs1, lhs2) && spanless_eq.eq_expr(rhs1, rhs2);
|
|
|
|
let same_transposed_operands = spanless_eq.eq_expr(lhs1, rhs2) && spanless_eq.eq_expr(rhs1, lhs2);
|
|
|
|
|
|
|
|
if !same_fixed_operands && !same_transposed_operands {
|
2019-09-24 21:55:05 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-11-23 21:35:21 +00:00
|
|
|
|
2020-05-17 15:36:26 +00:00
|
|
|
// Check that if the operation is the same, either it's not `==` or the operands are transposed
|
|
|
|
if kind1.node == kind2.node {
|
|
|
|
if kind1.node == BinOpKind::Eq {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if !same_transposed_operands {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 21:35:21 +00:00
|
|
|
// Check that the type being compared implements `core::cmp::Ord`
|
2020-07-17 08:47:04 +00:00
|
|
|
let ty = cx.typeck_results().expr_ty(lhs1);
|
2019-11-23 21:35:21 +00:00
|
|
|
let is_ord = get_trait_def_id(cx, &paths::ORD).map_or(false, |id| implements_trait(cx, ty, id, &[]));
|
|
|
|
|
|
|
|
if !is_ord {
|
|
|
|
return;
|
|
|
|
}
|
2019-09-24 21:55:05 +00:00
|
|
|
} else {
|
|
|
|
// We only care about comparison chains
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-01-27 01:56:22 +00:00
|
|
|
span_lint_and_help(
|
2019-09-24 21:55:05 +00:00
|
|
|
cx,
|
|
|
|
COMPARISON_CHAIN,
|
|
|
|
expr.span,
|
|
|
|
"`if` chain can be rewritten with `match`",
|
2020-04-18 10:28:29 +00:00
|
|
|
None,
|
2021-02-24 13:02:51 +00:00
|
|
|
"consider rewriting the `if` chain to use `cmp` and `match`",
|
2019-09-24 21:55:05 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn kind_is_cmp(kind: BinOpKind) -> bool {
|
2020-07-14 12:59:59 +00:00
|
|
|
matches!(kind, BinOpKind::Lt | BinOpKind::Gt | BinOpKind::Eq)
|
2019-09-24 21:55:05 +00:00
|
|
|
}
|
2021-04-22 07:52:34 +00:00
|
|
|
|
|
|
|
fn parent_node_is_if_const_fn(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
|
|
|
|
match cx.tcx.hir().find(cx.tcx.hir().get_parent_item(expr.hir_id)) {
|
|
|
|
Some(Node::Item(item)) => rustc_mir::const_eval::is_const_fn(cx.tcx, item.def_id.to_def_id()),
|
|
|
|
Some(Node::ImplItem(impl_item)) => rustc_mir::const_eval::is_const_fn(cx.tcx, impl_item.def_id.to_def_id()),
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|