Auto merge of #9474 - c410-f3r:arith, r=llogiq

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

* Adding or Subtracting 0
* Division and Module of anything other than 0
* Multiplying 1 or 0

changelog: [arithmetic-side-effects] More non-overflowing operations
This commit is contained in:
bors 2022-09-13 19:00:24 +00:00
commit 6466258f7f
3 changed files with 72 additions and 8 deletions

View File

@ -48,9 +48,21 @@ fn has_valid_assign_op(op: &Spanned<hir::BinOpKind>, rhs: &hir::Expr<'_>, rhs_re
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