From 611c905482d407581c104ba9d331d612f6f56461 Mon Sep 17 00:00:00 2001 From: Caio Date: Tue, 13 Sep 2022 15:50:24 -0300 Subject: [PATCH] [arithmetic-side-effects] More non-overflowing ops --- .../src/operators/arithmetic_side_effects.rs | 16 ++++++- tests/ui/arithmetic_side_effects.rs | 20 ++++++++- 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