|
| 1 | +use clippy_config::Conf; |
| 2 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 3 | +use clippy_utils::msrvs::{self, Msrv}; |
| 4 | +use clippy_utils::res::{MaybeDef, MaybeTypeckRes}; |
| 5 | +use clippy_utils::source::snippet_with_applicability; |
| 6 | +use clippy_utils::{is_from_proc_macro, sym}; |
| 7 | +use rustc_ast::LitKind; |
| 8 | +use rustc_data_structures::packed::Pu128; |
| 9 | +use rustc_errors::Applicability; |
| 10 | +use rustc_hir::{BinOpKind, Expr, ExprKind}; |
| 11 | +use rustc_lint::{LateContext, LateLintPass, LintContext}; |
| 12 | +use rustc_middle::ty; |
| 13 | +use rustc_session::impl_lint_pass; |
| 14 | + |
| 15 | +declare_clippy_lint! { |
| 16 | + /// ### What it does |
| 17 | + /// Checks for expressions like `31 - x.leading_zeros()` or `x.ilog(2)` which are manual |
| 18 | + /// reimplementations of `x.ilog2()` |
| 19 | + /// |
| 20 | + /// ### Why is this bad? |
| 21 | + /// Manual reimplementations of `ilog2` increase code complexity for little benefit. |
| 22 | + /// |
| 23 | + /// ### Example |
| 24 | + /// ```no_run |
| 25 | + /// let x: u32 = 5; |
| 26 | + /// let log = 31 - x.leading_zeros(); |
| 27 | + /// let log = x.ilog(2); |
| 28 | + /// ``` |
| 29 | + /// Use instead: |
| 30 | + /// ```no_run |
| 31 | + /// let x: u32 = 5; |
| 32 | + /// let log = x.ilog2(); |
| 33 | + /// let log = x.ilog2(); |
| 34 | + /// ``` |
| 35 | + #[clippy::version = "1.92.0"] |
| 36 | + pub MANUAL_ILOG2, |
| 37 | + complexity, |
| 38 | + "manually reimplementing `ilog2`" |
| 39 | +} |
| 40 | + |
| 41 | +pub struct ManualIlog2 { |
| 42 | + msrv: Msrv, |
| 43 | +} |
| 44 | + |
| 45 | +impl ManualIlog2 { |
| 46 | + pub fn new(conf: &Conf) -> Self { |
| 47 | + Self { msrv: conf.msrv } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +impl_lint_pass!(ManualIlog2 => [MANUAL_ILOG2]); |
| 52 | + |
| 53 | +impl LateLintPass<'_> for ManualIlog2 { |
| 54 | + fn check_expr<'tcx>(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) { |
| 55 | + if expr.span.in_external_macro(cx.sess().source_map()) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + match expr.kind { |
| 60 | + // `BIT_WIDTH - 1 - n.leading_zeros()` |
| 61 | + ExprKind::Binary(op, left, right) |
| 62 | + if left.span.eq_ctxt(right.span) |
| 63 | + && op.node == BinOpKind::Sub |
| 64 | + && let ExprKind::Lit(lit) = left.kind |
| 65 | + && let LitKind::Int(Pu128(val), _) = lit.node |
| 66 | + && let ExprKind::MethodCall(leading_zeros, recv, [], _) = right.kind |
| 67 | + && leading_zeros.ident.name == sym::leading_zeros |
| 68 | + // Whether `leading_zeros` is an inherent method, i.e. doesn't come from some unrelated trait |
| 69 | + && cx.ty_based_def(right).opt_parent(cx).is_impl(cx) |
| 70 | + && let ty = cx.typeck_results().expr_ty(recv) |
| 71 | + && let Some(bit_width) = match ty.kind() { |
| 72 | + ty::Int(int_ty) => int_ty.bit_width(), |
| 73 | + ty::Uint(uint_ty) => uint_ty.bit_width(), |
| 74 | + _ => return, |
| 75 | + } |
| 76 | + && val == u128::from(bit_width) - 1 |
| 77 | + && self.msrv.meets(cx, msrvs::ILOG2) |
| 78 | + && !is_from_proc_macro(cx, expr) => |
| 79 | + { |
| 80 | + emit(cx, recv, expr); |
| 81 | + }, |
| 82 | + |
| 83 | + // `n.ilog(2)` |
| 84 | + ExprKind::MethodCall(ilog, recv, [two], _) |
| 85 | + if expr.span.eq_ctxt(two.span) |
| 86 | + && ilog.ident.name == sym::ilog |
| 87 | + // Whether `ilog` is an inherent method, i.e. doesn't come from some unrelated trait |
| 88 | + && cx.ty_based_def(expr).opt_parent(cx).is_impl(cx) |
| 89 | + && let ExprKind::Lit(lit) = two.kind |
| 90 | + && let LitKind::Int(Pu128(2), _) = lit.node |
| 91 | + && cx.typeck_results().expr_ty(recv).is_integral() |
| 92 | + && self.msrv.meets(cx, msrvs::ILOG2) |
| 93 | + && !is_from_proc_macro(cx, expr) => |
| 94 | + { |
| 95 | + emit(cx, recv, expr); |
| 96 | + }, |
| 97 | + |
| 98 | + _ => {}, |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +fn emit(cx: &LateContext<'_>, recv: &Expr<'_>, full_expr: &Expr<'_>) { |
| 104 | + let mut app = Applicability::MachineApplicable; |
| 105 | + let recv = snippet_with_applicability(cx, recv.span, "_", &mut app); |
| 106 | + span_lint_and_sugg( |
| 107 | + cx, |
| 108 | + MANUAL_ILOG2, |
| 109 | + full_expr.span, |
| 110 | + "manually reimplementing `ilog2`", |
| 111 | + "try", |
| 112 | + format!("{recv}.ilog2()"), |
| 113 | + app, |
| 114 | + ); |
| 115 | +} |
0 commit comments