]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/modulo_arithmetic.rs
Auto merge of #73453 - erikdesjardins:tuplayout, r=eddyb
[rust.git] / src / tools / clippy / clippy_lints / src / modulo_arithmetic.rs
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};
8 use std::fmt::Display;
9
10 declare_clippy_lint! {
11     /// **What it does:** Checks for modulo arithmetic.
12     ///
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.
17     ///
18     /// For example, in Rust `17 % -3 = 2`, but in Python `17 % -3 = -1`.
19     ///
20     /// **Known problems:** None.
21     ///
22     /// **Example:**
23     /// ```rust
24     /// let x = -17 % 3;
25     /// ```
26     pub MODULO_ARITHMETIC,
27     restriction,
28     "any modulo arithmetic statement"
29 }
30
31 declare_lint_pass!(ModuloArithmetic => [MODULO_ARITHMETIC]);
32
33 struct OperandInfo {
34     string_representation: Option<String>,
35     is_negative: bool,
36     is_integral: bool,
37 }
38
39 fn analyze_operand(operand: &Expr<'_>, cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<OperandInfo> {
40     match constant(cx, cx.typeck_results(), operand) {
41         Some((Constant::Int(v), _)) => match *cx.typeck_results().expr_ty(expr).kind() {
42             ty::Int(ity) => {
43                 let value = sext(cx.tcx, v, ity);
44                 return Some(OperandInfo {
45                     string_representation: Some(value.to_string()),
46                     is_negative: value < 0,
47                     is_integral: true,
48                 });
49             },
50             ty::Uint(_) => {
51                 return Some(OperandInfo {
52                     string_representation: None,
53                     is_negative: false,
54                     is_integral: true,
55                 });
56             },
57             _ => {},
58         },
59         Some((Constant::F32(f), _)) => {
60             return Some(floating_point_operand_info(&f));
61         },
62         Some((Constant::F64(f), _)) => {
63             return Some(floating_point_operand_info(&f));
64         },
65         _ => {},
66     }
67     None
68 }
69
70 fn floating_point_operand_info<T: Display + PartialOrd + From<f32>>(f: &T) -> OperandInfo {
71     OperandInfo {
72         string_representation: Some(format!("{:.3}", *f)),
73         is_negative: *f < 0.0.into(),
74         is_integral: false,
75     }
76 }
77
78 fn might_have_negative_value(t: &ty::TyS<'_>) -> bool {
79     t.is_signed() || t.is_floating_point()
80 }
81
82 fn check_const_operands<'tcx>(
83     cx: &LateContext<'tcx>,
84     expr: &'tcx Expr<'_>,
85     lhs_operand: &OperandInfo,
86     rhs_operand: &OperandInfo,
87 ) {
88     if lhs_operand.is_negative ^ rhs_operand.is_negative {
89         span_lint_and_then(
90             cx,
91             MODULO_ARITHMETIC,
92             expr.span,
93             &format!(
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()
97             ),
98             |diag| {
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");
102                 }
103             },
104         );
105     }
106 }
107
108 fn check_non_const_operands<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, operand: &Expr<'_>) {
109     let operand_type = cx.typeck_results().expr_ty(operand);
110     if might_have_negative_value(operand_type) {
111         span_lint_and_then(
112             cx,
113             MODULO_ARITHMETIC,
114             expr.span,
115             "you are using modulo operator on types that might have different signs",
116             |diag| {
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");
120                 }
121             },
122         );
123     }
124 }
125
126 impl<'tcx> LateLintPass<'tcx> for ModuloArithmetic {
127     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
128         match &expr.kind {
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);
133                     if_chain! {
134                         if let Some(lhs_operand) = lhs_operand;
135                         if let Some(rhs_operand) = rhs_operand;
136                         then {
137                             check_const_operands(cx, expr, &lhs_operand, &rhs_operand);
138                         }
139                         else {
140                             check_non_const_operands(cx, expr, lhs);
141                         }
142                     }
143                 };
144             },
145             _ => {},
146         }
147     }
148 }