|
| 1 | +use std::fmt::Display; |
| 2 | + |
| 3 | +use clippy_utils::consts::{constant, Constant}; |
| 4 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 5 | +use clippy_utils::sugg; |
| 6 | +use rustc_errors::Applicability; |
| 7 | +use rustc_hir::{BinOpKind, Expr, ExprKind}; |
| 8 | +use rustc_lint::{LateContext, LateLintPass}; |
| 9 | +use rustc_middle::ty; |
| 10 | +use rustc_session::declare_lint_pass; |
| 11 | + |
| 12 | +declare_clippy_lint! { |
| 13 | + /// ### What it does |
| 14 | + /// |
| 15 | + /// It detects manual bit rotations that could be rewritten using standard |
| 16 | + /// functions `rotate_left` or `rotate_right`. |
| 17 | + /// |
| 18 | + /// ### Why is this bad? |
| 19 | + /// |
| 20 | + /// Calling the function better conveys the intent. |
| 21 | + /// |
| 22 | + /// ### Known issues |
| 23 | + /// |
| 24 | + /// Currently, the lint only catches shifts by constant amount. |
| 25 | + /// |
| 26 | + /// ### Example |
| 27 | + /// ```no_run |
| 28 | + /// let x = 12345678_u32; |
| 29 | + /// let _ = (x >> 8) | (x << 24); |
| 30 | + /// ``` |
| 31 | + /// Use instead: |
| 32 | + /// ```no_run |
| 33 | + /// let x = 12345678_u32; |
| 34 | + /// let _ = x.rotate_right(8); |
| 35 | + /// ``` |
| 36 | + #[clippy::version = "1.81.0"] |
| 37 | + pub MANUAL_ROTATE, |
| 38 | + style, |
| 39 | + "using bit shifts to rotate integers" |
| 40 | +} |
| 41 | + |
| 42 | +declare_lint_pass!(ManualRotate => [MANUAL_ROTATE]); |
| 43 | + |
| 44 | +#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 45 | +enum ShiftDirection { |
| 46 | + Left, |
| 47 | + Right, |
| 48 | +} |
| 49 | + |
| 50 | +impl Display for ShiftDirection { |
| 51 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 52 | + f.write_str(match self { |
| 53 | + Self::Left => "rotate_left", |
| 54 | + Self::Right => "rotate_right", |
| 55 | + }) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +fn parse_shift<'tcx>( |
| 60 | + cx: &LateContext<'tcx>, |
| 61 | + expr: &'tcx Expr<'tcx>, |
| 62 | +) -> Option<(ShiftDirection, u128, &'tcx Expr<'tcx>)> { |
| 63 | + if let ExprKind::Binary(op, l, r) = expr.kind { |
| 64 | + let dir = match op.node { |
| 65 | + BinOpKind::Shl => ShiftDirection::Left, |
| 66 | + BinOpKind::Shr => ShiftDirection::Right, |
| 67 | + _ => return None, |
| 68 | + }; |
| 69 | + let const_expr = constant(cx, cx.typeck_results(), r)?; |
| 70 | + if let Constant::Int(shift) = const_expr { |
| 71 | + return Some((dir, shift, l)); |
| 72 | + } |
| 73 | + } |
| 74 | + None |
| 75 | +} |
| 76 | + |
| 77 | +impl LateLintPass<'_> for ManualRotate { |
| 78 | + fn check_expr<'tcx>(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) { |
| 79 | + if let ExprKind::Binary(op, l, r) = expr.kind |
| 80 | + && let BinOpKind::Add | BinOpKind::BitOr = op.node |
| 81 | + && let Some((l_shift_dir, l_amount, l_expr)) = parse_shift(cx, l) |
| 82 | + && let Some((r_shift_dir, r_amount, r_expr)) = parse_shift(cx, r) |
| 83 | + { |
| 84 | + if l_shift_dir == r_shift_dir { |
| 85 | + return; |
| 86 | + } |
| 87 | + if !clippy_utils::eq_expr_value(cx, l_expr, r_expr) { |
| 88 | + return; |
| 89 | + } |
| 90 | + let Some(bit_width) = (match cx.typeck_results().expr_ty(expr).kind() { |
| 91 | + ty::Int(itype) => itype.bit_width(), |
| 92 | + ty::Uint(itype) => itype.bit_width(), |
| 93 | + _ => return, |
| 94 | + }) else { |
| 95 | + return; |
| 96 | + }; |
| 97 | + if l_amount + r_amount == u128::from(bit_width) { |
| 98 | + let (shift_function, amount) = if l_amount < r_amount { |
| 99 | + (l_shift_dir, l_amount) |
| 100 | + } else { |
| 101 | + (r_shift_dir, r_amount) |
| 102 | + }; |
| 103 | + let mut applicability = Applicability::MachineApplicable; |
| 104 | + let expr_sugg = sugg::Sugg::hir_with_applicability(cx, l_expr, "_", &mut applicability).maybe_par(); |
| 105 | + span_lint_and_sugg( |
| 106 | + cx, |
| 107 | + MANUAL_ROTATE, |
| 108 | + expr.span, |
| 109 | + "there is no need to manually implement bit rotation", |
| 110 | + "this expression can be rewritten as", |
| 111 | + format!("{expr_sugg}.{shift_function}({amount})"), |
| 112 | + Applicability::MachineApplicable, |
| 113 | + ); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments