about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCaio <c410.f3r@gmail.com>2022-09-20 12:06:49 -0300
committerCaio <c410.f3r@gmail.com>2022-09-20 12:06:49 -0300
commit736d88b5499e771ed7ca9ff2e874ea2f11503285 (patch)
treec34d819ba7e5a336ecf2d85aa2057495c0f844d3
parent5c3c6a2de666525e8c32dbc90072a065143c5450 (diff)
downloadrust-736d88b5499e771ed7ca9ff2e874ea2f11503285.tar.gz
rust-736d88b5499e771ed7ca9ff2e874ea2f11503285.zip
[arithmetic_side_effects] Add more tests
-rw-r--r--tests/ui/arithmetic_side_effects.rs48
-rw-r--r--tests/ui/arithmetic_side_effects.stderr34
2 files changed, 65 insertions, 17 deletions
diff --git a/tests/ui/arithmetic_side_effects.rs b/tests/ui/arithmetic_side_effects.rs
index c07425be50a..dd24f5aa592 100644
--- a/tests/ui/arithmetic_side_effects.rs
+++ b/tests/ui/arithmetic_side_effects.rs
@@ -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;
@@ -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 };
 
@@ -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
@@ -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;
 
@@ -92,6 +131,9 @@ pub fn overflowing_runtime_ops() {
     _n = _n % 0;
     _n = _n * 2;
     _n = 2 * _n;
+
+    // Unary
+    _n = -_n;
 }
 
 fn main() {}
diff --git a/tests/ui/arithmetic_side_effects.stderr b/tests/ui/arithmetic_side_effects.stderr
index 2f953d26699..a2a856efbff 100644
--- a/tests/ui/arithmetic_side_effects.stderr
+++ b/tests/ui/arithmetic_side_effects.stderr
@@ -1,5 +1,5 @@
 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;
    |     ^^^^^^^
@@ -7,76 +7,82 @@ 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