2019-12-26 12:34:55 +00:00
|
|
|
#![warn(clippy::modulo_arithmetic)]
|
2021-09-27 22:19:33 +00:00
|
|
|
#![allow(clippy::no_effect, clippy::unnecessary_operation, clippy::modulo_one)]
|
2019-12-26 12:34:55 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
// Lint on signed integral numbers
|
|
|
|
let a = -1;
|
|
|
|
let mut b = 2;
|
|
|
|
a % b;
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: you are using modulo operator on types that might have different signs
|
|
|
|
//~| NOTE: double check for expected result especially when interoperating with differ
|
2019-12-26 12:34:55 +00:00
|
|
|
b % a;
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: you are using modulo operator on types that might have different signs
|
|
|
|
//~| NOTE: double check for expected result especially when interoperating with differ
|
2019-12-26 12:34:55 +00:00
|
|
|
b %= a;
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: you are using modulo operator on types that might have different signs
|
|
|
|
//~| NOTE: double check for expected result especially when interoperating with differ
|
2019-12-26 12:34:55 +00:00
|
|
|
|
|
|
|
let a_i8: i8 = 1;
|
|
|
|
let mut b_i8: i8 = 2;
|
|
|
|
a_i8 % b_i8;
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: you are using modulo operator on types that might have different signs
|
|
|
|
//~| NOTE: double check for expected result especially when interoperating with differ
|
2019-12-26 12:34:55 +00:00
|
|
|
b_i8 %= a_i8;
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: you are using modulo operator on types that might have different signs
|
|
|
|
//~| NOTE: double check for expected result especially when interoperating with differ
|
2019-12-26 12:34:55 +00:00
|
|
|
|
|
|
|
let a_i16: i16 = 1;
|
|
|
|
let mut b_i16: i16 = 2;
|
|
|
|
a_i16 % b_i16;
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: you are using modulo operator on types that might have different signs
|
|
|
|
//~| NOTE: double check for expected result especially when interoperating with differ
|
2019-12-26 12:34:55 +00:00
|
|
|
b_i16 %= a_i16;
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: you are using modulo operator on types that might have different signs
|
|
|
|
//~| NOTE: double check for expected result especially when interoperating with differ
|
2019-12-26 12:34:55 +00:00
|
|
|
|
|
|
|
let a_i32: i32 = 1;
|
|
|
|
let mut b_i32: i32 = 2;
|
|
|
|
a_i32 % b_i32;
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: you are using modulo operator on types that might have different signs
|
|
|
|
//~| NOTE: double check for expected result especially when interoperating with differ
|
2019-12-26 12:34:55 +00:00
|
|
|
b_i32 %= a_i32;
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: you are using modulo operator on types that might have different signs
|
|
|
|
//~| NOTE: double check for expected result especially when interoperating with differ
|
2019-12-26 12:34:55 +00:00
|
|
|
|
|
|
|
let a_i64: i64 = 1;
|
|
|
|
let mut b_i64: i64 = 2;
|
|
|
|
a_i64 % b_i64;
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: you are using modulo operator on types that might have different signs
|
|
|
|
//~| NOTE: double check for expected result especially when interoperating with differ
|
2019-12-26 12:34:55 +00:00
|
|
|
b_i64 %= a_i64;
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: you are using modulo operator on types that might have different signs
|
|
|
|
//~| NOTE: double check for expected result especially when interoperating with differ
|
2019-12-26 12:34:55 +00:00
|
|
|
|
|
|
|
let a_i128: i128 = 1;
|
|
|
|
let mut b_i128: i128 = 2;
|
|
|
|
a_i128 % b_i128;
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: you are using modulo operator on types that might have different signs
|
|
|
|
//~| NOTE: double check for expected result especially when interoperating with differ
|
2019-12-26 12:34:55 +00:00
|
|
|
b_i128 %= a_i128;
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: you are using modulo operator on types that might have different signs
|
|
|
|
//~| NOTE: double check for expected result especially when interoperating with differ
|
2019-12-26 12:34:55 +00:00
|
|
|
|
|
|
|
let a_isize: isize = 1;
|
|
|
|
let mut b_isize: isize = 2;
|
|
|
|
a_isize % b_isize;
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: you are using modulo operator on types that might have different signs
|
|
|
|
//~| NOTE: double check for expected result especially when interoperating with differ
|
2019-12-26 12:34:55 +00:00
|
|
|
b_isize %= a_isize;
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: you are using modulo operator on types that might have different signs
|
|
|
|
//~| NOTE: double check for expected result especially when interoperating with differ
|
2019-12-26 12:34:55 +00:00
|
|
|
|
|
|
|
let a = 1;
|
|
|
|
let mut b = 2;
|
|
|
|
a % b;
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: you are using modulo operator on types that might have different signs
|
|
|
|
//~| NOTE: double check for expected result especially when interoperating with differ
|
2019-12-26 12:34:55 +00:00
|
|
|
b %= a;
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: you are using modulo operator on types that might have different signs
|
|
|
|
//~| NOTE: double check for expected result especially when interoperating with differ
|
2019-12-26 12:34:55 +00:00
|
|
|
|
|
|
|
// 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;
|
2024-01-19 16:05:14 +00:00
|
|
|
|
|
|
|
// No lint when comparing to zero
|
|
|
|
let a = -1;
|
|
|
|
let mut b = 2;
|
|
|
|
let c = a % b == 0;
|
|
|
|
let c = 0 == a % b;
|
|
|
|
let c = a % b != 0;
|
|
|
|
let c = 0 != a % b;
|
2019-12-26 12:34:55 +00:00
|
|
|
}
|