1 use crate::consts::{constant, Constant};
2 use crate::utils::{sext, span_lint_and_then};
3 use if_chain::if_chain;
4 use rustc_hir::{BinOpKind, Expr, ExprKind};
5 use rustc_lint::{LateContext, LateLintPass};
6 use rustc_middle::ty::{self};
7 use rustc_session::{declare_lint_pass, declare_tool_lint};
10 declare_clippy_lint! {
11 /// **What it does:** Checks for modulo arithemtic.
13 /// **Why is this bad?** The results of modulo (%) operation might differ
14 /// depending on the language, when negative numbers are involved.
15 /// If you interop with different languages it might be beneficial
16 /// to double check all places that use modulo arithmetic.
18 /// For example, in Rust `17 % -3 = 2`, but in Python `17 % -3 = -1`.
20 /// **Known problems:** None.
26 pub MODULO_ARITHMETIC,
28 "any modulo arithmetic statement"
31 declare_lint_pass!(ModuloArithmetic => [MODULO_ARITHMETIC]);
34 string_representation: Option<String>,
39 fn analyze_operand(operand: &Expr<'_>, cx: &LateContext<'_, '_>, expr: &Expr<'_>) -> Option<OperandInfo> {
40 match constant(cx, cx.tables, operand) {
41 Some((Constant::Int(v), _)) => match cx.tables.expr_ty(expr).kind {
43 let value = sext(cx.tcx, v, ity);
44 return Some(OperandInfo {
45 string_representation: Some(value.to_string()),
46 is_negative: value < 0,
51 return Some(OperandInfo {
52 string_representation: None,
59 Some((Constant::F32(f), _)) => {
60 return Some(floating_point_operand_info(&f));
62 Some((Constant::F64(f), _)) => {
63 return Some(floating_point_operand_info(&f));
70 fn floating_point_operand_info<T: Display + PartialOrd + From<f32>>(f: &T) -> OperandInfo {
72 string_representation: Some(format!("{:.3}", *f)),
73 is_negative: *f < 0.0.into(),
78 fn might_have_negative_value(t: &ty::TyS<'_>) -> bool {
79 t.is_signed() || t.is_floating_point()
82 fn check_const_operands<'a, 'tcx>(
83 cx: &LateContext<'a, 'tcx>,
85 lhs_operand: &OperandInfo,
86 rhs_operand: &OperandInfo,
88 if lhs_operand.is_negative ^ rhs_operand.is_negative {
94 "you are using modulo operator on constants with different signs: `{} % {}`",
95 lhs_operand.string_representation.as_ref().unwrap(),
96 rhs_operand.string_representation.as_ref().unwrap()
99 diag.note("double check for expected result especially when interoperating with different languages");
100 if lhs_operand.is_integral {
101 diag.note("or consider using `rem_euclid` or similar function");
108 fn check_non_const_operands<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>, operand: &Expr<'_>) {
109 let operand_type = cx.tables.expr_ty(operand);
110 if might_have_negative_value(operand_type) {
115 "you are using modulo operator on types that might have different signs",
117 diag.note("double check for expected result especially when interoperating with different languages");
118 if operand_type.is_integral() {
119 diag.note("or consider using `rem_euclid` or similar function");
126 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ModuloArithmetic {
127 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
129 ExprKind::Binary(op, lhs, rhs) | ExprKind::AssignOp(op, lhs, rhs) => {
130 if let BinOpKind::Rem = op.node {
131 let lhs_operand = analyze_operand(lhs, cx, expr);
132 let rhs_operand = analyze_operand(rhs, cx, expr);
134 if let Some(lhs_operand) = lhs_operand;
135 if let Some(rhs_operand) = rhs_operand;
137 check_const_operands(cx, expr, &lhs_operand, &rhs_operand);
140 check_non_const_operands(cx, expr, lhs);