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};
6 use rustc_lint::LateContext;
7 use rustc_middle::ty::{self, Ty};
10 use super::MODULO_ARITHMETIC;
12 pub(super) fn check<'tcx>(
13 cx: &LateContext<'tcx>,
19 if op == BinOpKind::Rem {
20 let lhs_operand = analyze_operand(lhs, cx, e);
21 let rhs_operand = analyze_operand(rhs, cx, e);
23 if let Some(lhs_operand) = lhs_operand;
24 if let Some(rhs_operand) = rhs_operand;
26 check_const_operands(cx, e, &lhs_operand, &rhs_operand);
29 check_non_const_operands(cx, e, lhs);
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<'_>) -> 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");