[arithmetic-side-effects] Consider negative numbers and add more tests

This commit is contained in:
Caio 2023-01-06 12:25:51 -03:00
parent 179a22f624
commit 4262aebeaa
5 changed files with 304 additions and 62 deletions

View file

@ -2,7 +2,7 @@ use super::ARITHMETIC_SIDE_EFFECTS;
use clippy_utils::{
consts::{constant, constant_simple},
diagnostics::span_lint,
peel_hir_expr_refs,
peel_hir_expr_refs, peel_hir_expr_unary,
};
use rustc_ast as ast;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
@ -98,8 +98,11 @@ impl ArithmeticSideEffects {
}
/// If `expr` is not a literal integer like `1`, returns `None`.
///
/// Returns the absolute value of the expression, if this is an integer literal.
fn literal_integer(expr: &hir::Expr<'_>) -> Option<u128> {
if let hir::ExprKind::Lit(ref lit) = expr.kind && let ast::LitKind::Int(n, _) = lit.node {
let actual = peel_hir_expr_unary(expr).0;
if let hir::ExprKind::Lit(ref lit) = actual.kind && let ast::LitKind::Int(n, _) = lit.node {
Some(n)
}
else {
@ -123,12 +126,12 @@ impl ArithmeticSideEffects {
if !matches!(
op.node,
hir::BinOpKind::Add
| hir::BinOpKind::Sub
| hir::BinOpKind::Mul
| hir::BinOpKind::Div
| hir::BinOpKind::Mul
| hir::BinOpKind::Rem
| hir::BinOpKind::Shl
| hir::BinOpKind::Shr
| hir::BinOpKind::Sub
) {
return;
};

View file

@ -2258,6 +2258,18 @@ pub fn peel_n_hir_expr_refs<'a>(expr: &'a Expr<'a>, count: usize) -> (&'a Expr<'
(e, count - remaining)
}
/// Peels off all unary operators of an expression. Returns the underlying expression and the number
/// of operators removed.
pub fn peel_hir_expr_unary<'a>(expr: &'a Expr<'a>) -> (&'a Expr<'a>, usize) {
let mut count: usize = 0;
let mut curr_expr = expr;
while let ExprKind::Unary(_, local_expr) = curr_expr.kind {
count = count.wrapping_add(1);
curr_expr = local_expr;
}
(curr_expr, count)
}
/// Peels off all references on the expression. Returns the underlying expression and the number of
/// references removed.
pub fn peel_hir_expr_refs<'a>(expr: &'a Expr<'a>) -> (&'a Expr<'a>, usize) {

View file

@ -107,7 +107,7 @@ fn rhs_is_different() {
fn unary() {
// is explicitly on the list
let _ = -OutOfNames;
// is specifically on the list
// is explicitly on the list
let _ = -Foo;
// not on the list
let _ = -Bar;

View file

@ -2,6 +2,7 @@
clippy::assign_op_pattern,
clippy::erasing_op,
clippy::identity_op,
clippy::no_effect,
clippy::op_ref,
clippy::unnecessary_owned_empty_strings,
arithmetic_overflow,
@ -125,6 +126,18 @@ pub fn non_overflowing_ops_or_ops_already_handled_by_the_compiler_should_not_tri
_n *= &0;
_n *= 1;
_n *= &1;
_n += -0;
_n += &-0;
_n -= -0;
_n -= &-0;
_n /= -99;
_n /= &-99;
_n %= -99;
_n %= &-99;
_n *= -0;
_n *= &-0;
_n *= -1;
_n *= &-1;
// Binary
_n = _n + 0;
@ -158,7 +171,7 @@ pub fn non_overflowing_ops_or_ops_already_handled_by_the_compiler_should_not_tri
_n = -&i32::MIN;
}
pub fn runtime_ops() {
pub fn unknown_ops_or_runtime_ops_that_can_overflow() {
let mut _n = i32::MAX;
// Assign
@ -172,6 +185,16 @@ pub fn runtime_ops() {
_n %= &0;
_n *= 2;
_n *= &2;
_n += -1;
_n += &-1;
_n -= -1;
_n -= &-1;
_n /= -0;
_n /= &-0;
_n %= -0;
_n %= &-0;
_n *= -2;
_n *= &-2;
// Binary
_n = _n + 1;
@ -225,4 +248,46 @@ pub fn runtime_ops() {
_n = -&_n;
}
// Copied and pasted from the `integer_arithmetic` lint for comparison.
pub fn integer_arithmetic() {
let mut i = 1i32;
let mut var1 = 0i32;
let mut var2 = -1i32;
1 + i;
i * 2;
1 % i / 2;
i - 2 + 2 - i;
-i;
i >> 1;
i << 1;
-1;
-(-1);
i & 1;
i | 1;
i ^ 1;
i += 1;
i -= 1;
i *= 2;
i /= 2;
i /= 0;
i /= -1;
i /= var1;
i /= var2;
i %= 2;
i %= 0;
i %= -1;
i %= var1;
i %= var2;
i <<= 3;
i >>= 2;
i |= 1;
i &= 1;
i ^= i;
}
fn main() {}

View file

@ -1,5 +1,5 @@
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:165:5
--> $DIR/arithmetic_side_effects.rs:178:5
|
LL | _n += 1;
| ^^^^^^^
@ -7,328 +7,490 @@ LL | _n += 1;
= note: `-D clippy::arithmetic-side-effects` implied by `-D warnings`
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:166:5
--> $DIR/arithmetic_side_effects.rs:179:5
|
LL | _n += &1;
| ^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:167:5
--> $DIR/arithmetic_side_effects.rs:180:5
|
LL | _n -= 1;
| ^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:168:5
--> $DIR/arithmetic_side_effects.rs:181:5
|
LL | _n -= &1;
| ^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:169:5
--> $DIR/arithmetic_side_effects.rs:182:5
|
LL | _n /= 0;
| ^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:170:5
--> $DIR/arithmetic_side_effects.rs:183:5
|
LL | _n /= &0;
| ^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:171:5
--> $DIR/arithmetic_side_effects.rs:184:5
|
LL | _n %= 0;
| ^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:172:5
--> $DIR/arithmetic_side_effects.rs:185:5
|
LL | _n %= &0;
| ^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:173:5
--> $DIR/arithmetic_side_effects.rs:186:5
|
LL | _n *= 2;
| ^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:174:5
--> $DIR/arithmetic_side_effects.rs:187:5
|
LL | _n *= &2;
| ^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:177:10
--> $DIR/arithmetic_side_effects.rs:188:5
|
LL | _n += -1;
| ^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:189:5
|
LL | _n += &-1;
| ^^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:190:5
|
LL | _n -= -1;
| ^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:191:5
|
LL | _n -= &-1;
| ^^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:192:5
|
LL | _n /= -0;
| ^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:193:5
|
LL | _n /= &-0;
| ^^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:194:5
|
LL | _n %= -0;
| ^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:195:5
|
LL | _n %= &-0;
| ^^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:196:5
|
LL | _n *= -2;
| ^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:197:5
|
LL | _n *= &-2;
| ^^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:200:10
|
LL | _n = _n + 1;
| ^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:178:10
--> $DIR/arithmetic_side_effects.rs:201:10
|
LL | _n = _n + &1;
| ^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:179:10
--> $DIR/arithmetic_side_effects.rs:202:10
|
LL | _n = 1 + _n;
| ^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:180:10
--> $DIR/arithmetic_side_effects.rs:203:10
|
LL | _n = &1 + _n;
| ^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:181:10
--> $DIR/arithmetic_side_effects.rs:204:10
|
LL | _n = _n - 1;
| ^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:182:10
--> $DIR/arithmetic_side_effects.rs:205:10
|
LL | _n = _n - &1;
| ^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:183:10
--> $DIR/arithmetic_side_effects.rs:206:10
|
LL | _n = 1 - _n;
| ^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:184:10
--> $DIR/arithmetic_side_effects.rs:207:10
|
LL | _n = &1 - _n;
| ^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:185:10
--> $DIR/arithmetic_side_effects.rs:208:10
|
LL | _n = _n / 0;
| ^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:186:10
--> $DIR/arithmetic_side_effects.rs:209:10
|
LL | _n = _n / &0;
| ^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:187:10
--> $DIR/arithmetic_side_effects.rs:210:10
|
LL | _n = _n % 0;
| ^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:188:10
--> $DIR/arithmetic_side_effects.rs:211:10
|
LL | _n = _n % &0;
| ^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:189:10
--> $DIR/arithmetic_side_effects.rs:212:10
|
LL | _n = _n * 2;
| ^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:190:10
--> $DIR/arithmetic_side_effects.rs:213:10
|
LL | _n = _n * &2;
| ^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:191:10
--> $DIR/arithmetic_side_effects.rs:214:10
|
LL | _n = 2 * _n;
| ^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:192:10
--> $DIR/arithmetic_side_effects.rs:215:10
|
LL | _n = &2 * _n;
| ^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:193:10
--> $DIR/arithmetic_side_effects.rs:216:10
|
LL | _n = 23 + &85;
| ^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:194:10
--> $DIR/arithmetic_side_effects.rs:217:10
|
LL | _n = &23 + 85;
| ^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:195:10
--> $DIR/arithmetic_side_effects.rs:218:10
|
LL | _n = &23 + &85;
| ^^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:198:13
--> $DIR/arithmetic_side_effects.rs:221:13
|
LL | let _ = Custom + 0;
| ^^^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:199:13
--> $DIR/arithmetic_side_effects.rs:222:13
|
LL | let _ = Custom + 1;
| ^^^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:200:13
--> $DIR/arithmetic_side_effects.rs:223:13
|
LL | let _ = Custom + 2;
| ^^^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:201:13
--> $DIR/arithmetic_side_effects.rs:224:13
|
LL | let _ = Custom + 0.0;
| ^^^^^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:202:13
--> $DIR/arithmetic_side_effects.rs:225:13
|
LL | let _ = Custom + 1.0;
| ^^^^^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:203:13
--> $DIR/arithmetic_side_effects.rs:226:13
|
LL | let _ = Custom + 2.0;
| ^^^^^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:204:13
--> $DIR/arithmetic_side_effects.rs:227:13
|
LL | let _ = Custom - 0;
| ^^^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:205:13
--> $DIR/arithmetic_side_effects.rs:228:13
|
LL | let _ = Custom - 1;
| ^^^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:206:13
--> $DIR/arithmetic_side_effects.rs:229:13
|
LL | let _ = Custom - 2;
| ^^^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:207:13
--> $DIR/arithmetic_side_effects.rs:230:13
|
LL | let _ = Custom - 0.0;
| ^^^^^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:208:13
--> $DIR/arithmetic_side_effects.rs:231:13
|
LL | let _ = Custom - 1.0;
| ^^^^^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:209:13
--> $DIR/arithmetic_side_effects.rs:232:13
|
LL | let _ = Custom - 2.0;
| ^^^^^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:210:13
--> $DIR/arithmetic_side_effects.rs:233:13
|
LL | let _ = Custom / 0;
| ^^^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:211:13
--> $DIR/arithmetic_side_effects.rs:234:13
|
LL | let _ = Custom / 1;
| ^^^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:212:13
--> $DIR/arithmetic_side_effects.rs:235:13
|
LL | let _ = Custom / 2;
| ^^^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:213:13
--> $DIR/arithmetic_side_effects.rs:236:13
|
LL | let _ = Custom / 0.0;
| ^^^^^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:214:13
--> $DIR/arithmetic_side_effects.rs:237:13
|
LL | let _ = Custom / 1.0;
| ^^^^^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:215:13
--> $DIR/arithmetic_side_effects.rs:238:13
|
LL | let _ = Custom / 2.0;
| ^^^^^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:216:13
--> $DIR/arithmetic_side_effects.rs:239:13
|
LL | let _ = Custom * 0;
| ^^^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:217:13
--> $DIR/arithmetic_side_effects.rs:240:13
|
LL | let _ = Custom * 1;
| ^^^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:218:13
--> $DIR/arithmetic_side_effects.rs:241:13
|
LL | let _ = Custom * 2;
| ^^^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:219:13
--> $DIR/arithmetic_side_effects.rs:242:13
|
LL | let _ = Custom * 0.0;
| ^^^^^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:220:13
--> $DIR/arithmetic_side_effects.rs:243:13
|
LL | let _ = Custom * 1.0;
| ^^^^^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:221:13
--> $DIR/arithmetic_side_effects.rs:244:13
|
LL | let _ = Custom * 2.0;
| ^^^^^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:224:10
--> $DIR/arithmetic_side_effects.rs:247:10
|
LL | _n = -_n;
| ^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:225:10
--> $DIR/arithmetic_side_effects.rs:248:10
|
LL | _n = -&_n;
| ^^^^
error: aborting due to 55 previous errors
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:257:5
|
LL | 1 + i;
| ^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:258:5
|
LL | i * 2;
| ^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:260:5
|
LL | i - 2 + 2 - i;
| ^^^^^^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:261:5
|
LL | -i;
| ^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:262:5
|
LL | i >> 1;
| ^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:263:5
|
LL | i << 1;
| ^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:272:5
|
LL | i += 1;
| ^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:273:5
|
LL | i -= 1;
| ^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:274:5
|
LL | i *= 2;
| ^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:276:5
|
LL | i /= 0;
| ^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:278:5
|
LL | i /= var1;
| ^^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:279:5
|
LL | i /= var2;
| ^^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:281:5
|
LL | i %= 0;
| ^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:283:5
|
LL | i %= var1;
| ^^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:284:5
|
LL | i %= var2;
| ^^^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:285:5
|
LL | i <<= 3;
| ^^^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:286:5
|
LL | i >>= 2;
| ^^^^^^^
error: aborting due to 82 previous errors