|
| 1 | +use clippy_config::msrvs::{self, Msrv}; |
| 2 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 3 | +use clippy_utils::source::snippet_with_applicability; |
| 4 | +use clippy_utils::sugg::Sugg; |
| 5 | +use clippy_utils::SpanlessEq; |
| 6 | +use rustc_ast::{BinOpKind, LitKind}; |
| 7 | +use rustc_data_structures::packed::Pu128; |
| 8 | +use rustc_errors::Applicability; |
| 9 | +use rustc_hir::{Expr, ExprKind}; |
| 10 | +use rustc_lint::{LateContext, LateLintPass}; |
| 11 | +use rustc_middle::ty::{self}; |
| 12 | +use rustc_session::impl_lint_pass; |
| 13 | +use rustc_span::symbol::Symbol; |
| 14 | + |
| 15 | +use clippy_config::Conf; |
| 16 | + |
| 17 | +declare_clippy_lint! { |
| 18 | + /// ### What it does |
| 19 | + /// Checks for an expression like `(x + (y - 1)) / y` which is a common manual reimplementation |
| 20 | + /// of `x.div_ceil(y)`. |
| 21 | + /// |
| 22 | + /// ### Why is this bad? |
| 23 | + /// It's simpler, clearer and more readable. |
| 24 | + /// |
| 25 | + /// ### Example |
| 26 | + /// ```no_run |
| 27 | + /// let x: i32 = 7; |
| 28 | + /// let y: i32 = 4; |
| 29 | + /// let div = (x + (y - 1)) / y; |
| 30 | + /// ``` |
| 31 | + /// Use instead: |
| 32 | + /// ```no_run |
| 33 | + /// #![feature(int_roundings)] |
| 34 | + /// let x: i32 = 7; |
| 35 | + /// let y: i32 = 4; |
| 36 | + /// let div = x.div_ceil(y); |
| 37 | + /// ``` |
| 38 | + #[clippy::version = "1.81.0"] |
| 39 | + pub MANUAL_DIV_CEIL, |
| 40 | + complexity, |
| 41 | + "manually reimplementing `div_ceil`" |
| 42 | +} |
| 43 | + |
| 44 | +pub struct ManualDivCeil { |
| 45 | + msrv: Msrv, |
| 46 | +} |
| 47 | + |
| 48 | +impl ManualDivCeil { |
| 49 | + #[must_use] |
| 50 | + pub fn new(conf: &'static Conf) -> Self { |
| 51 | + Self { |
| 52 | + msrv: conf.msrv.clone(), |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +impl_lint_pass!(ManualDivCeil => [MANUAL_DIV_CEIL]); |
| 58 | + |
| 59 | +impl<'tcx> LateLintPass<'tcx> for ManualDivCeil { |
| 60 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'_>) { |
| 61 | + if !self.msrv.meets(msrvs::MANUAL_DIV_CEIL) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + let mut applicability = Applicability::MachineApplicable; |
| 66 | + |
| 67 | + if let ExprKind::Binary(div_op, div_lhs, div_rhs) = expr.kind |
| 68 | + && div_op.node == BinOpKind::Div |
| 69 | + && check_int_ty_and_feature(cx, div_lhs) |
| 70 | + && check_int_ty_and_feature(cx, div_rhs) |
| 71 | + && let ExprKind::Binary(inner_op, inner_lhs, inner_rhs) = div_lhs.kind |
| 72 | + { |
| 73 | + // (x + (y - 1)) / y |
| 74 | + if let ExprKind::Binary(sub_op, sub_lhs, sub_rhs) = inner_rhs.kind |
| 75 | + && inner_op.node == BinOpKind::Add |
| 76 | + && sub_op.node == BinOpKind::Sub |
| 77 | + && check_literal(sub_rhs) |
| 78 | + && check_eq_expr(cx, sub_lhs, div_rhs) |
| 79 | + { |
| 80 | + build_suggestion(cx, expr, inner_lhs, div_rhs, &mut applicability); |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + // ((y - 1) + x) / y |
| 85 | + if let ExprKind::Binary(sub_op, sub_lhs, sub_rhs) = inner_lhs.kind |
| 86 | + && inner_op.node == BinOpKind::Add |
| 87 | + && sub_op.node == BinOpKind::Sub |
| 88 | + && check_literal(sub_rhs) |
| 89 | + && check_eq_expr(cx, sub_lhs, div_rhs) |
| 90 | + { |
| 91 | + build_suggestion(cx, expr, inner_rhs, div_rhs, &mut applicability); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + // (x + y - 1) / y |
| 96 | + if let ExprKind::Binary(add_op, add_lhs, add_rhs) = inner_lhs.kind |
| 97 | + && inner_op.node == BinOpKind::Sub |
| 98 | + && add_op.node == BinOpKind::Add |
| 99 | + && check_literal(inner_rhs) |
| 100 | + && check_eq_expr(cx, add_rhs, div_rhs) |
| 101 | + { |
| 102 | + build_suggestion(cx, expr, add_lhs, div_rhs, &mut applicability); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + extract_msrv_attr!(LateContext); |
| 108 | +} |
| 109 | + |
| 110 | +fn check_int_ty_and_feature(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { |
| 111 | + let expr_ty = cx.typeck_results().expr_ty(expr); |
| 112 | + match expr_ty.peel_refs().kind() { |
| 113 | + ty::Uint(_) => true, |
| 114 | + ty::Int(_) => cx |
| 115 | + .tcx |
| 116 | + .features() |
| 117 | + .declared_features |
| 118 | + .contains(&Symbol::intern("int_roundings")), |
| 119 | + |
| 120 | + _ => false, |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +fn check_literal(expr: &Expr<'_>) -> bool { |
| 125 | + if let ExprKind::Lit(lit) = expr.kind |
| 126 | + && let LitKind::Int(Pu128(1), _) = lit.node |
| 127 | + { |
| 128 | + return true; |
| 129 | + } |
| 130 | + false |
| 131 | +} |
| 132 | + |
| 133 | +fn check_eq_expr(cx: &LateContext<'_>, lhs: &Expr<'_>, rhs: &Expr<'_>) -> bool { |
| 134 | + SpanlessEq::new(cx).eq_expr(lhs, rhs) |
| 135 | +} |
| 136 | + |
| 137 | +fn build_suggestion( |
| 138 | + cx: &LateContext<'_>, |
| 139 | + expr: &Expr<'_>, |
| 140 | + lhs: &Expr<'_>, |
| 141 | + rhs: &Expr<'_>, |
| 142 | + applicability: &mut Applicability, |
| 143 | +) { |
| 144 | + let dividend_sugg = Sugg::hir_with_applicability(cx, lhs, "..", applicability).maybe_par(); |
| 145 | + let divisor_snippet = snippet_with_applicability(cx, rhs.span.source_callsite(), "..", applicability); |
| 146 | + |
| 147 | + let sugg = format!("{dividend_sugg}.div_ceil({divisor_snippet})"); |
| 148 | + |
| 149 | + span_lint_and_sugg( |
| 150 | + cx, |
| 151 | + MANUAL_DIV_CEIL, |
| 152 | + expr.span, |
| 153 | + "manually reimplementing `div_ceil`", |
| 154 | + "consider using `.div_ceil()`", |
| 155 | + sugg, |
| 156 | + *applicability, |
| 157 | + ); |
| 158 | +} |
0 commit comments