diff --git a/clippy_lints/src/operators/arithmetic_side_effects.rs b/clippy_lints/src/operators/arithmetic_side_effects.rs index 83b69fbb3..21c2b7643 100644 --- a/clippy_lints/src/operators/arithmetic_side_effects.rs +++ b/clippy_lints/src/operators/arithmetic_side_effects.rs @@ -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; } diff --git a/tests/ui/arithmetic_side_effects.rs b/tests/ui/arithmetic_side_effects.rs index f5390c746..a51c25142 100644 --- a/tests/ui/arithmetic_side_effects.rs +++ b/tests/ui/arithmetic_side_effects.rs @@ -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() {} diff --git a/tests/ui/arithmetic_side_effects.stderr b/tests/ui/arithmetic_side_effects.stderr index 6c4c8bdec..6652a1f8c 100644 --- a/tests/ui/arithmetic_side_effects.stderr +++ b/tests/ui/arithmetic_side_effects.stderr @@ -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