Auto merge of #9507 - c410-f3r:arith, r=Alexendoo
[arithmetic-side-effects] Consider references Takes into consideration integer references like `&i32::MAX` because currently things like `let _ = &1 + 0` trigger the lint. changelog: FP: [`arithmetic_side_effects`]: Now ignores references [9507](https://github.com/rust-lang/rust-clippy/pull/9507)
This commit is contained in:
commit
d31db02e47
3 changed files with 172 additions and 41 deletions
|
|
@ -2,11 +2,12 @@
|
|||
clippy::assign_op_pattern,
|
||||
clippy::erasing_op,
|
||||
clippy::identity_op,
|
||||
clippy::op_ref,
|
||||
clippy::unnecessary_owned_empty_strings,
|
||||
arithmetic_overflow,
|
||||
unconditional_panic
|
||||
)]
|
||||
#![feature(inline_const, saturating_int_impl)]
|
||||
#![feature(const_mut_refs, inline_const, saturating_int_impl)]
|
||||
#![warn(clippy::arithmetic_side_effects)]
|
||||
|
||||
use core::num::{Saturating, Wrapping};
|
||||
|
|
@ -79,33 +80,50 @@ pub fn const_ops_should_not_trigger_the_lint() {
|
|||
const _: i32 = 1 + 1;
|
||||
let _ = const { 1 + 1 };
|
||||
|
||||
const _: i32 = { let mut n = -1; n = -(-1); n = -n; n };
|
||||
let _ = const { let mut n = -1; n = -(-1); n = -n; n };
|
||||
const _: i32 = { let mut n = 1; n = -1; n = -(-1); n = -n; n };
|
||||
let _ = const { let mut n = 1; n = -1; n = -(-1); n = -n; n };
|
||||
}
|
||||
|
||||
pub fn non_overflowing_runtime_ops_or_ops_already_handled_by_the_compiler() {
|
||||
pub fn non_overflowing_ops_or_ops_already_handled_by_the_compiler_should_not_trigger_the_lint() {
|
||||
let mut _n = i32::MAX;
|
||||
|
||||
// Assign
|
||||
_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;
|
||||
_n = _n + &0;
|
||||
_n = 0 + _n;
|
||||
_n = &0 + _n;
|
||||
_n = _n - 0;
|
||||
_n = _n - &0;
|
||||
_n = 0 - _n;
|
||||
_n = &0 - _n;
|
||||
_n = _n / 99;
|
||||
_n = _n / &99;
|
||||
_n = _n % 99;
|
||||
_n = _n % &99;
|
||||
_n = _n * 0;
|
||||
_n = _n * &0;
|
||||
_n = 0 * _n;
|
||||
_n = &0 * _n;
|
||||
_n = _n * 1;
|
||||
_n = _n * &1;
|
||||
_n = 1 * _n;
|
||||
_n = &1 * _n;
|
||||
_n = 23 + 85;
|
||||
_n = &23 + &85;
|
||||
|
||||
// Unary
|
||||
_n = -1;
|
||||
|
|
@ -117,23 +135,37 @@ pub fn overflowing_runtime_ops() {
|
|||
|
||||
// Assign
|
||||
_n += 1;
|
||||
_n += &1;
|
||||
_n -= 1;
|
||||
_n -= &1;
|
||||
_n /= 0;
|
||||
_n /= &0;
|
||||
_n %= 0;
|
||||
_n %= &0;
|
||||
_n *= 2;
|
||||
_n *= &2;
|
||||
|
||||
// Binary
|
||||
_n = _n + 1;
|
||||
_n = _n + &1;
|
||||
_n = 1 + _n;
|
||||
_n = &1 + _n;
|
||||
_n = _n - 1;
|
||||
_n = _n - &1;
|
||||
_n = 1 - _n;
|
||||
_n = &1 - _n;
|
||||
_n = _n / 0;
|
||||
_n = _n / &0;
|
||||
_n = _n % 0;
|
||||
_n = _n % &0;
|
||||
_n = _n * 2;
|
||||
_n = _n * &2;
|
||||
_n = 2 * _n;
|
||||
_n = &2 * _n;
|
||||
|
||||
// Unary
|
||||
_n = -_n;
|
||||
_n = -&_n;
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
error: arithmetic operation that can potentially result in unexpected side-effects
|
||||
--> $DIR/arithmetic_side_effects.rs:119:5
|
||||
--> $DIR/arithmetic_side_effects.rs:137:5
|
||||
|
|
||||
LL | _n += 1;
|
||||
| ^^^^^^^
|
||||
|
|
@ -7,82 +7,166 @@ 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:120:5
|
||||
--> $DIR/arithmetic_side_effects.rs:138:5
|
||||
|
|
||||
LL | _n += &1;
|
||||
| ^^^^^^^^
|
||||
|
||||
error: arithmetic operation that can potentially result in unexpected side-effects
|
||||
--> $DIR/arithmetic_side_effects.rs:139:5
|
||||
|
|
||||
LL | _n -= 1;
|
||||
| ^^^^^^^
|
||||
|
||||
error: arithmetic operation that can potentially result in unexpected side-effects
|
||||
--> $DIR/arithmetic_side_effects.rs:121:5
|
||||
--> $DIR/arithmetic_side_effects.rs:140:5
|
||||
|
|
||||
LL | _n -= &1;
|
||||
| ^^^^^^^^
|
||||
|
||||
error: arithmetic operation that can potentially result in unexpected side-effects
|
||||
--> $DIR/arithmetic_side_effects.rs:141:5
|
||||
|
|
||||
LL | _n /= 0;
|
||||
| ^^^^^^^
|
||||
|
||||
error: arithmetic operation that can potentially result in unexpected side-effects
|
||||
--> $DIR/arithmetic_side_effects.rs:122:5
|
||||
--> $DIR/arithmetic_side_effects.rs:142:5
|
||||
|
|
||||
LL | _n /= &0;
|
||||
| ^^^^^^^^
|
||||
|
||||
error: arithmetic operation that can potentially result in unexpected side-effects
|
||||
--> $DIR/arithmetic_side_effects.rs:143:5
|
||||
|
|
||||
LL | _n %= 0;
|
||||
| ^^^^^^^
|
||||
|
||||
error: arithmetic operation that can potentially result in unexpected side-effects
|
||||
--> $DIR/arithmetic_side_effects.rs:123:5
|
||||
--> $DIR/arithmetic_side_effects.rs:144:5
|
||||
|
|
||||
LL | _n %= &0;
|
||||
| ^^^^^^^^
|
||||
|
||||
error: arithmetic operation that can potentially result in unexpected side-effects
|
||||
--> $DIR/arithmetic_side_effects.rs:145:5
|
||||
|
|
||||
LL | _n *= 2;
|
||||
| ^^^^^^^
|
||||
|
||||
error: arithmetic operation that can potentially result in unexpected side-effects
|
||||
--> $DIR/arithmetic_side_effects.rs:126:10
|
||||
--> $DIR/arithmetic_side_effects.rs:146:5
|
||||
|
|
||||
LL | _n *= &2;
|
||||
| ^^^^^^^^
|
||||
|
||||
error: arithmetic operation that can potentially result in unexpected side-effects
|
||||
--> $DIR/arithmetic_side_effects.rs:149:10
|
||||
|
|
||||
LL | _n = _n + 1;
|
||||
| ^^^^^^
|
||||
|
||||
error: arithmetic operation that can potentially result in unexpected side-effects
|
||||
--> $DIR/arithmetic_side_effects.rs:127:10
|
||||
--> $DIR/arithmetic_side_effects.rs:150:10
|
||||
|
|
||||
LL | _n = _n + &1;
|
||||
| ^^^^^^^
|
||||
|
||||
error: arithmetic operation that can potentially result in unexpected side-effects
|
||||
--> $DIR/arithmetic_side_effects.rs:151:10
|
||||
|
|
||||
LL | _n = 1 + _n;
|
||||
| ^^^^^^
|
||||
|
||||
error: arithmetic operation that can potentially result in unexpected side-effects
|
||||
--> $DIR/arithmetic_side_effects.rs:128:10
|
||||
--> $DIR/arithmetic_side_effects.rs:152:10
|
||||
|
|
||||
LL | _n = &1 + _n;
|
||||
| ^^^^^^^
|
||||
|
||||
error: arithmetic operation that can potentially result in unexpected side-effects
|
||||
--> $DIR/arithmetic_side_effects.rs:153:10
|
||||
|
|
||||
LL | _n = _n - 1;
|
||||
| ^^^^^^
|
||||
|
||||
error: arithmetic operation that can potentially result in unexpected side-effects
|
||||
--> $DIR/arithmetic_side_effects.rs:129:10
|
||||
--> $DIR/arithmetic_side_effects.rs:154:10
|
||||
|
|
||||
LL | _n = _n - &1;
|
||||
| ^^^^^^^
|
||||
|
||||
error: arithmetic operation that can potentially result in unexpected side-effects
|
||||
--> $DIR/arithmetic_side_effects.rs:155:10
|
||||
|
|
||||
LL | _n = 1 - _n;
|
||||
| ^^^^^^
|
||||
|
||||
error: arithmetic operation that can potentially result in unexpected side-effects
|
||||
--> $DIR/arithmetic_side_effects.rs:130:10
|
||||
--> $DIR/arithmetic_side_effects.rs:156:10
|
||||
|
|
||||
LL | _n = &1 - _n;
|
||||
| ^^^^^^^
|
||||
|
||||
error: arithmetic operation that can potentially result in unexpected side-effects
|
||||
--> $DIR/arithmetic_side_effects.rs:157:10
|
||||
|
|
||||
LL | _n = _n / 0;
|
||||
| ^^^^^^
|
||||
|
||||
error: arithmetic operation that can potentially result in unexpected side-effects
|
||||
--> $DIR/arithmetic_side_effects.rs:131:10
|
||||
--> $DIR/arithmetic_side_effects.rs:158:10
|
||||
|
|
||||
LL | _n = _n / &0;
|
||||
| ^^^^^^^
|
||||
|
||||
error: arithmetic operation that can potentially result in unexpected side-effects
|
||||
--> $DIR/arithmetic_side_effects.rs:159:10
|
||||
|
|
||||
LL | _n = _n % 0;
|
||||
| ^^^^^^
|
||||
|
||||
error: arithmetic operation that can potentially result in unexpected side-effects
|
||||
--> $DIR/arithmetic_side_effects.rs:132:10
|
||||
--> $DIR/arithmetic_side_effects.rs:160:10
|
||||
|
|
||||
LL | _n = _n % &0;
|
||||
| ^^^^^^^
|
||||
|
||||
error: arithmetic operation that can potentially result in unexpected side-effects
|
||||
--> $DIR/arithmetic_side_effects.rs:161:10
|
||||
|
|
||||
LL | _n = _n * 2;
|
||||
| ^^^^^^
|
||||
|
||||
error: arithmetic operation that can potentially result in unexpected side-effects
|
||||
--> $DIR/arithmetic_side_effects.rs:133:10
|
||||
--> $DIR/arithmetic_side_effects.rs:162:10
|
||||
|
|
||||
LL | _n = _n * &2;
|
||||
| ^^^^^^^
|
||||
|
||||
error: arithmetic operation that can potentially result in unexpected side-effects
|
||||
--> $DIR/arithmetic_side_effects.rs:163:10
|
||||
|
|
||||
LL | _n = 2 * _n;
|
||||
| ^^^^^^
|
||||
|
||||
error: arithmetic operation that can potentially result in unexpected side-effects
|
||||
--> $DIR/arithmetic_side_effects.rs:136:10
|
||||
--> $DIR/arithmetic_side_effects.rs:164:10
|
||||
|
|
||||
LL | _n = &2 * _n;
|
||||
| ^^^^^^^
|
||||
|
||||
error: arithmetic operation that can potentially result in unexpected side-effects
|
||||
--> $DIR/arithmetic_side_effects.rs:167:10
|
||||
|
|
||||
LL | _n = -_n;
|
||||
| ^^^
|
||||
|
||||
error: aborting due to 14 previous errors
|
||||
error: arithmetic operation that can potentially result in unexpected side-effects
|
||||
--> $DIR/arithmetic_side_effects.rs:168:10
|
||||
|
|
||||
LL | _n = -&_n;
|
||||
| ^^^^
|
||||
|
||||
error: aborting due to 28 previous errors
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue