diff --git a/tests/ui/implicit_saturating_sub.rs b/tests/ui/implicit_saturating_sub.rs index bf75555ca98e..ce3bbede99e6 100644 --- a/tests/ui/implicit_saturating_sub.rs +++ b/tests/ui/implicit_saturating_sub.rs @@ -1,53 +1,56 @@ #![warn(clippy::implicit_saturating_sub)] fn main() { + + // Tests for unsigned integers + let end_8: u8 = 10; let start_8: u8 = 5; - let mut i_8: u8 = end_8 - start_8; + let mut u_8: u8 = end_8 - start_8; // Lint - if i_8 > 0 { - i_8 -= 1; + if u_8 > 0 { + u_8 -= 1; } match end_8 { 10 => { // Lint - if i_8 > 0 { - i_8 -= 1; + if u_8 > 0 { + u_8 -= 1; } }, - 11 => i_8 += 1, - _ => i_8 = 0, + 11 => u_8 += 1, + _ => u_8 = 0, } let end_16: u16 = 35; let start_16: u16 = 40; - let mut i_16: u16 = end_16 - start_16; + let mut u_16: u16 = end_16 - start_16; // Lint - if i_16 > 0 { - i_16 -= 1; + if u_16 > 0 { + u_16 -= 1; } let mut end_32: u32 = 7000; let mut start_32: u32 = 7010; - let mut i_32: u32 = end_32 - start_32; + let mut u_32: u32 = end_32 - start_32; // Lint - if i_32 != 0 { - i_32 -= 1; + if u_32 != 0 { + u_32 -= 1; } // No Lint - if i_32 > 0 { - i_16 += 1; + if u_32 > 0 { + u_16 += 1; } // No Lint - if i_32 != 0 { + if u_32 != 0 { end_32 -= 1; start_32 += 1; } @@ -55,31 +58,73 @@ fn main() { let mut end_64: u64 = 75001; let mut start_64: u64 = 75000; - let mut i_64: u64 = end_64 - start_64; + let mut u_64: u64 = end_64 - start_64; // Lint - if i_64 > 0 { - i_64 -= 1; + if u_64 > 0 { + u_64 -= 1; } // No Lint - if i_64 >= 1 { - i_64 -= 1; + if u_64 >= 1 { + u_64 -= 1; } // No Lint - if i_64 > 0 { + if u_64 > 0 { end_64 -= 1; } - // Should signed integers trigger the lint ? - // let endi_64: i64 = 45; - // let starti_64: i64 = 44; + // Tests for usize + let end_usize: usize = 8054; + let start_usize: usize = 8050; + + let mut u_usize: usize = end_usize - start_usize; + + // Lint + if u_usize > 0 { + u_usize -= 1; + } + + // Tests for signed integers + + let endi_8: i8 = 10; + let starti_8: i8 = 50; + + let mut i_8: i8 = endi_8 - starti_8; + + // Lint + if i_8 > i8::MIN { + i_8 -= 1; + } + + let endi_16: i16 = 45; + let starti_16: i16 = 44; - // let mut ii_64 = endi_64 - starti_64; + let mut i_16: i16 = endi_16 - starti_16; - // Should this produce a warning - // if ii_64 > 0 { - // ii_64 -= 1; - // } + // Lint + if i_16 > i16::MIN { + i_16 -= 1; + } + + let endi_32: i32 = 45; + let starti_32: i32 = 44; + + let mut i_32: i32 = endi_32 - starti_32; + + // Lint + if i_32 > i32::MIN { + i_32 -= 1; + } + + let endi_64: i64 = 45; + let starti_64: i64 = 44; + + let mut i_64: i64 = endi_64 - starti_64; + + // Lint + if i_64 > i64::MIN { + i_64 -= 1; + } } diff --git a/tests/ui/implicit_saturating_sub.stderr b/tests/ui/implicit_saturating_sub.stderr index f5a81c220270..1a0d4f31363a 100644 --- a/tests/ui/implicit_saturating_sub.stderr +++ b/tests/ui/implicit_saturating_sub.stderr @@ -1,44 +1,84 @@ error: Implicitly performing saturating subtraction - --> $DIR/implicit_saturating_sub.rs:9:5 + --> $DIR/implicit_saturating_sub.rs:12:5 | -LL | / if i_8 > 0 { -LL | | i_8 -= 1; +LL | / if u_8 > 0 { +LL | | u_8 -= 1; LL | | } - | |_____^ help: try: `i_8 = i_8.saturating_sub(1);` + | |_____^ help: try: `u_8 = u_8.saturating_sub(1);` | = note: `-D clippy::implicit-saturating-sub` implied by `-D warnings` error: Implicitly performing saturating subtraction - --> $DIR/implicit_saturating_sub.rs:16:13 + --> $DIR/implicit_saturating_sub.rs:19:13 | -LL | / if i_8 > 0 { -LL | | i_8 -= 1; +LL | / if u_8 > 0 { +LL | | u_8 -= 1; LL | | } - | |_____________^ help: try: `i_8 = i_8.saturating_sub(1);` + | |_____________^ help: try: `u_8 = u_8.saturating_sub(1);` + +error: Implicitly performing saturating subtraction + --> $DIR/implicit_saturating_sub.rs:33:5 + | +LL | / if u_16 > 0 { +LL | | u_16 -= 1; +LL | | } + | |_____^ help: try: `u_16 = u_16.saturating_sub(1);` + +error: Implicitly performing saturating subtraction + --> $DIR/implicit_saturating_sub.rs:43:5 + | +LL | / if u_32 != 0 { +LL | | u_32 -= 1; +LL | | } + | |_____^ help: try: `u_32 = u_32.saturating_sub(1);` + +error: Implicitly performing saturating subtraction + --> $DIR/implicit_saturating_sub.rs:64:5 + | +LL | / if u_64 > 0 { +LL | | u_64 -= 1; +LL | | } + | |_____^ help: try: `u_64 = u_64.saturating_sub(1);` + +error: Implicitly performing saturating subtraction + --> $DIR/implicit_saturating_sub.rs:85:5 + | +LL | / if u_usize > 0 { +LL | | u_usize -= 1; +LL | | } + | |_____^ help: try: `u_usize = u_usize.saturating_sub(1);` + +error: Implicitly performing saturating subtraction + --> $DIR/implicit_saturating_sub.rs:97:5 + | +LL | / if i_8 > i8::MIN { +LL | | i_8 -= 1; +LL | | } + | |_____^ help: try: `i_8 = i_8.saturating_sub(1);` error: Implicitly performing saturating subtraction - --> $DIR/implicit_saturating_sub.rs:30:5 + --> $DIR/implicit_saturating_sub.rs:107:5 | -LL | / if i_16 > 0 { +LL | / if i_16 > i16::MIN { LL | | i_16 -= 1; LL | | } | |_____^ help: try: `i_16 = i_16.saturating_sub(1);` error: Implicitly performing saturating subtraction - --> $DIR/implicit_saturating_sub.rs:40:5 + --> $DIR/implicit_saturating_sub.rs:117:5 | -LL | / if i_32 != 0 { +LL | / if i_32 > i32::MIN { LL | | i_32 -= 1; LL | | } | |_____^ help: try: `i_32 = i_32.saturating_sub(1);` error: Implicitly performing saturating subtraction - --> $DIR/implicit_saturating_sub.rs:61:5 + --> $DIR/implicit_saturating_sub.rs:127:5 | -LL | / if i_64 > 0 { +LL | / if i_64 > i64::MIN { LL | | i_64 -= 1; LL | | } | |_____^ help: try: `i_64 = i_64.saturating_sub(1);` -error: aborting due to 5 previous errors +error: aborting due to 10 previous errors