Skip to content

Commit

Permalink
Add tests for new lint (modulo_arithmetic)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgr-inz-rafal committed Dec 26, 2019
1 parent 634de11 commit 5346954
Show file tree
Hide file tree
Showing 6 changed files with 565 additions and 0 deletions.
36 changes: 36 additions & 0 deletions tests/ui/modulo_arithmetic_float.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#![warn(clippy::modulo_arithmetic)]
#![allow(
unused,
clippy::shadow_reuse,
clippy::shadow_unrelated,
clippy::no_effect,
clippy::unnecessary_operation,
clippy::modulo_one
)]

fn main() {
// Lint when both sides are const and of the opposite sign
-1.6 % 2.1;
1.6 % -2.1;
(1.1 - 2.3) % (1.1 + 2.3);
(1.1 + 2.3) % (1.1 - 2.3);

// Lint on floating point numbers
let a_f32: f32 = -1.6;
let mut b_f32: f32 = 2.1;
a_f32 % b_f32;
b_f32 % a_f32;
b_f32 %= a_f32;

let a_f64: f64 = -1.6;
let mut b_f64: f64 = 2.1;
a_f64 % b_f64;
b_f64 % a_f64;
b_f64 %= a_f64;

// No lint when both sides are const and of the same sign
1.6 % 2.1;
-1.6 % -2.1;
(1.1 + 2.3) % (-1.1 + 2.3);
(-1.1 - 2.3) % (1.1 - 2.3);
}
83 changes: 83 additions & 0 deletions tests/ui/modulo_arithmetic_float.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
error: you are using modulo operator on constants with different signs: `-1.600 % 2.100`
--> $DIR/modulo_arithmetic_float.rs:13:5
|
LL | -1.6 % 2.1;
| ^^^^^^^^^^
|
= note: `-D clippy::modulo-arithmetic` implied by `-D warnings`
= note: double check for expected result especially when interoperating with different languages

error: you are using modulo operator on constants with different signs: `1.600 % -2.100`
--> $DIR/modulo_arithmetic_float.rs:14:5
|
LL | 1.6 % -2.1;
| ^^^^^^^^^^
|
= note: double check for expected result especially when interoperating with different languages

error: you are using modulo operator on constants with different signs: `-1.200 % 3.400`
--> $DIR/modulo_arithmetic_float.rs:15:5
|
LL | (1.1 - 2.3) % (1.1 + 2.3);
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: double check for expected result especially when interoperating with different languages

error: you are using modulo operator on constants with different signs: `3.400 % -1.200`
--> $DIR/modulo_arithmetic_float.rs:16:5
|
LL | (1.1 + 2.3) % (1.1 - 2.3);
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: double check for expected result especially when interoperating with different languages

error: you are using modulo operator on types that might have different signs
--> $DIR/modulo_arithmetic_float.rs:21:5
|
LL | a_f32 % b_f32;
| ^^^^^^^^^^^^^
|
= note: double check for expected result especially when interoperating with different languages

error: you are using modulo operator on types that might have different signs
--> $DIR/modulo_arithmetic_float.rs:22:5
|
LL | b_f32 % a_f32;
| ^^^^^^^^^^^^^
|
= note: double check for expected result especially when interoperating with different languages

error: you are using modulo operator on types that might have different signs
--> $DIR/modulo_arithmetic_float.rs:23:5
|
LL | b_f32 %= a_f32;
| ^^^^^^^^^^^^^^
|
= note: double check for expected result especially when interoperating with different languages

error: you are using modulo operator on types that might have different signs
--> $DIR/modulo_arithmetic_float.rs:27:5
|
LL | a_f64 % b_f64;
| ^^^^^^^^^^^^^
|
= note: double check for expected result especially when interoperating with different languages

error: you are using modulo operator on types that might have different signs
--> $DIR/modulo_arithmetic_float.rs:28:5
|
LL | b_f64 % a_f64;
| ^^^^^^^^^^^^^
|
= note: double check for expected result especially when interoperating with different languages

error: you are using modulo operator on types that might have different signs
--> $DIR/modulo_arithmetic_float.rs:29:5
|
LL | b_f64 %= a_f64;
| ^^^^^^^^^^^^^^
|
= note: double check for expected result especially when interoperating with different languages

error: aborting due to 10 previous errors

90 changes: 90 additions & 0 deletions tests/ui/modulo_arithmetic_integral.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#![warn(clippy::modulo_arithmetic)]
#![allow(
unused,
clippy::shadow_reuse,
clippy::shadow_unrelated,
clippy::no_effect,
clippy::unnecessary_operation,
clippy::modulo_one
)]

fn main() {
// Lint on signed integral numbers
let a = -1;
let mut b = 2;
a % b;
b % a;
b %= a;

let a_i8: i8 = 1;
let mut b_i8: i8 = 2;
a_i8 % b_i8;
b_i8 %= a_i8;

let a_i16: i16 = 1;
let mut b_i16: i16 = 2;
a_i16 % b_i16;
b_i16 %= a_i16;

let a_i32: i32 = 1;
let mut b_i32: i32 = 2;
a_i32 % b_i32;
b_i32 %= a_i32;

let a_i64: i64 = 1;
let mut b_i64: i64 = 2;
a_i64 % b_i64;
b_i64 %= a_i64;

let a_i128: i128 = 1;
let mut b_i128: i128 = 2;
a_i128 % b_i128;
b_i128 %= a_i128;

let a_isize: isize = 1;
let mut b_isize: isize = 2;
a_isize % b_isize;
b_isize %= a_isize;

let a = 1;
let mut b = 2;
a % b;
b %= a;

// No lint on unsigned integral value
let a_u8: u8 = 17;
let b_u8: u8 = 3;
a_u8 % b_u8;
let mut a_u8: u8 = 1;
a_u8 %= 2;

let a_u16: u16 = 17;
let b_u16: u16 = 3;
a_u16 % b_u16;
let mut a_u16: u16 = 1;
a_u16 %= 2;

let a_u32: u32 = 17;
let b_u32: u32 = 3;
a_u32 % b_u32;
let mut a_u32: u32 = 1;
a_u32 %= 2;

let a_u64: u64 = 17;
let b_u64: u64 = 3;
a_u64 % b_u64;
let mut a_u64: u64 = 1;
a_u64 %= 2;

let a_u128: u128 = 17;
let b_u128: u128 = 3;
a_u128 % b_u128;
let mut a_u128: u128 = 1;
a_u128 %= 2;

let a_usize: usize = 17;
let b_usize: usize = 3;
a_usize % b_usize;
let mut a_usize: usize = 1;
a_usize %= 2;
}
156 changes: 156 additions & 0 deletions tests/ui/modulo_arithmetic_integral.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
error: you are using modulo operator on types that might have different signs
--> $DIR/modulo_arithmetic_integral.rs:15:5
|
LL | a % b;
| ^^^^^
|
= note: `-D clippy::modulo-arithmetic` implied by `-D warnings`
= note: double check for expected result especially when interoperating with different languages
= note: or consider using `rem_euclid` or similar function

error: you are using modulo operator on types that might have different signs
--> $DIR/modulo_arithmetic_integral.rs:16:5
|
LL | b % a;
| ^^^^^
|
= note: double check for expected result especially when interoperating with different languages
= note: or consider using `rem_euclid` or similar function

error: you are using modulo operator on types that might have different signs
--> $DIR/modulo_arithmetic_integral.rs:17:5
|
LL | b %= a;
| ^^^^^^
|
= note: double check for expected result especially when interoperating with different languages
= note: or consider using `rem_euclid` or similar function

error: you are using modulo operator on types that might have different signs
--> $DIR/modulo_arithmetic_integral.rs:21:5
|
LL | a_i8 % b_i8;
| ^^^^^^^^^^^
|
= note: double check for expected result especially when interoperating with different languages
= note: or consider using `rem_euclid` or similar function

error: you are using modulo operator on types that might have different signs
--> $DIR/modulo_arithmetic_integral.rs:22:5
|
LL | b_i8 %= a_i8;
| ^^^^^^^^^^^^
|
= note: double check for expected result especially when interoperating with different languages
= note: or consider using `rem_euclid` or similar function

error: you are using modulo operator on types that might have different signs
--> $DIR/modulo_arithmetic_integral.rs:26:5
|
LL | a_i16 % b_i16;
| ^^^^^^^^^^^^^
|
= note: double check for expected result especially when interoperating with different languages
= note: or consider using `rem_euclid` or similar function

error: you are using modulo operator on types that might have different signs
--> $DIR/modulo_arithmetic_integral.rs:27:5
|
LL | b_i16 %= a_i16;
| ^^^^^^^^^^^^^^
|
= note: double check for expected result especially when interoperating with different languages
= note: or consider using `rem_euclid` or similar function

error: you are using modulo operator on types that might have different signs
--> $DIR/modulo_arithmetic_integral.rs:31:5
|
LL | a_i32 % b_i32;
| ^^^^^^^^^^^^^
|
= note: double check for expected result especially when interoperating with different languages
= note: or consider using `rem_euclid` or similar function

error: you are using modulo operator on types that might have different signs
--> $DIR/modulo_arithmetic_integral.rs:32:5
|
LL | b_i32 %= a_i32;
| ^^^^^^^^^^^^^^
|
= note: double check for expected result especially when interoperating with different languages
= note: or consider using `rem_euclid` or similar function

error: you are using modulo operator on types that might have different signs
--> $DIR/modulo_arithmetic_integral.rs:36:5
|
LL | a_i64 % b_i64;
| ^^^^^^^^^^^^^
|
= note: double check for expected result especially when interoperating with different languages
= note: or consider using `rem_euclid` or similar function

error: you are using modulo operator on types that might have different signs
--> $DIR/modulo_arithmetic_integral.rs:37:5
|
LL | b_i64 %= a_i64;
| ^^^^^^^^^^^^^^
|
= note: double check for expected result especially when interoperating with different languages
= note: or consider using `rem_euclid` or similar function

error: you are using modulo operator on types that might have different signs
--> $DIR/modulo_arithmetic_integral.rs:41:5
|
LL | a_i128 % b_i128;
| ^^^^^^^^^^^^^^^
|
= note: double check for expected result especially when interoperating with different languages
= note: or consider using `rem_euclid` or similar function

error: you are using modulo operator on types that might have different signs
--> $DIR/modulo_arithmetic_integral.rs:42:5
|
LL | b_i128 %= a_i128;
| ^^^^^^^^^^^^^^^^
|
= note: double check for expected result especially when interoperating with different languages
= note: or consider using `rem_euclid` or similar function

error: you are using modulo operator on types that might have different signs
--> $DIR/modulo_arithmetic_integral.rs:46:5
|
LL | a_isize % b_isize;
| ^^^^^^^^^^^^^^^^^
|
= note: double check for expected result especially when interoperating with different languages
= note: or consider using `rem_euclid` or similar function

error: you are using modulo operator on types that might have different signs
--> $DIR/modulo_arithmetic_integral.rs:47:5
|
LL | b_isize %= a_isize;
| ^^^^^^^^^^^^^^^^^^
|
= note: double check for expected result especially when interoperating with different languages
= note: or consider using `rem_euclid` or similar function

error: you are using modulo operator on types that might have different signs
--> $DIR/modulo_arithmetic_integral.rs:51:5
|
LL | a % b;
| ^^^^^
|
= note: double check for expected result especially when interoperating with different languages
= note: or consider using `rem_euclid` or similar function

error: you are using modulo operator on types that might have different signs
--> $DIR/modulo_arithmetic_integral.rs:52:5
|
LL | b %= a;
| ^^^^^^
|
= note: double check for expected result especially when interoperating with different languages
= note: or consider using `rem_euclid` or similar function

error: aborting due to 17 previous errors

Loading

0 comments on commit 5346954

Please sign in to comment.