Skip to content

Commit 9c1d134

Browse files
committed
Move ModuloArithmetic into Operators lint pass
1 parent ccd474f commit 9c1d134

File tree

5 files changed

+53
-53
lines changed

5 files changed

+53
-53
lines changed

clippy_lints/src/lib.register_lints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,6 @@ store.register_lints(&[
373373
mixed_read_write_in_expression::MIXED_READ_WRITE_IN_EXPRESSION,
374374
module_style::MOD_MODULE_FILES,
375375
module_style::SELF_NAMED_MODULE_FILES,
376-
modulo_arithmetic::MODULO_ARITHMETIC,
377376
mut_key::MUTABLE_KEY_TYPE,
378377
mut_mut::MUT_MUT,
379378
mut_mutex_lock::MUT_MUTEX_LOCK,
@@ -427,6 +426,7 @@ store.register_lints(&[
427426
operators::INTEGER_ARITHMETIC,
428427
operators::INTEGER_DIVISION,
429428
operators::MISREFACTORED_ASSIGN_OP,
429+
operators::MODULO_ARITHMETIC,
430430
operators::MODULO_ONE,
431431
operators::OP_REF,
432432
operators::VERBOSE_BIT_MASK,

clippy_lints/src/lib.register_restriction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ store.register_group(true, "clippy::restriction", Some("clippy_restriction"), ve
4747
LintId::of(mixed_read_write_in_expression::MIXED_READ_WRITE_IN_EXPRESSION),
4848
LintId::of(module_style::MOD_MODULE_FILES),
4949
LintId::of(module_style::SELF_NAMED_MODULE_FILES),
50-
LintId::of(modulo_arithmetic::MODULO_ARITHMETIC),
5150
LintId::of(operators::FLOAT_ARITHMETIC),
5251
LintId::of(operators::FLOAT_CMP_CONST),
5352
LintId::of(operators::INTEGER_ARITHMETIC),
5453
LintId::of(operators::INTEGER_DIVISION),
54+
LintId::of(operators::MODULO_ARITHMETIC),
5555
LintId::of(panic_in_result_fn::PANIC_IN_RESULT_FN),
5656
LintId::of(panic_unimplemented::PANIC),
5757
LintId::of(panic_unimplemented::TODO),

clippy_lints/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,6 @@ mod missing_enforced_import_rename;
291291
mod missing_inline;
292292
mod mixed_read_write_in_expression;
293293
mod module_style;
294-
mod modulo_arithmetic;
295294
mod mut_key;
296295
mod mut_mut;
297296
mod mut_mutex_lock;
@@ -709,7 +708,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
709708
store.register_late_pass(move || Box::new(trait_bounds::TraitBounds::new(max_trait_bounds)));
710709
store.register_late_pass(|| Box::new(comparison_chain::ComparisonChain));
711710
store.register_late_pass(|| Box::new(mut_key::MutableKeyType));
712-
store.register_late_pass(|| Box::new(modulo_arithmetic::ModuloArithmetic));
713711
store.register_early_pass(|| Box::new(reference::DerefAddrOf));
714712
store.register_early_pass(|| Box::new(double_parens::DoubleParens));
715713
store.register_late_pass(|| Box::new(format_impl::FormatImpl::new()));

clippy_lints/src/operators/mod.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ mod float_equality_without_abs;
1616
mod identity_op;
1717
mod integer_division;
1818
mod misrefactored_assign_op;
19+
mod modulo_arithmetic;
1920
mod modulo_one;
2021
mod numeric_arithmetic;
2122
mod op_ref;
@@ -617,6 +618,28 @@ declare_clippy_lint! {
617618
"taking a number modulo +/-1, which can either panic/overflow or always returns 0"
618619
}
619620

621+
declare_clippy_lint! {
622+
/// ### What it does
623+
/// Checks for modulo arithmetic.
624+
///
625+
/// ### Why is this bad?
626+
/// The results of modulo (%) operation might differ
627+
/// depending on the language, when negative numbers are involved.
628+
/// If you interop with different languages it might be beneficial
629+
/// to double check all places that use modulo arithmetic.
630+
///
631+
/// For example, in Rust `17 % -3 = 2`, but in Python `17 % -3 = -1`.
632+
///
633+
/// ### Example
634+
/// ```rust
635+
/// let x = -17 % 3;
636+
/// ```
637+
#[clippy::version = "1.42.0"]
638+
pub MODULO_ARITHMETIC,
639+
restriction,
640+
"any modulo arithmetic statement"
641+
}
642+
620643
pub struct Operators {
621644
arithmetic_context: numeric_arithmetic::Context,
622645
verbose_bit_mask_threshold: u64,
@@ -643,6 +666,7 @@ impl_lint_pass!(Operators => [
643666
FLOAT_CMP,
644667
FLOAT_CMP_CONST,
645668
MODULO_ONE,
669+
MODULO_ARITHMETIC,
646670
]);
647671
impl Operators {
648672
pub fn new(verbose_bit_mask_threshold: u64) -> Self {
@@ -677,10 +701,12 @@ impl<'tcx> LateLintPass<'tcx> for Operators {
677701
cmp_owned::check(cx, op.node, lhs, rhs);
678702
float_cmp::check(cx, e, op.node, lhs, rhs);
679703
modulo_one::check(cx, e, op.node, rhs);
704+
modulo_arithmetic::check(cx, e, op.node, lhs, rhs);
680705
},
681706
ExprKind::AssignOp(op, lhs, rhs) => {
682707
self.arithmetic_context.check_binary(cx, e, op.node, lhs, rhs);
683708
misrefactored_assign_op::check(cx, e, op.node, lhs, rhs);
709+
modulo_arithmetic::check(cx, e, op.node, lhs, rhs);
684710
},
685711
ExprKind::Assign(lhs, rhs, _) => {
686712
assign_op_pattern::check(cx, e, lhs, rhs);

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

Lines changed: 25 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,35 @@ use clippy_utils::consts::{constant, Constant};
22
use clippy_utils::diagnostics::span_lint_and_then;
33
use clippy_utils::sext;
44
use if_chain::if_chain;
5-
use rustc_hir::{BinOpKind, Expr, ExprKind};
6-
use rustc_lint::{LateContext, LateLintPass};
5+
use rustc_hir::{BinOpKind, Expr};
6+
use rustc_lint::LateContext;
77
use rustc_middle::ty::{self, Ty};
8-
use rustc_session::{declare_lint_pass, declare_tool_lint};
98
use std::fmt::Display;
109

11-
declare_clippy_lint! {
12-
/// ### What it does
13-
/// Checks for modulo arithmetic.
14-
///
15-
/// ### Why is this bad?
16-
/// The results of modulo (%) operation might differ
17-
/// depending on the language, when negative numbers are involved.
18-
/// If you interop with different languages it might be beneficial
19-
/// to double check all places that use modulo arithmetic.
20-
///
21-
/// For example, in Rust `17 % -3 = 2`, but in Python `17 % -3 = -1`.
22-
///
23-
/// ### Example
24-
/// ```rust
25-
/// let x = -17 % 3;
26-
/// ```
27-
#[clippy::version = "1.42.0"]
28-
pub MODULO_ARITHMETIC,
29-
restriction,
30-
"any modulo arithmetic statement"
31-
}
10+
use super::MODULO_ARITHMETIC;
3211

33-
declare_lint_pass!(ModuloArithmetic => [MODULO_ARITHMETIC]);
12+
pub(super) fn check<'tcx>(
13+
cx: &LateContext<'tcx>,
14+
e: &'tcx Expr<'_>,
15+
op: BinOpKind,
16+
lhs: &'tcx Expr<'_>,
17+
rhs: &'tcx Expr<'_>,
18+
) {
19+
if op == BinOpKind::Rem {
20+
let lhs_operand = analyze_operand(lhs, cx, e);
21+
let rhs_operand = analyze_operand(rhs, cx, e);
22+
if_chain! {
23+
if let Some(lhs_operand) = lhs_operand;
24+
if let Some(rhs_operand) = rhs_operand;
25+
then {
26+
check_const_operands(cx, e, &lhs_operand, &rhs_operand);
27+
}
28+
else {
29+
check_non_const_operands(cx, e, lhs);
30+
}
31+
}
32+
};
33+
}
3434

3535
struct OperandInfo {
3636
string_representation: Option<String>,
@@ -124,27 +124,3 @@ fn check_non_const_operands<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>,
124124
);
125125
}
126126
}
127-
128-
impl<'tcx> LateLintPass<'tcx> for ModuloArithmetic {
129-
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
130-
match &expr.kind {
131-
ExprKind::Binary(op, lhs, rhs) | ExprKind::AssignOp(op, lhs, rhs) => {
132-
if op.node == BinOpKind::Rem {
133-
let lhs_operand = analyze_operand(lhs, cx, expr);
134-
let rhs_operand = analyze_operand(rhs, cx, expr);
135-
if_chain! {
136-
if let Some(lhs_operand) = lhs_operand;
137-
if let Some(rhs_operand) = rhs_operand;
138-
then {
139-
check_const_operands(cx, expr, &lhs_operand, &rhs_operand);
140-
}
141-
else {
142-
check_non_const_operands(cx, expr, lhs);
143-
}
144-
}
145-
};
146-
},
147-
_ => {},
148-
}
149-
}
150-
}

0 commit comments

Comments
 (0)