|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::sugg::Sugg; |
| 3 | +use clippy_utils::visitors::{Descend, for_each_expr_without_closures}; |
| 4 | +use clippy_utils::{SpanlessEq, is_integer_literal}; |
| 5 | +use rustc_errors::Applicability; |
| 6 | +use rustc_hir::{BinOpKind, Block, Expr, ExprKind}; |
| 7 | +use rustc_lint::{LateContext, LateLintPass}; |
| 8 | +use rustc_middle::ty; |
| 9 | +use rustc_session::declare_lint_pass; |
| 10 | +use std::ops::ControlFlow; |
| 11 | + |
| 12 | +declare_clippy_lint! { |
| 13 | + /// ### What it does |
| 14 | + /// Detects manual zero checks before dividing unsigned integers, such as `if x != 0 { y / x }`. |
| 15 | + /// |
| 16 | + /// ### Why is this bad? |
| 17 | + /// `checked_div` already handles the zero case and makes the intent clearer while avoiding a |
| 18 | + /// panic from a manual division. |
| 19 | + /// |
| 20 | + /// ### Example |
| 21 | + /// ```no_run |
| 22 | + /// # let (a, b) = (10u32, 5u32); |
| 23 | + /// if b != 0 { |
| 24 | + /// let result = a / b; |
| 25 | + /// println!("{result}"); |
| 26 | + /// } |
| 27 | + /// ``` |
| 28 | + /// Use instead: |
| 29 | + /// ```no_run |
| 30 | + /// # let (a, b) = (10u32, 5u32); |
| 31 | + /// if let Some(result) = a.checked_div(b) { |
| 32 | + /// println!("{result}"); |
| 33 | + /// } |
| 34 | + /// ``` |
| 35 | + #[clippy::version = "1.93.0"] |
| 36 | + pub MANUAL_CHECKED_DIV, |
| 37 | + nursery, |
| 38 | + "manual zero checks before dividing unsigned integers" |
| 39 | +} |
| 40 | +declare_lint_pass!(ManualCheckedDiv => [MANUAL_CHECKED_DIV]); |
| 41 | + |
| 42 | +#[derive(Copy, Clone)] |
| 43 | +enum NonZeroBranch { |
| 44 | + Then, |
| 45 | + Else, |
| 46 | +} |
| 47 | + |
| 48 | +impl LateLintPass<'_> for ManualCheckedDiv { |
| 49 | + fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { |
| 50 | + if expr.span.from_expansion() { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + if let ExprKind::If(cond, then, r#else) = expr.kind |
| 55 | + && let Some((divisor, branch)) = divisor_from_condition(cond) |
| 56 | + && is_unsigned(cx, divisor) |
| 57 | + { |
| 58 | + let Some(block) = branch_block(then, r#else, branch) else { |
| 59 | + return; |
| 60 | + }; |
| 61 | + let mut eq = SpanlessEq::new(cx); |
| 62 | + |
| 63 | + for_each_expr_without_closures(block, |e| { |
| 64 | + if let ExprKind::Binary(binop, lhs, rhs) = e.kind |
| 65 | + && binop.node == BinOpKind::Div |
| 66 | + && eq.eq_expr(rhs, divisor) |
| 67 | + && is_unsigned(cx, lhs) |
| 68 | + { |
| 69 | + let mut applicability = Applicability::MaybeIncorrect; |
| 70 | + let lhs_snip = Sugg::hir_with_applicability(cx, lhs, "..", &mut applicability); |
| 71 | + let rhs_snip = Sugg::hir_with_applicability(cx, rhs, "..", &mut applicability); |
| 72 | + |
| 73 | + span_lint_and_sugg( |
| 74 | + cx, |
| 75 | + MANUAL_CHECKED_DIV, |
| 76 | + e.span, |
| 77 | + "manual checked division", |
| 78 | + "consider using `checked_div`", |
| 79 | + format!("{}.checked_div({})", lhs_snip.maybe_paren(), rhs_snip), |
| 80 | + applicability, |
| 81 | + ); |
| 82 | + |
| 83 | + ControlFlow::<(), _>::Continue(Descend::No) |
| 84 | + } else { |
| 85 | + ControlFlow::<(), _>::Continue(Descend::Yes) |
| 86 | + } |
| 87 | + }); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +fn divisor_from_condition<'tcx>(cond: &'tcx Expr<'tcx>) -> Option<(&'tcx Expr<'tcx>, NonZeroBranch)> { |
| 93 | + let ExprKind::Binary(binop, lhs, rhs) = cond.kind else { |
| 94 | + return None; |
| 95 | + }; |
| 96 | + |
| 97 | + match binop.node { |
| 98 | + BinOpKind::Ne | BinOpKind::Lt if is_zero(lhs) => Some((rhs, NonZeroBranch::Then)), |
| 99 | + BinOpKind::Ne | BinOpKind::Gt if is_zero(rhs) => Some((lhs, NonZeroBranch::Then)), |
| 100 | + BinOpKind::Eq if is_zero(lhs) => Some((rhs, NonZeroBranch::Else)), |
| 101 | + BinOpKind::Eq if is_zero(rhs) => Some((lhs, NonZeroBranch::Else)), |
| 102 | + _ => None, |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +fn branch_block<'tcx>( |
| 107 | + then: &'tcx Expr<'tcx>, |
| 108 | + r#else: Option<&'tcx Expr<'tcx>>, |
| 109 | + branch: NonZeroBranch, |
| 110 | +) -> Option<&'tcx Block<'tcx>> { |
| 111 | + match branch { |
| 112 | + NonZeroBranch::Then => { |
| 113 | + if let ExprKind::Block(block, _) = then.kind { |
| 114 | + Some(block) |
| 115 | + } else { |
| 116 | + None |
| 117 | + } |
| 118 | + }, |
| 119 | + NonZeroBranch::Else => match r#else.map(|expr| &expr.kind) { |
| 120 | + Some(ExprKind::Block(block, _)) => Some(block), |
| 121 | + _ => None, |
| 122 | + }, |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +fn is_zero(expr: &Expr<'_>) -> bool { |
| 127 | + is_integer_literal(expr, 0) |
| 128 | +} |
| 129 | + |
| 130 | +fn is_unsigned(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { |
| 131 | + matches!(cx.typeck_results().expr_ty(expr).peel_refs().kind(), ty::Uint(_)) |
| 132 | +} |
0 commit comments