Skip to content

Commit

Permalink
Auto merge of rust-lang#9502 - c410-f3r:arith, r=Alexendoo
Browse files Browse the repository at this point in the history
[arithmetic-side-effects] Add more tests

Taken from the `integer-arithmetic` lint.

changelog: [arithmetic-side-effects] Add more tests
  • Loading branch information
bors committed Sep 20, 2022
2 parents 5c3c6a2 + 736d88b commit 1f66a3e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 17 deletions.
48 changes: 45 additions & 3 deletions tests/ui/arithmetic_side_effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,39 @@

use core::num::{Saturating, Wrapping};

pub fn association_with_structures_should_not_trigger_the_lint() {
enum Foo {
Bar = -2,
}

impl Trait for Foo {
const ASSOC: i32 = {
let _: [i32; 1 + 1];
fn foo() {}
1 + 1
};
}

struct Baz([i32; 1 + 1]);

trait Trait {
const ASSOC: i32 = 1 + 1;
}

type Alias = [i32; 1 + 1];

union Qux {
field: [i32; 1 + 1],
}

let _: [i32; 1 + 1] = [0, 0];

let _: [i32; 1 + 1] = {
let a: [i32; 1 + 1] = [0, 0];
a
};
}

pub fn hard_coded_allowed() {
let _ = 1f32 + 1f32;
let _ = 1f64 + 1f64;
Expand All @@ -33,7 +66,7 @@ pub fn hard_coded_allowed() {
}

#[rustfmt::skip]
pub fn non_overflowing_const_ops() {
pub fn const_ops_should_not_trigger_the_lint() {
const _: i32 = { let mut n = 1; n += 1; n };
let _ = const { let mut n = 1; n += 1; n };

Expand All @@ -45,9 +78,12 @@ pub fn non_overflowing_const_ops() {

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 };
}

pub fn non_overflowing_runtime_ops() {
pub fn non_overflowing_runtime_ops_or_ops_already_handled_by_the_compiler() {
let mut _n = i32::MAX;

// Assign
Expand All @@ -70,9 +106,12 @@ pub fn non_overflowing_runtime_ops() {
_n = _n * 1;
_n = 1 * _n;
_n = 23 + 85;

// Unary
_n = -1;
_n = -(-1);
}

#[rustfmt::skip]
pub fn overflowing_runtime_ops() {
let mut _n = i32::MAX;

Expand All @@ -92,6 +131,9 @@ pub fn overflowing_runtime_ops() {
_n = _n % 0;
_n = _n * 2;
_n = 2 * _n;

// Unary
_n = -_n;
}

fn main() {}
34 changes: 20 additions & 14 deletions tests/ui/arithmetic_side_effects.stderr
Original file line number Diff line number Diff line change
@@ -1,82 +1,88 @@
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:80:5
--> $DIR/arithmetic_side_effects.rs:119:5
|
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:81:5
--> $DIR/arithmetic_side_effects.rs:120:5
|
LL | _n -= 1;
| ^^^^^^^

error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:82:5
--> $DIR/arithmetic_side_effects.rs:121:5
|
LL | _n /= 0;
| ^^^^^^^

error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:83:5
--> $DIR/arithmetic_side_effects.rs:122:5
|
LL | _n %= 0;
| ^^^^^^^

error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:84:5
--> $DIR/arithmetic_side_effects.rs:123:5
|
LL | _n *= 2;
| ^^^^^^^

error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:87:10
--> $DIR/arithmetic_side_effects.rs:126:10
|
LL | _n = _n + 1;
| ^^^^^^

error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:88:10
--> $DIR/arithmetic_side_effects.rs:127:10
|
LL | _n = 1 + _n;
| ^^^^^^

error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:89:10
--> $DIR/arithmetic_side_effects.rs:128:10
|
LL | _n = _n - 1;
| ^^^^^^

error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:90:10
--> $DIR/arithmetic_side_effects.rs:129:10
|
LL | _n = 1 - _n;
| ^^^^^^

error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:91:10
--> $DIR/arithmetic_side_effects.rs:130:10
|
LL | _n = _n / 0;
| ^^^^^^

error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:92:10
--> $DIR/arithmetic_side_effects.rs:131:10
|
LL | _n = _n % 0;
| ^^^^^^

error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:93:10
--> $DIR/arithmetic_side_effects.rs:132:10
|
LL | _n = _n * 2;
| ^^^^^^

error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:94:10
--> $DIR/arithmetic_side_effects.rs:133:10
|
LL | _n = 2 * _n;
| ^^^^^^

error: aborting due to 13 previous errors
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:136:10
|
LL | _n = -_n;
| ^^^

error: aborting due to 14 previous errors

0 comments on commit 1f66a3e

Please sign in to comment.