diff options
| author | bors <bors@rust-lang.org> | 2022-09-13 19:00:24 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2022-09-13 19:00:24 +0000 |
| commit | 6466258f7f197d09119fb9211dc08e6558620144 (patch) | |
| tree | 48960c5415ce44a87a0c14274e74acaeeca549b6 | |
| parent | 556415870d68d1f69ac884a07838b99e9a2aa73c (diff) | |
| parent | 611c905482d407581c104ba9d331d612f6f56461 (diff) | |
| download | rust-6466258f7f197d09119fb9211dc08e6558620144.tar.gz rust-6466258f7f197d09119fb9211dc08e6558620144.zip | |
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
| -rw-r--r-- | clippy_lints/src/operators/arithmetic_side_effects.rs | 16 | ||||
| -rw-r--r-- | tests/ui/arithmetic_side_effects.rs | 20 | ||||
| -rw-r--r-- | tests/ui/arithmetic_side_effects.stderr | 44 |
3 files changed, 72 insertions, 8 deletions
diff --git a/clippy_lints/src/operators/arithmetic_side_effects.rs b/clippy_lints/src/operators/arithmetic_side_effects.rs index 83b69fbb311..21c2b76439b 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 f5390c74642..a51c2514283 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 6c4c8bdec0f..6652a1f8ca8 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 |
