Skip to content

Commit 05a567b

Browse files
committed
Split binary and assign functions to avoid duplicated logic
1 parent a6c0cfd commit 05a567b

File tree

2 files changed

+23
-29
lines changed

2 files changed

+23
-29
lines changed

clippy_lints/src/operators/decimal_bitwise_operands.rs

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,32 @@ use rustc_span::source_map::Spanned;
99

1010
use super::DECIMAL_BITWISE_OPERANDS;
1111

12-
fn check_bitwise_binary_expr(cx: &LateContext<'_>, expr: &Expr<'_>) {
13-
if let ExprKind::Binary(op, left, right) = &expr.kind
14-
&& matches!(op.node, BinOpKind::BitAnd | BinOpKind::BitOr | BinOpKind::BitXor)
15-
{
16-
for expr in [left, right] {
17-
if let ExprKind::Lit(lit) = &expr.kind
18-
&& is_decimal_number(cx, lit.span)
19-
&& !is_power_of_twoish(lit)
20-
{
21-
emit_lint(cx, lit.span);
22-
}
12+
pub(super) fn check_binary<'tcx>(cx: &LateContext<'tcx>, op: BinOpKind, left: &'tcx Expr<'_>, right: &'tcx Expr<'_>) {
13+
if !matches!(op, BinOpKind::BitAnd | BinOpKind::BitOr | BinOpKind::BitXor) {
14+
return;
15+
}
16+
17+
for expr in [left, right] {
18+
if let ExprKind::Lit(lit) = &expr.kind
19+
&& is_decimal_number(cx, lit.span)
20+
&& !is_power_of_twoish(lit)
21+
{
22+
emit_lint(cx, lit.span);
2323
}
2424
}
2525
}
2626

27-
fn check_bitwise_assign_expr(cx: &LateContext<'_>, expr: &Expr<'_>) {
28-
if let ExprKind::AssignOp(op, _, e) = &expr.kind
29-
&& matches!(
30-
op.node,
31-
AssignOpKind::BitAndAssign | AssignOpKind::BitOrAssign | AssignOpKind::BitXorAssign
32-
)
33-
&& let ExprKind::Lit(lit) = e.kind
27+
pub(super) fn check_assign<'tcx>(cx: &LateContext<'tcx>, op: AssignOpKind, rhs: &'tcx Expr<'_>) {
28+
if !matches!(
29+
op,
30+
AssignOpKind::BitAndAssign | AssignOpKind::BitOrAssign | AssignOpKind::BitXorAssign
31+
) {
32+
return;
33+
}
34+
35+
if let ExprKind::Lit(lit) = &rhs.kind
3436
&& is_decimal_number(cx, lit.span)
35-
&& !is_power_of_twoish(&lit)
37+
&& !is_power_of_twoish(lit)
3638
{
3739
emit_lint(cx, lit.span);
3840
}
@@ -61,11 +63,3 @@ fn emit_lint(cx: &LateContext<'_>, span: Span) {
6163
"use binary (0b...), hex (0x...), or octal (0o...) notation for better readability",
6264
);
6365
}
64-
65-
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
66-
if expr.span.from_expansion() {
67-
return;
68-
}
69-
check_bitwise_binary_expr(cx, expr);
70-
check_bitwise_assign_expr(cx, expr);
71-
}

clippy_lints/src/operators/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,7 +1027,7 @@ impl<'tcx> LateLintPass<'tcx> for Operators {
10271027
needless_bitwise_bool::check(cx, e, op.node, lhs, rhs);
10281028
manual_midpoint::check(cx, e, op.node, lhs, rhs, self.msrv);
10291029
manual_is_multiple_of::check(cx, e, op.node, lhs, rhs, self.msrv);
1030-
decimal_bitwise_operands::check(cx, e);
1030+
decimal_bitwise_operands::check_binary(cx, op.node, lhs, rhs);
10311031
}
10321032
self.arithmetic_context.check_binary(cx, e, op.node, lhs, rhs);
10331033
bit_mask::check(cx, e, op.node, lhs, rhs);
@@ -1053,7 +1053,7 @@ impl<'tcx> LateLintPass<'tcx> for Operators {
10531053
},
10541054
ExprKind::AssignOp(op, lhs, rhs) => {
10551055
if !e.span.from_expansion() {
1056-
decimal_bitwise_operands::check(cx, e);
1056+
decimal_bitwise_operands::check_assign(cx, op.node, rhs);
10571057
}
10581058
let bin_op = op.node.into();
10591059
self.arithmetic_context.check_binary(cx, e, bin_op, lhs, rhs);

0 commit comments

Comments
 (0)