|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::source::snippet_with_applicability; |
| 3 | +use rustc_ast::LitKind; |
| 4 | +use rustc_data_structures::packed::Pu128; |
| 5 | +use rustc_errors::Applicability; |
| 6 | +use rustc_hir::{BinOpKind, Expr, ExprKind}; |
| 7 | +use rustc_lint::{LateContext, LateLintPass}; |
| 8 | +use rustc_middle::ty::Uint; |
| 9 | +use rustc_session::declare_lint_pass; |
| 10 | + |
| 11 | +declare_clippy_lint! { |
| 12 | + /// ### What it does |
| 13 | + /// Checks for expressions like `x.count_ones() == 1` or `x & (x - 1) == 0` which are manual |
| 14 | + /// reimplementations of `x.is_power_of_two()`` |
| 15 | + /// ### Why is this bad? |
| 16 | + /// It's simpler and clearer |
| 17 | + /// ### Example |
| 18 | + /// ```no_run |
| 19 | + /// let x: u32 = 1; |
| 20 | + /// let result = x.count_ones() == 1; |
| 21 | + /// ``` |
| 22 | + /// Use instead: |
| 23 | + /// ```no_run |
| 24 | + /// let x: u32 = 1; |
| 25 | + /// let result = x.is_power_of_two(); |
| 26 | + /// ``` |
| 27 | + #[clippy::version = "1.82.0"] |
| 28 | + pub MANUAL_IS_POWER_OF_TWO, |
| 29 | + complexity, |
| 30 | + "manually reimplementing `is_power_of_two`" |
| 31 | +} |
| 32 | + |
| 33 | +declare_lint_pass!(ManualIsPowerOfTwo => [MANUAL_IS_POWER_OF_TWO]); |
| 34 | + |
| 35 | +impl LateLintPass<'_> for ManualIsPowerOfTwo { |
| 36 | + fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { |
| 37 | + let mut applicability = Applicability::MachineApplicable; |
| 38 | + |
| 39 | + // x.count_ones() == 1 |
| 40 | + if let ExprKind::Binary(op, left, right) = expr.kind |
| 41 | + && BinOpKind::Eq == op.node |
| 42 | + && let ExprKind::MethodCall(method_name, reciever, _, _) = left.kind |
| 43 | + && method_name.ident.as_str() == "count_ones" |
| 44 | + && let ExprKind::Lit(lit) = right.kind |
| 45 | + && let LitKind::Int(Pu128(1), _) = lit.node |
| 46 | + && let &Uint(_) = cx.typeck_results().expr_ty(reciever).kind() |
| 47 | + { |
| 48 | + let snippet = snippet_with_applicability(cx, reciever.span, "..", &mut applicability); |
| 49 | + let sugg = format!("{snippet}.is_power_of_two()"); |
| 50 | + span_lint_and_sugg( |
| 51 | + cx, |
| 52 | + MANUAL_IS_POWER_OF_TWO, |
| 53 | + expr.span, |
| 54 | + "manually reimplementing `is_power_of_two`", |
| 55 | + "consider using `.is_power_of_two()`", |
| 56 | + sugg, |
| 57 | + applicability, |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + // x & (x - 1) == 0 |
| 62 | + if let ExprKind::Binary(op, left, right) = expr.kind |
| 63 | + && BinOpKind::Eq == op.node |
| 64 | + && let ExprKind::Binary(op1, left1, right1) = left.kind |
| 65 | + && BinOpKind::BitAnd == op1.node |
| 66 | + && let ExprKind::Binary(op2, left2, right2) = right1.kind |
| 67 | + && BinOpKind::Sub == op2.node |
| 68 | + && left1.span.eq_ctxt(left2.span) |
| 69 | + && let &Uint(_) = cx.typeck_results().expr_ty(left1).kind() |
| 70 | + && let ExprKind::Lit(lit) = right2.kind |
| 71 | + && let LitKind::Int(Pu128(1), _) = lit.node |
| 72 | + && let ExprKind::Lit(lit1) = right.kind |
| 73 | + && let LitKind::Int(Pu128(0), _) = lit1.node |
| 74 | + { |
| 75 | + let snippet = snippet_with_applicability(cx, left1.span, "..", &mut applicability); |
| 76 | + let sugg = format!("{snippet}.is_power_of_two()"); |
| 77 | + span_lint_and_sugg( |
| 78 | + cx, |
| 79 | + MANUAL_IS_POWER_OF_TWO, |
| 80 | + expr.span, |
| 81 | + "manually reimplementing `is_power_of_two`", |
| 82 | + "consider using `.is_power_of_two()`", |
| 83 | + sugg, |
| 84 | + applicability, |
| 85 | + ); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments