Skip to content

[arithmetic-side-effects] Add more tests #9502

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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