Skip to content

Commit f39b81a

Browse files
committed
[arithmetic-side-effects] Add more tests
1 parent 5c3c6a2 commit f39b81a

File tree

2 files changed

+65
-17
lines changed

2 files changed

+65
-17
lines changed

tests/ui/arithmetic_side_effects.rs

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub fn hard_coded_allowed() {
3333
}
3434

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

@@ -45,9 +45,12 @@ pub fn non_overflowing_const_ops() {
4545

4646
const _: i32 = 1 + 1;
4747
let _ = const { 1 + 1 };
48+
49+
const _: i32 = { let mut n = -1; n = -(-1); n = -n; n };
50+
let _ = const { let mut n = -1; n = -(-1); n = -n; n };
4851
}
4952

50-
pub fn non_overflowing_runtime_ops() {
53+
pub fn non_overflowing_runtime_ops_or_ops_already_handled_by_the_compiler() {
5154
let mut _n = i32::MAX;
5255

5356
// Assign
@@ -70,9 +73,12 @@ pub fn non_overflowing_runtime_ops() {
7073
_n = _n * 1;
7174
_n = 1 * _n;
7275
_n = 23 + 85;
76+
77+
// Unary
78+
_n = -1;
79+
_n = -(-1);
7380
}
7481

75-
#[rustfmt::skip]
7682
pub fn overflowing_runtime_ops() {
7783
let mut _n = i32::MAX;
7884

@@ -92,6 +98,42 @@ pub fn overflowing_runtime_ops() {
9298
_n = _n % 0;
9399
_n = _n * 2;
94100
_n = 2 * _n;
101+
102+
// Unary
103+
_n = -_n;
104+
}
105+
106+
pub fn association_with_structures_should_not_trigger_the_lint() {
107+
enum Foo {
108+
Bar = -2,
109+
}
110+
111+
impl Trait for Foo {
112+
const ASSOC: i32 = {
113+
let _: [i32; 1 + 1];
114+
fn foo() {}
115+
1 + 1
116+
};
117+
}
118+
119+
struct Baz([i32; 1 + 1]);
120+
121+
trait Trait {
122+
const ASSOC: i32 = 1 + 1;
123+
}
124+
125+
type Alias = [i32; 1 + 1];
126+
127+
union Qux {
128+
field: [i32; 1 + 1],
129+
}
130+
131+
let _: [i32; 1 + 1] = [0, 0];
132+
133+
let _: [i32; 1 + 1] = {
134+
let a: [i32; 1 + 1] = [0, 0];
135+
a
136+
};
95137
}
96138

97139
fn main() {}
Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,88 @@
11
error: arithmetic operation that can potentially result in unexpected side-effects
2-
--> $DIR/arithmetic_side_effects.rs:80:5
2+
--> $DIR/arithmetic_side_effects.rs:86:5
33
|
44
LL | _n += 1;
55
| ^^^^^^^
66
|
77
= note: `-D clippy::arithmetic-side-effects` implied by `-D warnings`
88

99
error: arithmetic operation that can potentially result in unexpected side-effects
10-
--> $DIR/arithmetic_side_effects.rs:81:5
10+
--> $DIR/arithmetic_side_effects.rs:87:5
1111
|
1212
LL | _n -= 1;
1313
| ^^^^^^^
1414

1515
error: arithmetic operation that can potentially result in unexpected side-effects
16-
--> $DIR/arithmetic_side_effects.rs:82:5
16+
--> $DIR/arithmetic_side_effects.rs:88:5
1717
|
1818
LL | _n /= 0;
1919
| ^^^^^^^
2020

2121
error: arithmetic operation that can potentially result in unexpected side-effects
22-
--> $DIR/arithmetic_side_effects.rs:83:5
22+
--> $DIR/arithmetic_side_effects.rs:89:5
2323
|
2424
LL | _n %= 0;
2525
| ^^^^^^^
2626

2727
error: arithmetic operation that can potentially result in unexpected side-effects
28-
--> $DIR/arithmetic_side_effects.rs:84:5
28+
--> $DIR/arithmetic_side_effects.rs:90:5
2929
|
3030
LL | _n *= 2;
3131
| ^^^^^^^
3232

3333
error: arithmetic operation that can potentially result in unexpected side-effects
34-
--> $DIR/arithmetic_side_effects.rs:87:10
34+
--> $DIR/arithmetic_side_effects.rs:93:10
3535
|
3636
LL | _n = _n + 1;
3737
| ^^^^^^
3838

3939
error: arithmetic operation that can potentially result in unexpected side-effects
40-
--> $DIR/arithmetic_side_effects.rs:88:10
40+
--> $DIR/arithmetic_side_effects.rs:94:10
4141
|
4242
LL | _n = 1 + _n;
4343
| ^^^^^^
4444

4545
error: arithmetic operation that can potentially result in unexpected side-effects
46-
--> $DIR/arithmetic_side_effects.rs:89:10
46+
--> $DIR/arithmetic_side_effects.rs:95:10
4747
|
4848
LL | _n = _n - 1;
4949
| ^^^^^^
5050

5151
error: arithmetic operation that can potentially result in unexpected side-effects
52-
--> $DIR/arithmetic_side_effects.rs:90:10
52+
--> $DIR/arithmetic_side_effects.rs:96:10
5353
|
5454
LL | _n = 1 - _n;
5555
| ^^^^^^
5656

5757
error: arithmetic operation that can potentially result in unexpected side-effects
58-
--> $DIR/arithmetic_side_effects.rs:91:10
58+
--> $DIR/arithmetic_side_effects.rs:97:10
5959
|
6060
LL | _n = _n / 0;
6161
| ^^^^^^
6262

6363
error: arithmetic operation that can potentially result in unexpected side-effects
64-
--> $DIR/arithmetic_side_effects.rs:92:10
64+
--> $DIR/arithmetic_side_effects.rs:98:10
6565
|
6666
LL | _n = _n % 0;
6767
| ^^^^^^
6868

6969
error: arithmetic operation that can potentially result in unexpected side-effects
70-
--> $DIR/arithmetic_side_effects.rs:93:10
70+
--> $DIR/arithmetic_side_effects.rs:99:10
7171
|
7272
LL | _n = _n * 2;
7373
| ^^^^^^
7474

7575
error: arithmetic operation that can potentially result in unexpected side-effects
76-
--> $DIR/arithmetic_side_effects.rs:94:10
76+
--> $DIR/arithmetic_side_effects.rs:100:10
7777
|
7878
LL | _n = 2 * _n;
7979
| ^^^^^^
8080

81-
error: aborting due to 13 previous errors
81+
error: arithmetic operation that can potentially result in unexpected side-effects
82+
--> $DIR/arithmetic_side_effects.rs:103:10
83+
|
84+
LL | _n = -_n;
85+
| ^^^
86+
87+
error: aborting due to 14 previous errors
8288

0 commit comments

Comments
 (0)