1 use clippy_utils::consts::{constant, Constant};
2 use clippy_utils::diagnostics::span_lint_and_then;
3 use clippy_utils::sext;
4 use if_chain::if_chain;
5 use rustc_hir::{BinOpKind, Expr, ExprKind};
6 use rustc_lint::{LateContext, LateLintPass};
8 use rustc_session::{declare_lint_pass, declare_tool_lint};
11 declare_clippy_lint! {
13 /// Checks for modulo arithmetic.
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.
21 /// For example, in Rust `17 % -3 = 2`, but in Python `17 % -3 = -1`.
27 #[clippy::version = "1.42.0"]
28 pub MODULO_ARITHMETIC,
30 "any modulo arithmetic statement"
33 declare_lint_pass!(ModuloArithmetic => [MODULO_ARITHMETIC]);
36 string_representation: Option<String>,
41 fn analyze_operand(operand: &Expr<'_>, cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<OperandInfo> {
42 match constant(cx, cx.typeck_results(), operand) {
43 Some((Constant::Int(v), _)) => match *cx.typeck_results().expr_ty(expr).kind() {
45 let value = sext(cx.tcx, v, ity);
46 return Some(OperandInfo {
47 string_representation: Some(value.to_string()),
48 is_negative: value < 0,
53 return Some(OperandInfo {
54 string_representation: None,
61 Some((Constant::F32(f), _)) => {
62 return Some(floating_point_operand_info(&f));
64 Some((Constant::F64(f), _)) => {
65 return Some(floating_point_operand_info(&f));
72 fn floating_point_operand_info<T: Display + PartialOrd + From<f32>>(f: &T) -> OperandInfo {
74 string_representation: Some(format!("{:.3}", *f)),
75 is_negative: *f < 0.0.into(),
80 fn might_have_negative_value(t: &ty::TyS<'_>) -> bool {
81 t.is_signed() || t.is_floating_point()
84 fn check_const_operands<'tcx>(
85 cx: &LateContext<'tcx>,
87 lhs_operand: &OperandInfo,
88 rhs_operand: &OperandInfo,
90 if lhs_operand.is_negative ^ rhs_operand.is_negative {
96 "you are using modulo operator on constants with different signs: `{} % {}`",
97 lhs_operand.string_representation.as_ref().unwrap(),
98 rhs_operand.string_representation.as_ref().unwrap()
101 diag.note("double check for expected result especially when interoperating with different languages");
102 if lhs_operand.is_integral {
103 diag.note("or consider using `rem_euclid` or similar function");
110 fn check_non_const_operands<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, operand: &Expr<'_>) {
111 let operand_type = cx.typeck_results().expr_ty(operand);
112 if might_have_negative_value(operand_type) {
117 "you are using modulo operator on types that might have different signs",
119 diag.note("double check for expected result especially when interoperating with different languages");
120 if operand_type.is_integral() {
121 diag.note("or consider using `rem_euclid` or similar function");
128 impl<'tcx> LateLintPass<'tcx> for ModuloArithmetic {
129 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
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);
136 if let Some(lhs_operand) = lhs_operand;
137 if let Some(rhs_operand) = rhs_operand;
139 check_const_operands(cx, expr, &lhs_operand, &rhs_operand);
142 check_non_const_operands(cx, expr, lhs);