2019-11-23 15:35:21 -06:00
|
|
|
use crate::utils::{
|
2020-01-26 19:56:22 -06:00
|
|
|
get_trait_def_id, if_sequence, implements_trait, parent_node_is_if_expr, paths, span_lint_and_help, SpanlessEq,
|
2019-11-23 15:35:21 -06:00
|
|
|
};
|
2020-02-21 02:39:38 -06:00
|
|
|
use rustc_hir::{BinOpKind, Expr, ExprKind};
|
2020-01-12 00:08:41 -06:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-01-11 05:37:08 -06:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2019-09-24 16:55:05 -05: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
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **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 17:05:43 -05:00
|
|
|
/// match x.cmp(&y) {
|
2019-09-24 16:55:05 -05: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]);
|
|
|
|
|
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ComparisonChain {
|
2019-12-27 01:12:26 -06:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
|
2019-09-24 16:55:05 -05:00
|
|
|
if expr.span.from_expansion() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We only care about the top-most `if` in the chain
|
|
|
|
if parent_node_is_if_expr(expr, cx) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
if let (
|
|
|
|
&ExprKind::Binary(ref kind1, ref lhs1, ref rhs1),
|
|
|
|
&ExprKind::Binary(ref kind2, ref lhs2, ref rhs2),
|
2019-09-27 10:16:06 -05:00
|
|
|
) = (&cond[0].kind, &cond[1].kind)
|
2019-09-24 16:55:05 -05: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 10:36:26 -05: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 16:55:05 -05:00
|
|
|
return;
|
|
|
|
}
|
2019-11-23 15:35:21 -06:00
|
|
|
|
2020-05-17 10:36:26 -05: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 15:35:21 -06:00
|
|
|
// Check that the type being compared implements `core::cmp::Ord`
|
2020-06-25 18:56:23 -05:00
|
|
|
let ty = cx.tables().expr_ty(lhs1);
|
2019-11-23 15:35:21 -06: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 16:55:05 -05:00
|
|
|
} else {
|
|
|
|
// We only care about comparison chains
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-01-26 19:56:22 -06:00
|
|
|
span_lint_and_help(
|
2019-09-24 16:55:05 -05:00
|
|
|
cx,
|
|
|
|
COMPARISON_CHAIN,
|
|
|
|
expr.span,
|
|
|
|
"`if` chain can be rewritten with `match`",
|
2020-04-18 05:28:29 -05:00
|
|
|
None,
|
2019-09-24 16:55:05 -05:00
|
|
|
"Consider rewriting the `if` chain to use `cmp` and `match`.",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn kind_is_cmp(kind: BinOpKind) -> bool {
|
|
|
|
match kind {
|
|
|
|
BinOpKind::Lt | BinOpKind::Gt | BinOpKind::Eq => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|