[arithmetic-side-effects] More non-overflowing ops

This commit is contained in:
Caio 2022-09-13 15:50:24 -03:00
parent 556415870d
commit 611c905482
3 changed files with 72 additions and 8 deletions

View file

@ -48,9 +48,21 @@ impl ArithmeticSideEffects {
if !Self::is_literal_integer(rhs, rhs_refs) {
return false;
}
if let hir::BinOpKind::Div | hir::BinOpKind::Mul = op.node
if let hir::BinOpKind::Add | hir::BinOpKind::Sub = op.node
&& let hir::ExprKind::Lit(ref lit) = rhs.kind
&& let ast::LitKind::Int(1, _) = lit.node
&& let ast::LitKind::Int(0, _) = lit.node
{
return true;
}
if let hir::BinOpKind::Div | hir::BinOpKind::Rem = op.node
&& let hir::ExprKind::Lit(ref lit) = rhs.kind
&& !matches!(lit.node, ast::LitKind::Int(0, _))
{
return true;
}
if let hir::BinOpKind::Mul = op.node
&& let hir::ExprKind::Lit(ref lit) = rhs.kind
&& let ast::LitKind::Int(0 | 1, _) = lit.node
{
return true;
}

View file

@ -1,4 +1,8 @@
#![allow(clippy::assign_op_pattern, clippy::unnecessary_owned_empty_strings)]
#![allow(
clippy::assign_op_pattern,
unconditional_panic,
clippy::unnecessary_owned_empty_strings
)]
#![feature(inline_const, saturating_int_impl)]
#![warn(clippy::arithmetic_side_effects)]
@ -41,8 +45,12 @@ pub fn non_overflowing_ops() {
let _ = const { 1 + 1 };
let mut _a = 1;
_a += 0;
_a -= 0;
_a /= 99;
_a %= 99;
_a *= 0;
_a *= 1;
_a /= 1;
}
#[rustfmt::skip]
@ -52,6 +60,14 @@ pub fn overflowing_ops() {
let mut _b = 1; _b = _b + 1;
let mut _c = 1; _c = 1 + _c;
let mut _a = 1;
_a += 1;
_a -= 1;
_a /= 0;
_a %= 0;
_a *= 2;
_a *= 3;
}
fn main() {}

View file

@ -1,5 +1,5 @@
error: arithmetic detected
--> $DIR/arithmetic_side_effects.rs:50:21
--> $DIR/arithmetic_side_effects.rs:58:21
|
LL | let mut _a = 1; _a += 1;
| ^^^^^^^
@ -7,16 +7,52 @@ LL | let mut _a = 1; _a += 1;
= note: `-D clippy::arithmetic-side-effects` implied by `-D warnings`
error: arithmetic detected
--> $DIR/arithmetic_side_effects.rs:52:26
--> $DIR/arithmetic_side_effects.rs:60:26
|
LL | let mut _b = 1; _b = _b + 1;
| ^^^^^^
error: arithmetic detected
--> $DIR/arithmetic_side_effects.rs:54:26
--> $DIR/arithmetic_side_effects.rs:62:26
|
LL | let mut _c = 1; _c = 1 + _c;
| ^^^^^^
error: aborting due to 3 previous errors
error: arithmetic detected
--> $DIR/arithmetic_side_effects.rs:65:5
|
LL | _a += 1;
| ^^^^^^^
error: arithmetic detected
--> $DIR/arithmetic_side_effects.rs:66:5
|
LL | _a -= 1;
| ^^^^^^^
error: arithmetic detected
--> $DIR/arithmetic_side_effects.rs:67:5
|
LL | _a /= 0;
| ^^^^^^^
error: arithmetic detected
--> $DIR/arithmetic_side_effects.rs:68:5
|
LL | _a %= 0;
| ^^^^^^^
error: arithmetic detected
--> $DIR/arithmetic_side_effects.rs:69:5
|
LL | _a *= 2;
| ^^^^^^^
error: arithmetic detected
--> $DIR/arithmetic_side_effects.rs:70:5
|
LL | _a *= 3;
| ^^^^^^^
error: aborting due to 9 previous errors