Skip to content

Commit daca3b4

Browse files
committed
Review: use span_note_and_lint, present offending expression in message
1 parent 2ef7376 commit daca3b4

File tree

3 files changed

+113
-99
lines changed

3 files changed

+113
-99
lines changed

clippy_lints/src/modulo_arithmetic.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::consts::{constant, Constant};
2-
use crate::utils::{sext, span_help_and_lint};
2+
use crate::utils::{sext, span_note_and_lint};
33
use if_chain::if_chain;
44
use rustc::declare_lint_pass;
55
use rustc::hir::*;
@@ -30,22 +30,23 @@ declare_clippy_lint! {
3030

3131
declare_lint_pass!(ModuloArithmetic => [MODULO_ARITHMETIC]);
3232

33-
fn is_negative(operand: &Expr, cx: &LateContext<'_, '_>, expr: &Expr) -> Option<bool> {
33+
fn is_negative(operand: &Expr, cx: &LateContext<'_, '_>, expr: &Expr) -> Option<(bool, Option<String>)> {
3434
match constant(cx, cx.tables, operand) {
3535
Some((Constant::Int(v), _)) => match cx.tables.expr_ty(expr).kind {
3636
ty::Int(ity) => {
37-
return Some(sext(cx.tcx, v, ity) < 0);
37+
let value = sext(cx.tcx, v, ity);
38+
return Some((value < 0, Some(value.to_string())));
3839
},
3940
ty::Uint(_) => {
40-
return Some(false);
41+
return Some((false, None));
4142
},
4243
_ => {},
4344
},
4445
Some((Constant::F32(f), _)) => {
45-
return Some(f < 0.0);
46+
return Some((f < 0.0, Some(format!("{:.3}", f))));
4647
},
4748
Some((Constant::F64(f), _)) => {
48-
return Some(f < 0.0);
49+
return Some((f < 0.0, Some(format!("{:.3}", f))));
4950
},
5051
_ => {},
5152
}
@@ -68,15 +69,18 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ModuloArithmetic {
6869
if_chain! {
6970
if let Some(lhs_negative) = lhs_negative;
7071
if let Some(rhs_negative) = rhs_negative;
71-
if lhs_negative ^ rhs_negative;
72+
if lhs_negative.0 ^ rhs_negative.0;
7273
then
7374
{
74-
span_help_and_lint(
75+
span_note_and_lint(
7576
cx,
7677
MODULO_ARITHMETIC,
7778
expr.span,
78-
"modulo arithemtic detected",
79-
"modulo operands have different signs, double check all modulo arithemtic results when interoperating with different languages",
79+
&format!(
80+
"you are using modulo operator on constants with different signs: `{} % {}`", lhs_negative.1.unwrap(), rhs_negative.1.unwrap()
81+
),
82+
expr.span,
83+
"double check for expected result especially when interoperating with different languages",
8084
);
8185
return;
8286
}
@@ -85,12 +89,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ModuloArithmetic {
8589
// Operands are not consts, fallback to the 'might have different signs' lint
8690
let lhs_type = cx.tables.expr_ty(lhs);
8791
if might_have_negative_value(lhs_type) {
88-
span_help_and_lint(
92+
span_note_and_lint(
8993
cx,
9094
MODULO_ARITHMETIC,
9195
expr.span,
92-
"modulo arithemtic detected",
93-
"modulo operands might have different signs, double check all modulo arithemtic results when interoperating with different languages",
96+
"you are using modulo operator on types that might have different signs",
97+
expr.span,
98+
"double check for expected result especially when interoperating with different languages",
9499
);
95100
}
96101
}

tests/ui/modulo_arithmetic.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ fn main() {
1818
(1 + 2) % (1 - 2);
1919
(1.1 - 2.3) % (1.1 + 2.3);
2020
(1.1 + 2.3) % (1.1 - 2.3);
21+
35 * (7 - 4 * 2) % (-500 * -600);
2122

2223
-1i8 % 2i8;
2324
1i8 % -2i8;

0 commit comments

Comments
 (0)