|
| 1 | +use clippy_config::msrvs::{self, Msrv}; |
| 2 | +use clippy_config::Conf; |
| 3 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 4 | +use clippy_utils::source::snippet_with_applicability; |
| 5 | +use rustc_ast::LitKind; |
| 6 | +use rustc_data_structures::packed::Pu128; |
| 7 | +use rustc_errors::Applicability; |
| 8 | +use rustc_hir::{BinOpKind, Expr, ExprKind}; |
| 9 | +use rustc_lint::{LateContext, LateLintPass}; |
| 10 | +use rustc_middle::ty; |
| 11 | +use rustc_session::impl_lint_pass; |
| 12 | + |
| 13 | +declare_clippy_lint! { |
| 14 | + /// ### What it does |
| 15 | + /// Checks for expressions like `31 - x.leading_zeros()` or `x.ilog(2)` which are manual |
| 16 | + /// reimplementations of `x.ilog2()` |
| 17 | + /// ### Why is this bad? |
| 18 | + /// It is easier to read and understand |
| 19 | + /// ### Example |
| 20 | + /// ```no_run |
| 21 | + /// let x: u32 = 5; |
| 22 | + /// let log = 31 - x.leading_zeros() |
| 23 | + /// ``` |
| 24 | + /// Use instead: |
| 25 | + /// ```no_run |
| 26 | + /// let x: u32 = 5; |
| 27 | + /// let log = x.ilog2() |
| 28 | + /// ``` |
| 29 | + #[clippy::version = "1.82.0"] |
| 30 | + pub MANUAL_ILOG2, |
| 31 | + complexity, |
| 32 | + "manually reimplementing `ilog2`" |
| 33 | +} |
| 34 | + |
| 35 | +pub struct ManualIlog2 { |
| 36 | + msrv: Msrv, |
| 37 | +} |
| 38 | + |
| 39 | +impl ManualIlog2 { |
| 40 | + #[must_use] |
| 41 | + pub fn new(conf: &Conf) -> Self { |
| 42 | + Self { |
| 43 | + msrv: conf.msrv.clone(), |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +impl_lint_pass!(ManualIlog2 => [MANUAL_ILOG2]); |
| 49 | + |
| 50 | +impl LateLintPass<'_> for ManualIlog2 { |
| 51 | + fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { |
| 52 | + if !self.msrv.meets(msrvs::MANUAL_ILOG2) { |
| 53 | + return; |
| 54 | + } |
| 55 | + let mut applicability = Applicability::MachineApplicable; |
| 56 | + |
| 57 | + if let ExprKind::Binary(op, left, right) = expr.kind |
| 58 | + && BinOpKind::Sub == op.node |
| 59 | + && let ExprKind::Lit(lit) = left.kind |
| 60 | + && let LitKind::Int(Pu128(val), _) = lit.node |
| 61 | + && let ExprKind::MethodCall(method_name, reciever, _, _) = right.kind |
| 62 | + && method_name.ident.as_str() == "leading_zeros" |
| 63 | + { |
| 64 | + let type_ = cx.typeck_results().expr_ty(reciever); |
| 65 | + let Some(bit_width) = (match type_.kind() { |
| 66 | + ty::Int(itype) => itype.bit_width(), |
| 67 | + ty::Uint(itype) => itype.bit_width(), |
| 68 | + _ => return, |
| 69 | + }) else { |
| 70 | + return; |
| 71 | + }; |
| 72 | + if val == u128::from(bit_width) - 1 { |
| 73 | + let sugg = snippet_with_applicability(cx, reciever.span, "..", &mut applicability); |
| 74 | + span_lint_and_sugg( |
| 75 | + cx, |
| 76 | + MANUAL_ILOG2, |
| 77 | + expr.span, |
| 78 | + "manually reimplementing `ilog2`", |
| 79 | + "consider using .ilog2()", |
| 80 | + format!("{sugg}.ilog2()"), |
| 81 | + applicability, |
| 82 | + ); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if let ExprKind::MethodCall(method_name, reciever, args, _) = expr.kind |
| 87 | + && method_name.ident.as_str() == "ilog" |
| 88 | + && args.len() == 1 |
| 89 | + && let ExprKind::Lit(lit) = args[0].kind |
| 90 | + && let LitKind::Int(Pu128(2), _) = lit.node |
| 91 | + && cx.typeck_results().expr_ty(reciever).is_integral() |
| 92 | + { |
| 93 | + let sugg = snippet_with_applicability(cx, reciever.span, "..", &mut applicability); |
| 94 | + span_lint_and_sugg( |
| 95 | + cx, |
| 96 | + MANUAL_ILOG2, |
| 97 | + expr.span, |
| 98 | + "manually reimplementing `ilog2`", |
| 99 | + "consider using .ilog2()", |
| 100 | + format!("{sugg}.ilog2()"), |
| 101 | + applicability, |
| 102 | + ); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments