2021-10-07 09:21:30 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
2021-10-21 11:11:36 +00:00
|
|
|
use clippy_utils::source::snippet_with_context;
|
2021-10-07 09:21:30 +00:00
|
|
|
use clippy_utils::ty::implements_trait;
|
|
|
|
use if_chain::if_chain;
|
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir::{Expr, ExprKind, Pat, PatKind};
|
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
|
|
|
use rustc_middle::ty::Ty;
|
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// ### What it does
|
|
|
|
/// Checks for pattern matchings that can be expressed using equality.
|
|
|
|
///
|
|
|
|
/// ### Why is this bad?
|
|
|
|
///
|
|
|
|
/// * It reads better and has less cognitive load because equality won't cause binding.
|
|
|
|
/// * It is a [Yoda condition](https://en.wikipedia.org/wiki/Yoda_conditions). Yoda conditions are widely
|
|
|
|
/// criticized for increasing the cognitive load of reading the code.
|
|
|
|
/// * Equality is a simple bool expression and can be merged with `&&` and `||` and
|
|
|
|
/// reuse if blocks
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// if let Some(2) = x {
|
|
|
|
/// do_thing();
|
|
|
|
/// }
|
|
|
|
/// ```
|
2022-05-28 13:22:59 +00:00
|
|
|
/// Use instead:
|
2021-10-07 09:21:30 +00:00
|
|
|
/// ```rust,ignore
|
|
|
|
/// if x == Some(2) {
|
|
|
|
/// do_thing();
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-12-06 11:33:31 +00:00
|
|
|
#[clippy::version = "1.57.0"]
|
2021-10-07 09:21:30 +00:00
|
|
|
pub EQUATABLE_IF_LET,
|
|
|
|
nursery,
|
|
|
|
"using pattern matching instead of equality"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(PatternEquality => [EQUATABLE_IF_LET]);
|
|
|
|
|
|
|
|
/// detects if pattern matches just one thing
|
|
|
|
fn unary_pattern(pat: &Pat<'_>) -> bool {
|
|
|
|
fn array_rec(pats: &[Pat<'_>]) -> bool {
|
|
|
|
pats.iter().all(unary_pattern)
|
|
|
|
}
|
|
|
|
match &pat.kind {
|
|
|
|
PatKind::Slice(_, _, _) | PatKind::Range(_, _, _) | PatKind::Binding(..) | PatKind::Wild | PatKind::Or(_) => {
|
|
|
|
false
|
|
|
|
},
|
|
|
|
PatKind::Struct(_, a, etc) => !etc && a.iter().all(|x| unary_pattern(x.pat)),
|
|
|
|
PatKind::Tuple(a, etc) | PatKind::TupleStruct(_, a, etc) => !etc.is_some() && array_rec(a),
|
|
|
|
PatKind::Ref(x, _) | PatKind::Box(x) => unary_pattern(x),
|
|
|
|
PatKind::Path(_) | PatKind::Lit(_) => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-11 15:52:23 +00:00
|
|
|
fn is_structural_partial_eq<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, other: Ty<'tcx>) -> bool {
|
2021-10-07 09:21:30 +00:00
|
|
|
if let Some(def_id) = cx.tcx.lang_items().eq_trait() {
|
|
|
|
implements_trait(cx, ty, def_id, &[other.into()])
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for PatternEquality {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
|
|
|
|
if_chain! {
|
2021-10-13 07:44:47 +00:00
|
|
|
if let ExprKind::Let(let_expr) = expr.kind;
|
|
|
|
if unary_pattern(let_expr.pat);
|
|
|
|
let exp_ty = cx.typeck_results().expr_ty(let_expr.init);
|
|
|
|
let pat_ty = cx.typeck_results().pat_ty(let_expr.pat);
|
2021-10-07 09:21:30 +00:00
|
|
|
if is_structural_partial_eq(cx, exp_ty, pat_ty);
|
|
|
|
then {
|
|
|
|
|
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
2021-10-13 07:44:47 +00:00
|
|
|
let pat_str = match let_expr.pat.kind {
|
2021-10-07 09:21:30 +00:00
|
|
|
PatKind::Struct(..) => format!(
|
|
|
|
"({})",
|
2021-10-13 07:44:47 +00:00
|
|
|
snippet_with_context(cx, let_expr.pat.span, expr.span.ctxt(), "..", &mut applicability).0,
|
2021-10-07 09:21:30 +00:00
|
|
|
),
|
2021-10-13 07:44:47 +00:00
|
|
|
_ => snippet_with_context(cx, let_expr.pat.span, expr.span.ctxt(), "..", &mut applicability).0.to_string(),
|
2021-10-07 09:21:30 +00:00
|
|
|
};
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
EQUATABLE_IF_LET,
|
|
|
|
expr.span,
|
|
|
|
"this pattern matching can be expressed using equality",
|
|
|
|
"try",
|
|
|
|
format!(
|
|
|
|
"{} == {}",
|
2021-10-13 07:44:47 +00:00
|
|
|
snippet_with_context(cx, let_expr.init.span, expr.span.ctxt(), "..", &mut applicability).0,
|
2021-10-07 09:21:30 +00:00
|
|
|
pat_str,
|
|
|
|
),
|
|
|
|
applicability,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|