-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #4867 - mgr-inz-rafal:modulo_arithmetic, r=flip1995
Modulo arithmetic changelog: Added modulo_arithmetic lint
- Loading branch information
Showing
11 changed files
with
728 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
use crate::consts::{constant, Constant}; | ||
use crate::utils::{sext, span_lint_and_then}; | ||
use if_chain::if_chain; | ||
use rustc::declare_lint_pass; | ||
use rustc::hir::*; | ||
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; | ||
use rustc::ty::{self}; | ||
use rustc_session::declare_tool_lint; | ||
use std::fmt::Display; | ||
|
||
declare_clippy_lint! { | ||
/// **What it does:** Checks for modulo arithemtic. | ||
/// | ||
/// **Why is this bad?** The results of modulo (%) operation might differ | ||
/// depending on the language, when negative numbers are involved. | ||
/// If you interop with different languages it might be beneficial | ||
/// to double check all places that use modulo arithmetic. | ||
/// | ||
/// For example, in Rust `17 % -3 = 2`, but in Python `17 % -3 = -1`. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// ```rust | ||
/// let x = -17 % 3; | ||
/// ``` | ||
pub MODULO_ARITHMETIC, | ||
restriction, | ||
"any modulo arithmetic statement" | ||
} | ||
|
||
declare_lint_pass!(ModuloArithmetic => [MODULO_ARITHMETIC]); | ||
|
||
struct OperandInfo { | ||
string_representation: Option<String>, | ||
is_negative: bool, | ||
is_integral: bool, | ||
} | ||
|
||
fn analyze_operand(operand: &Expr, cx: &LateContext<'_, '_>, expr: &Expr) -> Option<OperandInfo> { | ||
match constant(cx, cx.tables, operand) { | ||
Some((Constant::Int(v), _)) => match cx.tables.expr_ty(expr).kind { | ||
ty::Int(ity) => { | ||
let value = sext(cx.tcx, v, ity); | ||
return Some(OperandInfo { | ||
string_representation: Some(value.to_string()), | ||
is_negative: value < 0, | ||
is_integral: true, | ||
}); | ||
}, | ||
ty::Uint(_) => { | ||
return Some(OperandInfo { | ||
string_representation: None, | ||
is_negative: false, | ||
is_integral: true, | ||
}); | ||
}, | ||
_ => {}, | ||
}, | ||
Some((Constant::F32(f), _)) => { | ||
return Some(floating_point_operand_info(&f)); | ||
}, | ||
Some((Constant::F64(f), _)) => { | ||
return Some(floating_point_operand_info(&f)); | ||
}, | ||
_ => {}, | ||
} | ||
None | ||
} | ||
|
||
fn floating_point_operand_info<T: Display + PartialOrd + From<f32>>(f: &T) -> OperandInfo { | ||
OperandInfo { | ||
string_representation: Some(format!("{:.3}", *f)), | ||
is_negative: *f < 0.0.into(), | ||
is_integral: false, | ||
} | ||
} | ||
|
||
fn might_have_negative_value(t: &ty::TyS<'_>) -> bool { | ||
t.is_signed() || t.is_floating_point() | ||
} | ||
|
||
fn check_const_operands<'a, 'tcx>( | ||
cx: &LateContext<'a, 'tcx>, | ||
expr: &'tcx Expr, | ||
lhs_operand: &OperandInfo, | ||
rhs_operand: &OperandInfo, | ||
) { | ||
if lhs_operand.is_negative ^ rhs_operand.is_negative { | ||
span_lint_and_then( | ||
cx, | ||
MODULO_ARITHMETIC, | ||
expr.span, | ||
&format!( | ||
"you are using modulo operator on constants with different signs: `{} % {}`", | ||
lhs_operand.string_representation.as_ref().unwrap(), | ||
rhs_operand.string_representation.as_ref().unwrap() | ||
), | ||
|db| { | ||
db.note("double check for expected result especially when interoperating with different languages"); | ||
if lhs_operand.is_integral { | ||
db.note("or consider using `rem_euclid` or similar function"); | ||
} | ||
}, | ||
); | ||
} | ||
} | ||
|
||
fn check_non_const_operands<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, operand: &Expr) { | ||
let operand_type = cx.tables.expr_ty(operand); | ||
if might_have_negative_value(operand_type) { | ||
span_lint_and_then( | ||
cx, | ||
MODULO_ARITHMETIC, | ||
expr.span, | ||
"you are using modulo operator on types that might have different signs", | ||
|db| { | ||
db.note("double check for expected result especially when interoperating with different languages"); | ||
if operand_type.is_integral() { | ||
db.note("or consider using `rem_euclid` or similar function"); | ||
} | ||
}, | ||
); | ||
} | ||
} | ||
|
||
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ModuloArithmetic { | ||
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { | ||
match &expr.kind { | ||
ExprKind::Binary(op, lhs, rhs) | ExprKind::AssignOp(op, lhs, rhs) => { | ||
if let BinOpKind::Rem = op.node { | ||
let lhs_operand = analyze_operand(lhs, cx, expr); | ||
let rhs_operand = analyze_operand(rhs, cx, expr); | ||
if_chain! { | ||
if let Some(lhs_operand) = lhs_operand; | ||
if let Some(rhs_operand) = rhs_operand; | ||
then { | ||
check_const_operands(cx, expr, &lhs_operand, &rhs_operand); | ||
} | ||
else { | ||
check_non_const_operands(cx, expr, lhs); | ||
} | ||
} | ||
}; | ||
}, | ||
_ => {}, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#![warn(clippy::modulo_arithmetic)] | ||
#![allow( | ||
unused, | ||
clippy::shadow_reuse, | ||
clippy::shadow_unrelated, | ||
clippy::no_effect, | ||
clippy::unnecessary_operation, | ||
clippy::modulo_one | ||
)] | ||
|
||
fn main() { | ||
// Lint when both sides are const and of the opposite sign | ||
-1.6 % 2.1; | ||
1.6 % -2.1; | ||
(1.1 - 2.3) % (1.1 + 2.3); | ||
(1.1 + 2.3) % (1.1 - 2.3); | ||
|
||
// Lint on floating point numbers | ||
let a_f32: f32 = -1.6; | ||
let mut b_f32: f32 = 2.1; | ||
a_f32 % b_f32; | ||
b_f32 % a_f32; | ||
b_f32 %= a_f32; | ||
|
||
let a_f64: f64 = -1.6; | ||
let mut b_f64: f64 = 2.1; | ||
a_f64 % b_f64; | ||
b_f64 % a_f64; | ||
b_f64 %= a_f64; | ||
|
||
// No lint when both sides are const and of the same sign | ||
1.6 % 2.1; | ||
-1.6 % -2.1; | ||
(1.1 + 2.3) % (-1.1 + 2.3); | ||
(-1.1 - 2.3) % (1.1 - 2.3); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
error: you are using modulo operator on constants with different signs: `-1.600 % 2.100` | ||
--> $DIR/modulo_arithmetic_float.rs:13:5 | ||
| | ||
LL | -1.6 % 2.1; | ||
| ^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::modulo-arithmetic` implied by `-D warnings` | ||
= note: double check for expected result especially when interoperating with different languages | ||
|
||
error: you are using modulo operator on constants with different signs: `1.600 % -2.100` | ||
--> $DIR/modulo_arithmetic_float.rs:14:5 | ||
| | ||
LL | 1.6 % -2.1; | ||
| ^^^^^^^^^^ | ||
| | ||
= note: double check for expected result especially when interoperating with different languages | ||
|
||
error: you are using modulo operator on constants with different signs: `-1.200 % 3.400` | ||
--> $DIR/modulo_arithmetic_float.rs:15:5 | ||
| | ||
LL | (1.1 - 2.3) % (1.1 + 2.3); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: double check for expected result especially when interoperating with different languages | ||
|
||
error: you are using modulo operator on constants with different signs: `3.400 % -1.200` | ||
--> $DIR/modulo_arithmetic_float.rs:16:5 | ||
| | ||
LL | (1.1 + 2.3) % (1.1 - 2.3); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: double check for expected result especially when interoperating with different languages | ||
|
||
error: you are using modulo operator on types that might have different signs | ||
--> $DIR/modulo_arithmetic_float.rs:21:5 | ||
| | ||
LL | a_f32 % b_f32; | ||
| ^^^^^^^^^^^^^ | ||
| | ||
= note: double check for expected result especially when interoperating with different languages | ||
|
||
error: you are using modulo operator on types that might have different signs | ||
--> $DIR/modulo_arithmetic_float.rs:22:5 | ||
| | ||
LL | b_f32 % a_f32; | ||
| ^^^^^^^^^^^^^ | ||
| | ||
= note: double check for expected result especially when interoperating with different languages | ||
|
||
error: you are using modulo operator on types that might have different signs | ||
--> $DIR/modulo_arithmetic_float.rs:23:5 | ||
| | ||
LL | b_f32 %= a_f32; | ||
| ^^^^^^^^^^^^^^ | ||
| | ||
= note: double check for expected result especially when interoperating with different languages | ||
|
||
error: you are using modulo operator on types that might have different signs | ||
--> $DIR/modulo_arithmetic_float.rs:27:5 | ||
| | ||
LL | a_f64 % b_f64; | ||
| ^^^^^^^^^^^^^ | ||
| | ||
= note: double check for expected result especially when interoperating with different languages | ||
|
||
error: you are using modulo operator on types that might have different signs | ||
--> $DIR/modulo_arithmetic_float.rs:28:5 | ||
| | ||
LL | b_f64 % a_f64; | ||
| ^^^^^^^^^^^^^ | ||
| | ||
= note: double check for expected result especially when interoperating with different languages | ||
|
||
error: you are using modulo operator on types that might have different signs | ||
--> $DIR/modulo_arithmetic_float.rs:29:5 | ||
| | ||
LL | b_f64 %= a_f64; | ||
| ^^^^^^^^^^^^^^ | ||
| | ||
= note: double check for expected result especially when interoperating with different languages | ||
|
||
error: aborting due to 10 previous errors | ||
|
Oops, something went wrong.