2021-05-27 13:49:37 +00:00
|
|
|
use clippy_utils::consts::{constant, Constant};
|
2021-03-16 00:55:45 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_then;
|
2021-03-16 16:06:34 +00:00
|
|
|
use clippy_utils::sext;
|
2019-12-26 12:34:18 +00:00
|
|
|
use if_chain::if_chain;
|
2020-02-21 08:39:38 +00:00
|
|
|
use rustc_hir::{BinOpKind, Expr, ExprKind};
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2021-04-13 01:24:47 +00:00
|
|
|
use rustc_middle::ty;
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2019-12-26 12:34:18 +00:00
|
|
|
use std::fmt::Display;
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-02 18:37:11 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for modulo arithmetic.
|
2019-12-26 12:34:18 +00:00
|
|
|
///
|
2021-07-02 18:37:11 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// The results of modulo (%) operation might differ
|
2019-12-26 12:34:18 +00:00
|
|
|
/// 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`.
|
|
|
|
///
|
2021-07-02 18:37:11 +00:00
|
|
|
/// ### Example
|
2019-12-26 12:34:18 +00:00
|
|
|
/// ```rust
|
|
|
|
/// let x = -17 % 3;
|
|
|
|
/// ```
|
Added `clippy::version` attribute to all normal lints
So, some context for this, well, more a story. I'm not used to scripting, I've never really scripted anything, even if it's a valuable skill. I just never really needed it. Now, `@flip1995` correctly suggested using a script for this in `rust-clippy#7813`...
And I decided to write a script using nushell because why not? This was a mistake... I spend way more time on this than I would like to admit. It has definitely been more than 4 hours. It shouldn't take that long, but me being new to scripting and nushell just wasn't a good mixture... Anyway, here is the script that creates another script which adds the versions. Fun...
Just execute this on the `gh-pages` branch and the resulting `replacer.sh` in `clippy_lints` and it should all work.
```nu
mv v0.0.212 rust-1.00.0;
mv beta rust-1.57.0;
mv master rust-1.58.0;
let paths = (open ./rust-1.58.0/lints.json | select id id_span | flatten | select id path);
let versions = (
ls | where name =~ "rust-" | select name | format {name}/lints.json |
each { open $it | select id | insert version $it | str substring "5,11" version} |
group-by id | rotate counter-clockwise id version |
update version {get version | first 1} | flatten | select id version);
$paths | each { |row|
let version = ($versions | where id == ($row.id) | format {version})
let idu = ($row.id | str upcase)
$"sed -i '0,/($idu),/{s/pub ($idu),/#[clippy::version = "($version)"]\n pub ($idu),/}' ($row.path)"
} | str collect ";" | str find-replace --all '1.00.0' 'pre 1.29.0' | save "replacer.sh";
```
And this still has some problems, but at this point I just want to be done -.-
2021-10-21 19:06:26 +00:00
|
|
|
#[clippy::version = "1.42.0"]
|
2019-12-26 12:34:18 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn analyze_operand(operand: &Expr<'_>, cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<OperandInfo> {
|
2020-07-17 08:47:04 +00:00
|
|
|
match constant(cx, cx.typeck_results(), operand) {
|
2020-08-03 22:18:29 +00:00
|
|
|
Some((Constant::Int(v), _)) => match *cx.typeck_results().expr_ty(expr).kind() {
|
2019-12-26 12:34:18 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_const_operands<'tcx>(
|
|
|
|
cx: &LateContext<'tcx>,
|
2019-12-28 16:14:19 +00:00
|
|
|
expr: &'tcx Expr<'_>,
|
2019-12-26 12:34:18 +00:00
|
|
|
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()
|
|
|
|
),
|
2020-04-17 06:08:00 +00:00
|
|
|
|diag| {
|
|
|
|
diag.note("double check for expected result especially when interoperating with different languages");
|
2019-12-26 12:34:18 +00:00
|
|
|
if lhs_operand.is_integral {
|
2020-04-17 06:08:00 +00:00
|
|
|
diag.note("or consider using `rem_euclid` or similar function");
|
2019-12-26 12:34:18 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_non_const_operands<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, operand: &Expr<'_>) {
|
2020-07-17 08:47:04 +00:00
|
|
|
let operand_type = cx.typeck_results().expr_ty(operand);
|
2019-12-26 12:34:18 +00:00
|
|
|
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",
|
2020-04-17 06:08:00 +00:00
|
|
|
|diag| {
|
|
|
|
diag.note("double check for expected result especially when interoperating with different languages");
|
2019-12-26 12:34:18 +00:00
|
|
|
if operand_type.is_integral() {
|
2020-04-17 06:08:00 +00:00
|
|
|
diag.note("or consider using `rem_euclid` or similar function");
|
2019-12-26 12:34:18 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for ModuloArithmetic {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
2019-12-26 12:34:18 +00:00
|
|
|
match &expr.kind {
|
|
|
|
ExprKind::Binary(op, lhs, rhs) | ExprKind::AssignOp(op, lhs, rhs) => {
|
2021-10-04 06:33:40 +00:00
|
|
|
if op.node == BinOpKind::Rem {
|
2019-12-26 12:34:18 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|