|
| 1 | +use clippy_utils::diagnostics::span_lint; |
| 2 | +use clippy_utils::source::snippet_opt; |
| 3 | +use rustc_hir::*; |
| 4 | +use rustc_lint::{LateContext, LateLintPass}; |
| 5 | +use rustc_session::declare_lint_pass; |
| 6 | +use rustc_span::source_map::Spanned; |
| 7 | + |
| 8 | +declare_clippy_lint! { |
| 9 | + /// ### What it does |
| 10 | + /// Checks for decimal literals used as bit masks in bitwise operations. |
| 11 | + /// |
| 12 | + /// ### Why is this bad? |
| 13 | + /// Using decimal literals for bit masks can make the code less readable and obscure the intended bit pattern. |
| 14 | + /// Binary or hexadecimal literals make the bit pattern more explicit and easier to understand at a glance. |
| 15 | + /// |
| 16 | + /// ### Example |
| 17 | + /// ```rust,no_run |
| 18 | + /// let a = 15 & 6; // Bit pattern is not immediately clear |
| 19 | + /// ``` |
| 20 | + /// Use instead: |
| 21 | + /// ```rust,no_run |
| 22 | + /// let a = 0b1111 & 0b0110; |
| 23 | + /// ``` |
| 24 | + #[clippy::version = "1.87.0"] |
| 25 | + pub DECIMAL_BIT_MASK, |
| 26 | + nursery, |
| 27 | + "default lint description" |
| 28 | +} |
| 29 | + |
| 30 | +declare_lint_pass!(DecimalBitMask => [DECIMAL_BIT_MASK]); |
| 31 | + |
| 32 | +impl<'tcx> LateLintPass<'tcx> for DecimalBitMask { |
| 33 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) { |
| 34 | + if let ExprKind::Binary( |
| 35 | + Spanned { |
| 36 | + node: BinOpKind::BitAnd | BinOpKind::BitOr | BinOpKind::BitXor, |
| 37 | + .. |
| 38 | + }, |
| 39 | + Expr { |
| 40 | + kind: kind1, |
| 41 | + span: span1, |
| 42 | + .. |
| 43 | + }, |
| 44 | + Expr { |
| 45 | + kind: kind2, |
| 46 | + span: span2, |
| 47 | + .. |
| 48 | + }, |
| 49 | + ) = &e.kind |
| 50 | + { |
| 51 | + if let ExprKind::Lit(_) = kind1 |
| 52 | + && let Some(snippet) = snippet_opt(cx, *span1) |
| 53 | + && !snippet.starts_with("0b") |
| 54 | + && !snippet.starts_with("0x") |
| 55 | + { |
| 56 | + span_lint( |
| 57 | + cx, |
| 58 | + DECIMAL_BIT_MASK, |
| 59 | + e.span, |
| 60 | + "Using decimal literal for bit mask. Consider using binary (0b...) or hexadecimal (0x...) notation for better readability.", |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + if let ExprKind::Lit(_) = kind2 |
| 65 | + && let Some(snippet) = snippet_opt(cx, *span2) |
| 66 | + && !snippet.starts_with("0b") |
| 67 | + && !snippet.starts_with("0x") |
| 68 | + { |
| 69 | + span_lint( |
| 70 | + cx, |
| 71 | + DECIMAL_BIT_MASK, |
| 72 | + e.span, |
| 73 | + "Using decimal literal for bit mask. Consider using binary (0b...) or hexadecimal (0x...) notation for better readability.", |
| 74 | + ); |
| 75 | + } |
| 76 | + } |
| 77 | + if let ExprKind::AssignOp( |
| 78 | + Spanned { |
| 79 | + node: AssignOpKind::BitAndAssign | AssignOpKind::BitOrAssign | AssignOpKind::BitXorAssign, |
| 80 | + .. |
| 81 | + }, |
| 82 | + _, |
| 83 | + Expr { |
| 84 | + kind: ExprKind::Lit(_), |
| 85 | + span, |
| 86 | + .. |
| 87 | + }, |
| 88 | + ) = &e.kind |
| 89 | + { |
| 90 | + if let Some(snippet) = snippet_opt(cx, *span) |
| 91 | + && !snippet.starts_with("0b") |
| 92 | + && !snippet.starts_with("0x") |
| 93 | + { |
| 94 | + span_lint( |
| 95 | + cx, |
| 96 | + DECIMAL_BIT_MASK, |
| 97 | + e.span, |
| 98 | + "Using decimal literal for bit mask. Consider using binary (0b...) or hexadecimal (0x...) notation for better readability.", |
| 99 | + ); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments