|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::source::snippet_with_applicability; |
| 3 | +use clippy_utils::SpanlessEq; |
| 4 | +use rustc_ast::LitKind; |
| 5 | +use rustc_data_structures::packed::Pu128; |
| 6 | +use rustc_errors::Applicability; |
| 7 | +use rustc_hir::{BinOpKind, Expr, ExprKind}; |
| 8 | +use rustc_lint::{LateContext, LateLintPass}; |
| 9 | +use rustc_middle::ty::Uint; |
| 10 | +use rustc_session::declare_lint_pass; |
| 11 | + |
| 12 | +declare_clippy_lint! { |
| 13 | + /// ### What it does |
| 14 | + /// Checks for expressions like `x.count_ones() == 1` or `x & (x - 1) == 0`, with x and unsigned integer, which are manual |
| 15 | + /// reimplementations of `x.is_power_of_two()`. |
| 16 | + /// ### Why is this bad? |
| 17 | + /// Manual reimplementations of `is_power_of_two` increase code complexity for little benefit. |
| 18 | + /// ### Example |
| 19 | + /// ```no_run |
| 20 | + /// let a: u32 = 4; |
| 21 | + /// let result = a.count_ones() == 1; |
| 22 | + /// ``` |
| 23 | + /// Use instead: |
| 24 | + /// ```no_run |
| 25 | + /// let a: u32 = 4; |
| 26 | + /// let result = a.is_power_of_two(); |
| 27 | + /// ``` |
| 28 | + #[clippy::version = "1.82.0"] |
| 29 | + pub MANUAL_IS_POWER_OF_TWO, |
| 30 | + complexity, |
| 31 | + "manually reimplementing `is_power_of_two`" |
| 32 | +} |
| 33 | + |
| 34 | +declare_lint_pass!(ManualIsPowerOfTwo => [MANUAL_IS_POWER_OF_TWO]); |
| 35 | + |
| 36 | +impl LateLintPass<'_> for ManualIsPowerOfTwo { |
| 37 | + fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { |
| 38 | + let mut applicability = Applicability::MachineApplicable; |
| 39 | + |
| 40 | + if let ExprKind::Binary(bin_op, left, right) = expr.kind |
| 41 | + && bin_op.node == BinOpKind::Eq |
| 42 | + { |
| 43 | + // a.count_ones() == 1 |
| 44 | + if let ExprKind::MethodCall(method_name, reciever, _, _) = left.kind |
| 45 | + && method_name.ident.as_str() == "count_ones" |
| 46 | + && let &Uint(_) = cx.typeck_results().expr_ty(reciever).kind() |
| 47 | + && check_lit(right, 1) |
| 48 | + { |
| 49 | + build_sugg(cx, expr, reciever, &mut applicability); |
| 50 | + } |
| 51 | + |
| 52 | + // 1 == a.count_ones() |
| 53 | + if let ExprKind::MethodCall(method_name, reciever, _, _) = right.kind |
| 54 | + && method_name.ident.as_str() == "count_ones" |
| 55 | + && let &Uint(_) = cx.typeck_results().expr_ty(reciever).kind() |
| 56 | + && check_lit(left, 1) |
| 57 | + { |
| 58 | + build_sugg(cx, expr, reciever, &mut applicability); |
| 59 | + } |
| 60 | + |
| 61 | + // a & (a - 1) == 0 |
| 62 | + if let ExprKind::Binary(op1, left1, right1) = left.kind |
| 63 | + && op1.node == BinOpKind::BitAnd |
| 64 | + && let ExprKind::Binary(op2, left2, right2) = right1.kind |
| 65 | + && op2.node == BinOpKind::Sub |
| 66 | + && check_eq_expr(cx, left1, left2) |
| 67 | + && let &Uint(_) = cx.typeck_results().expr_ty(left1).kind() |
| 68 | + && check_lit(right2, 1) |
| 69 | + && check_lit(right, 0) |
| 70 | + { |
| 71 | + build_sugg(cx, expr, left1, &mut applicability); |
| 72 | + } |
| 73 | + |
| 74 | + // (a - 1) & a == 0; |
| 75 | + if let ExprKind::Binary(op1, left1, right1) = left.kind |
| 76 | + && op1.node == BinOpKind::BitAnd |
| 77 | + && let ExprKind::Binary(op2, left2, right2) = left1.kind |
| 78 | + && op2.node == BinOpKind::Sub |
| 79 | + && check_eq_expr(cx, right1, left2) |
| 80 | + && let &Uint(_) = cx.typeck_results().expr_ty(right1).kind() |
| 81 | + && check_lit(right2, 1) |
| 82 | + && check_lit(right, 0) |
| 83 | + { |
| 84 | + build_sugg(cx, expr, right1, &mut applicability); |
| 85 | + } |
| 86 | + |
| 87 | + // 0 == a & (a - 1); |
| 88 | + if let ExprKind::Binary(op1, left1, right1) = right.kind |
| 89 | + && op1.node == BinOpKind::BitAnd |
| 90 | + && let ExprKind::Binary(op2, left2, right2) = right1.kind |
| 91 | + && op2.node == BinOpKind::Sub |
| 92 | + && check_eq_expr(cx, left1, left2) |
| 93 | + && let &Uint(_) = cx.typeck_results().expr_ty(left1).kind() |
| 94 | + && check_lit(right2, 1) |
| 95 | + && check_lit(left, 0) |
| 96 | + { |
| 97 | + build_sugg(cx, expr, left1, &mut applicability); |
| 98 | + } |
| 99 | + |
| 100 | + // 0 == (a - 1) & a |
| 101 | + if let ExprKind::Binary(op1, left1, right1) = right.kind |
| 102 | + && op1.node == BinOpKind::BitAnd |
| 103 | + && let ExprKind::Binary(op2, left2, right2) = left1.kind |
| 104 | + && op2.node == BinOpKind::Sub |
| 105 | + && check_eq_expr(cx, right1, left2) |
| 106 | + && let &Uint(_) = cx.typeck_results().expr_ty(right1).kind() |
| 107 | + && check_lit(right2, 1) |
| 108 | + && check_lit(left, 0) |
| 109 | + { |
| 110 | + build_sugg(cx, expr, right1, &mut applicability); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +fn build_sugg(cx: &LateContext<'_>, expr: &Expr<'_>, reciever: &Expr<'_>, applicability: &mut Applicability) { |
| 117 | + let snippet = snippet_with_applicability(cx, reciever.span, "..", applicability); |
| 118 | + |
| 119 | + span_lint_and_sugg( |
| 120 | + cx, |
| 121 | + MANUAL_IS_POWER_OF_TWO, |
| 122 | + expr.span, |
| 123 | + "manually reimplementing `is_power_of_two`", |
| 124 | + "consider using `.is_power_of_two()`", |
| 125 | + format!("{snippet}.is_power_of_two()"), |
| 126 | + *applicability, |
| 127 | + ); |
| 128 | +} |
| 129 | + |
| 130 | +fn check_lit(expr: &Expr<'_>, expected_num: u128) -> bool { |
| 131 | + if let ExprKind::Lit(lit) = expr.kind |
| 132 | + && let LitKind::Int(Pu128(num), _) = lit.node |
| 133 | + && num == expected_num |
| 134 | + { |
| 135 | + return true; |
| 136 | + } |
| 137 | + false |
| 138 | +} |
| 139 | + |
| 140 | +fn check_eq_expr(cx: &LateContext<'_>, lhs: &Expr<'_>, rhs: &Expr<'_>) -> bool { |
| 141 | + SpanlessEq::new(cx).eq_expr(lhs, rhs) |
| 142 | +} |
0 commit comments