2024-07-26 06:41:51 -05:00
|
|
|
#![warn(clippy::non_zero_suggestions)]
|
|
|
|
use std::num::{NonZeroI16, NonZeroI8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize};
|
|
|
|
|
|
|
|
fn main() {
|
2024-07-26 16:45:40 -05:00
|
|
|
/// Positive test cases (lint should trigger)
|
2024-07-26 06:41:51 -05:00
|
|
|
// U32 -> U64
|
|
|
|
let x: u64 = 100;
|
|
|
|
let y = NonZeroU32::new(10).unwrap();
|
|
|
|
let r1 = x / NonZeroU64::from(y);
|
2024-07-26 16:45:40 -05:00
|
|
|
//~^ ERROR: consider using `NonZeroU64::from()` for more efficient and type-safe conversion
|
|
|
|
|
2024-07-26 06:41:51 -05:00
|
|
|
let r2 = x % NonZeroU64::from(y);
|
2024-07-26 16:45:40 -05:00
|
|
|
//~^ ERROR: consider using `NonZeroU64::from()` for more efficient and type-safe conversion
|
2024-07-26 06:41:51 -05:00
|
|
|
|
|
|
|
// U16 -> U32
|
|
|
|
let a: u32 = 50;
|
|
|
|
let b = NonZeroU16::new(5).unwrap();
|
|
|
|
let r3 = a / NonZeroU32::from(b);
|
2024-07-26 16:45:40 -05:00
|
|
|
//~^ ERROR: consider using `NonZeroU32::from()` for more efficient and type-safe conversion
|
2024-07-26 06:41:51 -05:00
|
|
|
|
2024-08-01 06:59:07 -05:00
|
|
|
let x = NonZeroU64::from(NonZeroU32::new(5).unwrap());
|
2024-07-26 16:45:40 -05:00
|
|
|
//~^ ERROR: consider using `NonZeroU64::from()` for more efficient and type-safe conversion
|
2024-07-26 06:41:51 -05:00
|
|
|
|
2024-08-01 06:59:07 -05:00
|
|
|
/// Negative test cases (lint should not trigger)
|
|
|
|
// Left hand side expressions should not be triggered
|
|
|
|
let c: u32 = 50;
|
|
|
|
let d = NonZeroU16::new(5).unwrap();
|
|
|
|
let r4 = u32::from(b.get()) / a;
|
2024-07-26 06:41:51 -05:00
|
|
|
|
2024-08-01 06:59:07 -05:00
|
|
|
// Should not trigger for any other operand other than `/` and `%`
|
|
|
|
let r5 = a + u32::from(b.get());
|
|
|
|
let r6 = a - u32::from(b.get());
|
2024-07-26 06:41:51 -05:00
|
|
|
|
|
|
|
// Same size types
|
|
|
|
let e: u32 = 200;
|
|
|
|
let f = NonZeroU32::new(20).unwrap();
|
2024-08-01 06:59:07 -05:00
|
|
|
let r7 = e / f.get();
|
2024-07-26 06:41:51 -05:00
|
|
|
|
|
|
|
// Smaller to larger, but not NonZero
|
|
|
|
let g: u64 = 1000;
|
|
|
|
let h: u32 = 50;
|
2024-08-01 06:59:07 -05:00
|
|
|
let r8 = g / u64::from(h);
|
2024-07-26 06:41:51 -05:00
|
|
|
|
|
|
|
// Using From correctly
|
|
|
|
let k: u64 = 300;
|
|
|
|
let l = NonZeroU32::new(15).unwrap();
|
2024-08-01 06:59:07 -05:00
|
|
|
let r9 = k / NonZeroU64::from(l);
|
2024-07-26 06:41:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Additional function to test the lint in a different context
|
|
|
|
fn divide_numbers(x: u64, y: NonZeroU32) -> u64 {
|
|
|
|
x / NonZeroU64::from(y)
|
2024-07-26 16:45:40 -05:00
|
|
|
//~^ ERROR: consider using `NonZeroU64::from()` for more efficient and type-safe conversion
|
2024-07-26 06:41:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Calculator {
|
|
|
|
value: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Calculator {
|
|
|
|
fn divide(&self, divisor: NonZeroU32) -> u64 {
|
|
|
|
self.value / NonZeroU64::from(divisor)
|
2024-08-01 06:59:07 -05:00
|
|
|
//~^ ERROR: consider using `NonZeroU64::from()` for more efficient and type-safe conversion
|
2024-07-26 06:41:51 -05:00
|
|
|
}
|
|
|
|
}
|