Don't warn about modulo arithmetic when comparing to zero

Add lint configuration for `modulo_arithmetic`

Collect meta-data
This commit is contained in:
Marc Dominik Migge 2024-01-19 17:05:14 +01:00
parent 900a5aa036
commit e456c28e11
11 changed files with 122 additions and 5 deletions

View file

@ -0,0 +1 @@
allow-comparison-to-zero = false

View file

@ -0,0 +1,10 @@
#![warn(clippy::modulo_arithmetic)]
fn main() {
let a = -1;
let b = 2;
let c = a % b == 0;
let c = a % b != 0;
let c = 0 == a % b;
let c = 0 != a % b;
}

View file

@ -0,0 +1,40 @@
error: you are using modulo operator on types that might have different signs
--> $DIR/modulo_arithmetic.rs:6:13
|
LL | let c = a % b == 0;
| ^^^^^
|
= note: double check for expected result especially when interoperating with different languages
= note: or consider using `rem_euclid` or similar function
= note: `-D clippy::modulo-arithmetic` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::modulo_arithmetic)]`
error: you are using modulo operator on types that might have different signs
--> $DIR/modulo_arithmetic.rs:7:13
|
LL | let c = a % b != 0;
| ^^^^^
|
= 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.rs:8:18
|
LL | let c = 0 == 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.rs:9:18
|
LL | let c = 0 != a % b;
| ^^^^^
|
= 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 4 previous errors

View file

@ -3,6 +3,7 @@ error: error reading Clippy's configuration file: unknown field `foobar`, expect
absolute-paths-max-segments
accept-comment-above-attributes
accept-comment-above-statement
allow-comparison-to-zero
allow-dbg-in-tests
allow-expect-in-tests
allow-mixed-uninlined-format-args
@ -80,6 +81,7 @@ error: error reading Clippy's configuration file: unknown field `barfoo`, expect
absolute-paths-max-segments
accept-comment-above-attributes
accept-comment-above-statement
allow-comparison-to-zero
allow-dbg-in-tests
allow-expect-in-tests
allow-mixed-uninlined-format-args
@ -157,6 +159,7 @@ error: error reading Clippy's configuration file: unknown field `allow_mixed_uni
absolute-paths-max-segments
accept-comment-above-attributes
accept-comment-above-statement
allow-comparison-to-zero
allow-dbg-in-tests
allow-expect-in-tests
allow-mixed-uninlined-format-args

View file

@ -114,4 +114,12 @@ fn main() {
a_usize % b_usize;
let mut a_usize: usize = 1;
a_usize %= 2;
// 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;
}