|
| 1 | +use clippy_utils::diagnostics::span_lint_and_then; |
| 2 | +use clippy_utils::macros::{find_assert_args, root_macro_call_first_node}; |
| 3 | +use clippy_utils::source::walk_span_to_context; |
| 4 | +use clippy_utils::ty::implements_trait; |
| 5 | +use clippy_utils::{is_in_const_context, sym}; |
| 6 | +use rustc_errors::Applicability; |
| 7 | +use rustc_hir::{BinOpKind, Expr, ExprKind}; |
| 8 | +use rustc_lint::{LateContext, LateLintPass, LintContext}; |
| 9 | +use rustc_session::declare_lint_pass; |
| 10 | + |
| 11 | +declare_clippy_lint! { |
| 12 | + /// ### What it does |
| 13 | + /// Checks for `assert!` and `debug_assert!` that consist of only an (in)equality check |
| 14 | + /// |
| 15 | + /// ### Why is this bad? |
| 16 | + /// `assert_{eq,ne}!` and `debug_assert_{eq,ne}!` achieves the same goal, and provides some |
| 17 | + /// additional debug information |
| 18 | + /// |
| 19 | + /// ### Example |
| 20 | + /// ```no_run |
| 21 | + /// assert!(2 * 2 == 4); |
| 22 | + /// assert!(2 * 2 != 5); |
| 23 | + /// debug_assert!(2 * 2 == 4); |
| 24 | + /// debug_assert!(2 * 2 != 5); |
| 25 | + /// ``` |
| 26 | + /// Use instead: |
| 27 | + /// ```no_run |
| 28 | + /// assert_eq!(2 * 2, 4); |
| 29 | + /// assert_ne!(2 * 2, 5); |
| 30 | + /// debug_assert_eq!(2 * 2, 4); |
| 31 | + /// debug_assert_ne!(2 * 2, 5); |
| 32 | + /// ``` |
| 33 | + #[clippy::version = "1.92.0"] |
| 34 | + pub MANUAL_ASSERT_EQ, |
| 35 | + complexity, |
| 36 | + "checks for assertions consisting of an (in)equality check" |
| 37 | +} |
| 38 | +declare_lint_pass!(ManualAssertEq => [MANUAL_ASSERT_EQ]); |
| 39 | + |
| 40 | +impl LateLintPass<'_> for ManualAssertEq { |
| 41 | + fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { |
| 42 | + if let Some(macro_call) = root_macro_call_first_node(cx, expr) |
| 43 | + && let macro_name = match cx.tcx.get_diagnostic_name(macro_call.def_id) { |
| 44 | + Some(sym::assert_macro) => "assert", |
| 45 | + Some(sym::debug_assert_macro) => "debug_assert", |
| 46 | + _ => return, |
| 47 | + } |
| 48 | + // `assert_eq` isn't allowed in const context because it calls non-const `core::panicking::assert_failed` |
| 49 | + // XXX: this might change in the future, so might want to relax this restriction |
| 50 | + && !is_in_const_context(cx) |
| 51 | + && let Some((cond, _)) = find_assert_args(cx, expr, macro_call.expn) |
| 52 | + && let ExprKind::Binary(op, lhs, rhs) = cond.kind |
| 53 | + && matches!(op.node, BinOpKind::Eq | BinOpKind::Ne) |
| 54 | + && !cond.span.from_expansion() |
| 55 | + && let Some(debug_trait) = cx.tcx.get_diagnostic_item(sym::Debug) |
| 56 | + && implements_trait(cx, cx.typeck_results().expr_ty(lhs), debug_trait, &[]) |
| 57 | + && implements_trait(cx, cx.typeck_results().expr_ty(rhs), debug_trait, &[]) |
| 58 | + { |
| 59 | + span_lint_and_then( |
| 60 | + cx, |
| 61 | + MANUAL_ASSERT_EQ, |
| 62 | + macro_call.span, |
| 63 | + format!("used `{macro_name}!` with an equality comparison"), |
| 64 | + |diag| { |
| 65 | + let kind = if op.node == BinOpKind::Eq { "eq" } else { "ne" }; |
| 66 | + let new_name = format!("{macro_name}_{kind}"); |
| 67 | + let msg = format!("replace it with `{new_name}!(..)`"); |
| 68 | + |
| 69 | + let ctxt = cond.span.ctxt(); |
| 70 | + if let Some(lhs_span) = walk_span_to_context(lhs.span, ctxt) |
| 71 | + && let Some(rhs_span) = walk_span_to_context(rhs.span, ctxt) |
| 72 | + { |
| 73 | + let macro_name_span = cx.sess().source_map().span_until_char(macro_call.span, '!'); |
| 74 | + let eq_span = cond.span.with_lo(lhs_span.hi()).with_hi(rhs_span.lo()); |
| 75 | + let suggestions = vec![ |
| 76 | + (macro_name_span.shrink_to_hi(), format!("_{kind}")), |
| 77 | + (eq_span, ", ".to_string()), |
| 78 | + ]; |
| 79 | + |
| 80 | + diag.multipart_suggestion(msg, suggestions, Applicability::MachineApplicable); |
| 81 | + } else { |
| 82 | + diag.span_help(expr.span, msg); |
| 83 | + } |
| 84 | + }, |
| 85 | + ); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments