Skip to content

Commit 1f66a3e

Browse files
committed
Auto merge of rust-lang#9502 - c410-f3r:arith, r=Alexendoo
[arithmetic-side-effects] Add more tests Taken from the `integer-arithmetic` lint. changelog: [arithmetic-side-effects] Add more tests
2 parents 5c3c6a2 + 736d88b commit 1f66a3e

File tree

2 files changed

+65
-17
lines changed

2 files changed

+65
-17
lines changed

tests/ui/arithmetic_side_effects.rs

+45-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,39 @@
1111

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

14+
pub fn association_with_structures_should_not_trigger_the_lint() {
15+
enum Foo {
16+
Bar = -2,
17+
}
18+
19+
impl Trait for Foo {
20+
const ASSOC: i32 = {
21+
let _: [i32; 1 + 1];
22+
fn foo() {}
23+
1 + 1
24+
};
25+
}
26+
27+
struct Baz([i32; 1 + 1]);
28+
29+
trait Trait {
30+
const ASSOC: i32 = 1 + 1;
31+
}
32+
33+
type Alias = [i32; 1 + 1];
34+
35+
union Qux {
36+
field: [i32; 1 + 1],
37+
}
38+
39+
let _: [i32; 1 + 1] = [0, 0];
40+
41+
let _: [i32; 1 + 1] = {
42+
let a: [i32; 1 + 1] = [0, 0];
43+
a
44+
};
45+
}
46+
1447
pub fn hard_coded_allowed() {
1548
let _ = 1f32 + 1f32;
1649
let _ = 1f64 + 1f64;
@@ -33,7 +66,7 @@ pub fn hard_coded_allowed() {
3366
}
3467

3568
#[rustfmt::skip]
36-
pub fn non_overflowing_const_ops() {
69+
pub fn const_ops_should_not_trigger_the_lint() {
3770
const _: i32 = { let mut n = 1; n += 1; n };
3871
let _ = const { let mut n = 1; n += 1; n };
3972

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

4679
const _: i32 = 1 + 1;
4780
let _ = const { 1 + 1 };
81+
82+
const _: i32 = { let mut n = -1; n = -(-1); n = -n; n };
83+
let _ = const { let mut n = -1; n = -(-1); n = -n; n };
4884
}
4985

50-
pub fn non_overflowing_runtime_ops() {
86+
pub fn non_overflowing_runtime_ops_or_ops_already_handled_by_the_compiler() {
5187
let mut _n = i32::MAX;
5288

5389
// Assign
@@ -70,9 +106,12 @@ pub fn non_overflowing_runtime_ops() {
70106
_n = _n * 1;
71107
_n = 1 * _n;
72108
_n = 23 + 85;
109+
110+
// Unary
111+
_n = -1;
112+
_n = -(-1);
73113
}
74114

75-
#[rustfmt::skip]
76115
pub fn overflowing_runtime_ops() {
77116
let mut _n = i32::MAX;
78117

@@ -92,6 +131,9 @@ pub fn overflowing_runtime_ops() {
92131
_n = _n % 0;
93132
_n = _n * 2;
94133
_n = 2 * _n;
134+
135+
// Unary
136+
_n = -_n;
95137
}
96138

97139
fn main() {}
+20-14
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:119: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:120: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:121: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:122: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:123: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:126: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:127: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:128: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:129: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:130: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:131: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:132: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:133: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:136:10
83+
|
84+
LL | _n = -_n;
85+
| ^^^
86+
87+
error: aborting due to 14 previous errors
8288

0 commit comments

Comments
 (0)