tests: arithmetic: split into integer_arithmetic and float_arithmetic files.

This commit is contained in:
Matthias Krüger 2020-03-18 15:41:42 +01:00
parent f041dcdb4e
commit ec1dcde46b
5 changed files with 266 additions and 255 deletions

View File

@ -1,211 +0,0 @@
error: integer arithmetic detected
--> $DIR/arithmetic.rs:15:5
|
LL | 1 + i;
| ^^^^^
|
= note: `-D clippy::integer-arithmetic` implied by `-D warnings`
error: integer arithmetic detected
--> $DIR/arithmetic.rs:16:5
|
LL | i * 2;
| ^^^^^
error: integer arithmetic detected
--> $DIR/arithmetic.rs:17:5
|
LL | / 1 %
LL | | i / 2; // no error, this is part of the expression in the preceding line
| |_________^
error: integer arithmetic detected
--> $DIR/arithmetic.rs:19:5
|
LL | i - 2 + 2 - i;
| ^^^^^^^^^^^^^
error: integer arithmetic detected
--> $DIR/arithmetic.rs:20:5
|
LL | -i;
| ^^
error: integer arithmetic detected
--> $DIR/arithmetic.rs:32:5
|
LL | i += 1;
| ^^^^^^
error: integer arithmetic detected
--> $DIR/arithmetic.rs:33:5
|
LL | i -= 1;
| ^^^^^^
error: integer arithmetic detected
--> $DIR/arithmetic.rs:34:5
|
LL | i *= 2;
| ^^^^^^
error: integer arithmetic detected
--> $DIR/arithmetic.rs:35:5
|
LL | i /= 2;
| ^^^^^^
error: integer arithmetic detected
--> $DIR/arithmetic.rs:36:5
|
LL | i %= 2;
| ^^^^^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:47:5
|
LL | f * 2.0;
| ^^^^^^^
|
= note: `-D clippy::float-arithmetic` implied by `-D warnings`
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:49:5
|
LL | 1.0 + f;
| ^^^^^^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:50:5
|
LL | f * 2.0;
| ^^^^^^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:51:5
|
LL | f / 2.0;
| ^^^^^^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:52:5
|
LL | f - 2.0 * 4.2;
| ^^^^^^^^^^^^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:53:5
|
LL | -f;
| ^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:55:5
|
LL | f += 1.0;
| ^^^^^^^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:56:5
|
LL | f -= 1.0;
| ^^^^^^^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:57:5
|
LL | f *= 2.0;
| ^^^^^^^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:58:5
|
LL | f /= 2.0;
| ^^^^^^^^
error: integer arithmetic detected
--> $DIR/arithmetic.rs:101:5
|
LL | 3 + &1;
| ^^^^^^
error: integer arithmetic detected
--> $DIR/arithmetic.rs:102:5
|
LL | &3 + 1;
| ^^^^^^
error: integer arithmetic detected
--> $DIR/arithmetic.rs:103:5
|
LL | &3 + &1;
| ^^^^^^^
error: integer arithmetic detected
--> $DIR/arithmetic.rs:108:5
|
LL | a + x
| ^^^^^
error: integer arithmetic detected
--> $DIR/arithmetic.rs:112:5
|
LL | x + y
| ^^^^^
error: integer arithmetic detected
--> $DIR/arithmetic.rs:116:5
|
LL | x + y
| ^^^^^
error: integer arithmetic detected
--> $DIR/arithmetic.rs:120:5
|
LL | (&x + &y)
| ^^^^^^^^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:126:5
|
LL | 3.1_f32 + &1.2_f32;
| ^^^^^^^^^^^^^^^^^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:127:5
|
LL | &3.4_f32 + 1.5_f32;
| ^^^^^^^^^^^^^^^^^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:128:5
|
LL | &3.5_f32 + &1.3_f32;
| ^^^^^^^^^^^^^^^^^^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:133:5
|
LL | a + f
| ^^^^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:137:5
|
LL | f1 + f2
| ^^^^^^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:141:5
|
LL | f1 + f2
| ^^^^^^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:145:5
|
LL | (&f1 + &f2)
| ^^^^^^^^^^^
error: aborting due to 34 previous errors

View File

@ -0,0 +1,53 @@
#![warn(clippy::integer_arithmetic, clippy::float_arithmetic)]
#![allow(
unused,
clippy::shadow_reuse,
clippy::shadow_unrelated,
clippy::no_effect,
clippy::unnecessary_operation,
clippy::op_ref,
clippy::trivially_copy_pass_by_ref
)]
#[rustfmt::skip]
fn main() {
let mut f = 1.0f32;
f * 2.0;
1.0 + f;
f * 2.0;
f / 2.0;
f - 2.0 * 4.2;
-f;
f += 1.0;
f -= 1.0;
f *= 2.0;
f /= 2.0;
}
// also warn about floating point arith with references involved
pub fn float_arith_ref() {
3.1_f32 + &1.2_f32;
&3.4_f32 + 1.5_f32;
&3.5_f32 + &1.3_f32;
}
pub fn float_foo(f: &f32) -> f32 {
let a = 5.1;
a + f
}
pub fn float_bar(f1: &f32, f2: &f32) -> f32 {
f1 + f2
}
pub fn float_baz(f1: f32, f2: &f32) -> f32 {
f1 + f2
}
pub fn float_qux(f1: f32, f2: f32) -> f32 {
(&f1 + &f2)
}

View File

@ -0,0 +1,106 @@
error: floating-point arithmetic detected
--> $DIR/float_arithmetic.rs:16:5
|
LL | f * 2.0;
| ^^^^^^^
|
= note: `-D clippy::float-arithmetic` implied by `-D warnings`
error: floating-point arithmetic detected
--> $DIR/float_arithmetic.rs:18:5
|
LL | 1.0 + f;
| ^^^^^^^
error: floating-point arithmetic detected
--> $DIR/float_arithmetic.rs:19:5
|
LL | f * 2.0;
| ^^^^^^^
error: floating-point arithmetic detected
--> $DIR/float_arithmetic.rs:20:5
|
LL | f / 2.0;
| ^^^^^^^
error: floating-point arithmetic detected
--> $DIR/float_arithmetic.rs:21:5
|
LL | f - 2.0 * 4.2;
| ^^^^^^^^^^^^^
error: floating-point arithmetic detected
--> $DIR/float_arithmetic.rs:22:5
|
LL | -f;
| ^^
error: floating-point arithmetic detected
--> $DIR/float_arithmetic.rs:24:5
|
LL | f += 1.0;
| ^^^^^^^^
error: floating-point arithmetic detected
--> $DIR/float_arithmetic.rs:25:5
|
LL | f -= 1.0;
| ^^^^^^^^
error: floating-point arithmetic detected
--> $DIR/float_arithmetic.rs:26:5
|
LL | f *= 2.0;
| ^^^^^^^^
error: floating-point arithmetic detected
--> $DIR/float_arithmetic.rs:27:5
|
LL | f /= 2.0;
| ^^^^^^^^
error: floating-point arithmetic detected
--> $DIR/float_arithmetic.rs:33:5
|
LL | 3.1_f32 + &1.2_f32;
| ^^^^^^^^^^^^^^^^^^
error: floating-point arithmetic detected
--> $DIR/float_arithmetic.rs:34:5
|
LL | &3.4_f32 + 1.5_f32;
| ^^^^^^^^^^^^^^^^^^
error: floating-point arithmetic detected
--> $DIR/float_arithmetic.rs:35:5
|
LL | &3.5_f32 + &1.3_f32;
| ^^^^^^^^^^^^^^^^^^^
error: floating-point arithmetic detected
--> $DIR/float_arithmetic.rs:40:5
|
LL | a + f
| ^^^^^
error: floating-point arithmetic detected
--> $DIR/float_arithmetic.rs:44:5
|
LL | f1 + f2
| ^^^^^^^
error: floating-point arithmetic detected
--> $DIR/float_arithmetic.rs:48:5
|
LL | f1 + f2
| ^^^^^^^
error: floating-point arithmetic detected
--> $DIR/float_arithmetic.rs:52:5
|
LL | (&f1 + &f2)
| ^^^^^^^^^^^
error: aborting due to 17 previous errors

View File

@ -42,25 +42,6 @@ fn main() {
i &= 1;
i ^= i;
let mut f = 1.0f32;
f * 2.0;
1.0 + f;
f * 2.0;
f / 2.0;
f - 2.0 * 4.2;
-f;
f += 1.0;
f -= 1.0;
f *= 2.0;
f /= 2.0;
// no error, overflows are checked by `overflowing_literals`
-1.;
-(-1.);
// No errors for the following items because they are constant expressions
enum Foo {
Bar = -2,
@ -119,28 +100,3 @@ pub fn baz(x: i32, y: &i32) -> i32 {
pub fn qux(x: i32, y: i32) -> i32 {
(&x + &y)
}
// also warn about floating point arith with references involved
pub fn float_arith_ref() {
3.1_f32 + &1.2_f32;
&3.4_f32 + 1.5_f32;
&3.5_f32 + &1.3_f32;
}
pub fn float_foo(f: &f32) -> f32 {
let a = 5.1;
a + f
}
pub fn float_bar(f1: &f32, f2: &f32) -> f32 {
f1 + f2
}
pub fn float_baz(f1: f32, f2: &f32) -> f32 {
f1 + f2
}
pub fn float_qux(f1: f32, f2: f32) -> f32 {
(&f1 + &f2)
}

View File

@ -0,0 +1,107 @@
error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:15:5
|
LL | 1 + i;
| ^^^^^
|
= note: `-D clippy::integer-arithmetic` implied by `-D warnings`
error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:16:5
|
LL | i * 2;
| ^^^^^
error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:17:5
|
LL | / 1 %
LL | | i / 2; // no error, this is part of the expression in the preceding line
| |_________^
error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:19:5
|
LL | i - 2 + 2 - i;
| ^^^^^^^^^^^^^
error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:20:5
|
LL | -i;
| ^^
error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:32:5
|
LL | i += 1;
| ^^^^^^
error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:33:5
|
LL | i -= 1;
| ^^^^^^
error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:34:5
|
LL | i *= 2;
| ^^^^^^
error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:35:5
|
LL | i /= 2;
| ^^^^^^
error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:36:5
|
LL | i %= 2;
| ^^^^^^
error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:82:5
|
LL | 3 + &1;
| ^^^^^^
error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:83:5
|
LL | &3 + 1;
| ^^^^^^
error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:84:5
|
LL | &3 + &1;
| ^^^^^^^
error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:89:5
|
LL | a + x
| ^^^^^
error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:93:5
|
LL | x + y
| ^^^^^
error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:97:5
|
LL | x + y
| ^^^^^
error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:101:5
|
LL | (&x + &y)
| ^^^^^^^^^
error: aborting due to 17 previous errors