Skip to content

Commit a6c0cfd

Browse files
committed
Move the lint to operators category
1 parent d4a31bc commit a6c0cfd

File tree

4 files changed

+36
-35
lines changed

4 files changed

+36
-35
lines changed

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ pub static LINTS: &[&::declare_clippy_lint::LintInfo] = &[
8888
crate::crate_in_macro_def::CRATE_IN_MACRO_DEF_INFO,
8989
crate::create_dir::CREATE_DIR_INFO,
9090
crate::dbg_macro::DBG_MACRO_INFO,
91-
crate::decimal_bitwise_operands::DECIMAL_BITWISE_OPERANDS_INFO,
9291
crate::default::DEFAULT_TRAIT_ACCESS_INFO,
9392
crate::default::FIELD_REASSIGN_WITH_DEFAULT_INFO,
9493
crate::default_constructed_unit_structs::DEFAULT_CONSTRUCTED_UNIT_STRUCTS_INFO,
@@ -579,6 +578,7 @@ pub static LINTS: &[&::declare_clippy_lint::LintInfo] = &[
579578
crate::operators::ASSIGN_OP_PATTERN_INFO,
580579
crate::operators::BAD_BIT_MASK_INFO,
581580
crate::operators::CMP_OWNED_INFO,
581+
crate::operators::DECIMAL_BITWISE_OPERANDS_INFO,
582582
crate::operators::DOUBLE_COMPARISONS_INFO,
583583
crate::operators::DURATION_SUBSEC_INFO,
584584
crate::operators::EQ_OP_INFO,

clippy_lints/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ mod copy_iterator;
9696
mod crate_in_macro_def;
9797
mod create_dir;
9898
mod dbg_macro;
99-
mod decimal_bitwise_operands;
10099
mod default;
101100
mod default_constructed_unit_structs;
102101
mod default_instead_of_iter_empty;
@@ -846,7 +845,6 @@ pub fn register_lint_passes(store: &mut rustc_lint::LintStore, conf: &'static Co
846845
Box::new(|_| Box::new(cloned_ref_to_slice_refs::ClonedRefToSliceRefs::new(conf))),
847846
Box::new(|_| Box::new(infallible_try_from::InfallibleTryFrom)),
848847
Box::new(|_| Box::new(coerce_container_to_any::CoerceContainerToAny)),
849-
Box::new(|_| Box::new(decimal_bitwise_operands::DecimalBitwiseOperands)),
850848
Box::new(|_| Box::new(toplevel_ref_arg::ToplevelRefArg)),
851849
Box::new(|_| Box::new(volatile_composites::VolatileComposites)),
852850
Box::new(|_| Box::<replace_box::ReplaceBox>::default()),

clippy_lints/src/decimal_bitwise_operands.rs renamed to clippy_lints/src/operators/decimal_bitwise_operands.rs

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,11 @@ use clippy_utils::source::SpanRangeExt;
33
use rustc_ast::LitKind;
44
use rustc_data_structures::packed::Pu128;
55
use rustc_hir::{AssignOpKind, BinOpKind, Expr, ExprKind};
6-
use rustc_lint::{LateContext, LateLintPass};
7-
use rustc_session::declare_lint_pass;
6+
use rustc_lint::LateContext;
87
use rustc_span::Span;
98
use rustc_span::source_map::Spanned;
109

11-
declare_clippy_lint! {
12-
/// ### What it does
13-
/// Checks for decimal literals used as bit masks in bitwise operations.
14-
///
15-
/// ### Why is this bad?
16-
/// Using decimal literals for bit masks can make the code less readable and obscure the intended bit pattern.
17-
/// Binary, hexadecimal, or octal literals make the bit pattern more explicit and easier to understand at a glance.
18-
///
19-
/// ### Example
20-
/// ```rust,no_run
21-
/// let a = 14 & 6; // Bit pattern is not immediately clear
22-
/// ```
23-
/// Use instead:
24-
/// ```rust,no_run
25-
/// let a = 0b1110 & 0b0110;
26-
/// ```
27-
#[clippy::version = "1.92.0"]
28-
pub DECIMAL_BITWISE_OPERANDS,
29-
nursery,
30-
"use binary, hex, or octal literals for bitwise operations"
31-
}
32-
33-
declare_lint_pass!(DecimalBitwiseOperands => [DECIMAL_BITWISE_OPERANDS]);
10+
use super::DECIMAL_BITWISE_OPERANDS;
3411

3512
fn check_bitwise_binary_expr(cx: &LateContext<'_>, expr: &Expr<'_>) {
3613
if let ExprKind::Binary(op, left, right) = &expr.kind
@@ -85,12 +62,10 @@ fn emit_lint(cx: &LateContext<'_>, span: Span) {
8562
);
8663
}
8764

88-
impl<'tcx> LateLintPass<'tcx> for DecimalBitwiseOperands {
89-
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
90-
if expr.span.from_expansion() {
91-
return;
92-
}
93-
check_bitwise_binary_expr(cx, expr);
94-
check_bitwise_assign_expr(cx, expr);
65+
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
66+
if expr.span.from_expansion() {
67+
return;
9568
}
69+
check_bitwise_binary_expr(cx, expr);
70+
check_bitwise_assign_expr(cx, expr);
9671
}

clippy_lints/src/operators/mod.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ mod assign_op_pattern;
33
mod bit_mask;
44
mod cmp_owned;
55
mod const_comparisons;
6+
mod decimal_bitwise_operands;
67
mod double_comparison;
78
mod duration_subsec;
89
mod eq_op;
@@ -935,6 +936,28 @@ declare_clippy_lint! {
935936
"use of disallowed default division and remainder operations"
936937
}
937938

939+
declare_clippy_lint! {
940+
/// ### What it does
941+
/// Checks for decimal literals used as bit masks in bitwise operations.
942+
///
943+
/// ### Why is this bad?
944+
/// Using decimal literals for bit masks can make the code less readable and obscure the intended bit pattern.
945+
/// Binary, hexadecimal, or octal literals make the bit pattern more explicit and easier to understand at a glance.
946+
///
947+
/// ### Example
948+
/// ```rust,no_run
949+
/// let a = 14 & 6; // Bit pattern is not immediately clear
950+
/// ```
951+
/// Use instead:
952+
/// ```rust,no_run
953+
/// let a = 0b1110 & 0b0110;
954+
/// ```
955+
#[clippy::version = "1.93.0"]
956+
pub DECIMAL_BITWISE_OPERANDS,
957+
nursery,
958+
"use binary, hex, or octal literals for bitwise operations"
959+
}
960+
938961
pub struct Operators {
939962
arithmetic_context: numeric_arithmetic::Context,
940963
verbose_bit_mask_threshold: u64,
@@ -984,6 +1007,7 @@ impl_lint_pass!(Operators => [
9841007
MANUAL_IS_MULTIPLE_OF,
9851008
MANUAL_DIV_CEIL,
9861009
INVALID_UPCAST_COMPARISONS,
1010+
DECIMAL_BITWISE_OPERANDS
9871011
]);
9881012

9891013
impl<'tcx> LateLintPass<'tcx> for Operators {
@@ -1003,6 +1027,7 @@ impl<'tcx> LateLintPass<'tcx> for Operators {
10031027
needless_bitwise_bool::check(cx, e, op.node, lhs, rhs);
10041028
manual_midpoint::check(cx, e, op.node, lhs, rhs, self.msrv);
10051029
manual_is_multiple_of::check(cx, e, op.node, lhs, rhs, self.msrv);
1030+
decimal_bitwise_operands::check(cx, e);
10061031
}
10071032
self.arithmetic_context.check_binary(cx, e, op.node, lhs, rhs);
10081033
bit_mask::check(cx, e, op.node, lhs, rhs);
@@ -1027,6 +1052,9 @@ impl<'tcx> LateLintPass<'tcx> for Operators {
10271052
manual_div_ceil::check(cx, e, op.node, lhs, rhs, self.msrv);
10281053
},
10291054
ExprKind::AssignOp(op, lhs, rhs) => {
1055+
if !e.span.from_expansion() {
1056+
decimal_bitwise_operands::check(cx, e);
1057+
}
10301058
let bin_op = op.node.into();
10311059
self.arithmetic_context.check_binary(cx, e, bin_op, lhs, rhs);
10321060
misrefactored_assign_op::check(cx, e, bin_op, lhs, rhs);

0 commit comments

Comments
 (0)