diff --git a/clippy_lints/src/transmute/eager_transmute.rs b/clippy_lints/src/transmute/eager_transmute.rs index c44f5150dd1..1dfc9f7091e 100644 --- a/clippy_lints/src/transmute/eager_transmute.rs +++ b/clippy_lints/src/transmute/eager_transmute.rs @@ -87,7 +87,7 @@ pub(super) fn check<'tcx>( && is_normalizable(cx, cx.param_env, from_ty) && is_normalizable(cx, cx.param_env, to_ty) // we only want to lint if the target type has a niche that is larger than the one of the source type - // e.g. `u8` to `NonZeroU8` should lint, but `NonZeroU8` to `u8` should not + // e.g. `u8` to `NonZero` should lint, but `NonZero` to `u8` should not && let Ok(from_layout) = cx.tcx.layout_of(cx.param_env.and(from_ty)) && let Ok(to_layout) = cx.tcx.layout_of(cx.param_env.and(to_ty)) && match (from_layout.largest_niche, to_layout.largest_niche) { diff --git a/lintcheck/src/config.rs b/lintcheck/src/config.rs index e678d40795e..3f712f453fa 100644 --- a/lintcheck/src/config.rs +++ b/lintcheck/src/config.rs @@ -1,5 +1,5 @@ use clap::Parser; -use std::num::NonZeroUsize; +use std::num::NonZero; use std::path::PathBuf; #[derive(Clone, Debug, Parser)] @@ -61,7 +61,7 @@ impl LintcheckConfig { config.max_jobs = if config.fix || config.recursive { 1 } else { - std::thread::available_parallelism().map_or(1, NonZeroUsize::get) + std::thread::available_parallelism().map_or(1, NonZero::get) }; }; diff --git a/tests/ui/arithmetic_side_effects.rs b/tests/ui/arithmetic_side_effects.rs index 66d71f337f2..33a91e8bbbe 100644 --- a/tests/ui/arithmetic_side_effects.rs +++ b/tests/ui/arithmetic_side_effects.rs @@ -15,7 +15,7 @@ extern crate proc_macro_derive; -use core::num::{NonZeroUsize, Saturating, Wrapping}; +use core::num::{NonZero, Saturating, Wrapping}; const ONE: i32 = 1; const ZERO: i32 = 0; @@ -494,15 +494,15 @@ pub fn issue_11262() { } pub fn issue_11392() { - fn example_div(unsigned: usize, nonzero_unsigned: NonZeroUsize) -> usize { + fn example_div(unsigned: usize, nonzero_unsigned: NonZero) -> usize { unsigned / nonzero_unsigned } - fn example_rem(unsigned: usize, nonzero_unsigned: NonZeroUsize) -> usize { + fn example_rem(unsigned: usize, nonzero_unsigned: NonZero) -> usize { unsigned % nonzero_unsigned } - let (unsigned, nonzero_unsigned) = (0, NonZeroUsize::new(1).unwrap()); + let (unsigned, nonzero_unsigned) = (0, NonZero::new(1).unwrap()); example_div(unsigned, nonzero_unsigned); example_rem(unsigned, nonzero_unsigned); } diff --git a/tests/ui/eager_transmute.fixed b/tests/ui/eager_transmute.fixed index c29e7dd9ab3..ba4342462dc 100644 --- a/tests/ui/eager_transmute.fixed +++ b/tests/ui/eager_transmute.fixed @@ -2,7 +2,7 @@ #![warn(clippy::eager_transmute)] #![allow(clippy::transmute_int_to_non_zero, clippy::missing_transmute_annotations)] -use std::num::NonZeroU8; +use std::num::NonZero; #[repr(u8)] enum Opcode { @@ -85,21 +85,21 @@ macro_rules! impls { } impls!(NonMaxU8, NonZeroNonMaxU8); -fn niche_tests(v1: u8, v2: NonZeroU8, v3: NonZeroNonMaxU8) { - // u8 -> NonZeroU8, do lint - let _: Option = (v1 > 0).then(|| unsafe { std::mem::transmute(v1) }); +fn niche_tests(v1: u8, v2: NonZero, v3: NonZeroNonMaxU8) { + // u8 -> NonZero, do lint + let _: Option> = (v1 > 0).then(|| unsafe { std::mem::transmute(v1) }); - // NonZeroU8 -> u8, don't lint, target type has no niche and therefore a higher validity range - let _: Option = (v2 > NonZeroU8::new(1).unwrap()).then_some(unsafe { std::mem::transmute(v2) }); + // NonZero -> u8, don't lint, target type has no niche and therefore a higher validity range + let _: Option = (v2 > NonZero::new(1u8).unwrap()).then_some(unsafe { std::mem::transmute(v2) }); - // NonZeroU8 -> NonMaxU8, do lint, different niche - let _: Option = (v2 < NonZeroU8::new(255).unwrap()).then(|| unsafe { std::mem::transmute(v2) }); + // NonZero -> NonMaxU8, do lint, different niche + let _: Option = (v2 < NonZero::new(255u8).unwrap()).then(|| unsafe { std::mem::transmute(v2) }); // NonZeroNonMaxU8 -> NonMaxU8, don't lint, target type has more validity let _: Option = (v3 < 255).then_some(unsafe { std::mem::transmute(v2) }); - // NonZeroU8 -> NonZeroNonMaxU8, do lint, target type has less validity - let _: Option = (v2 < NonZeroU8::new(255).unwrap()).then(|| unsafe { std::mem::transmute(v2) }); + // NonZero -> NonZeroNonMaxU8, do lint, target type has less validity + let _: Option = (v2 < NonZero::new(255u8).unwrap()).then(|| unsafe { std::mem::transmute(v2) }); } fn main() {} diff --git a/tests/ui/eager_transmute.rs b/tests/ui/eager_transmute.rs index 491a9485c93..9750e87ce57 100644 --- a/tests/ui/eager_transmute.rs +++ b/tests/ui/eager_transmute.rs @@ -2,7 +2,7 @@ #![warn(clippy::eager_transmute)] #![allow(clippy::transmute_int_to_non_zero, clippy::missing_transmute_annotations)] -use std::num::NonZeroU8; +use std::num::NonZero; #[repr(u8)] enum Opcode { @@ -85,21 +85,21 @@ macro_rules! impls { } impls!(NonMaxU8, NonZeroNonMaxU8); -fn niche_tests(v1: u8, v2: NonZeroU8, v3: NonZeroNonMaxU8) { - // u8 -> NonZeroU8, do lint - let _: Option = (v1 > 0).then_some(unsafe { std::mem::transmute(v1) }); +fn niche_tests(v1: u8, v2: NonZero, v3: NonZeroNonMaxU8) { + // u8 -> NonZero, do lint + let _: Option> = (v1 > 0).then_some(unsafe { std::mem::transmute(v1) }); - // NonZeroU8 -> u8, don't lint, target type has no niche and therefore a higher validity range - let _: Option = (v2 > NonZeroU8::new(1).unwrap()).then_some(unsafe { std::mem::transmute(v2) }); + // NonZero -> u8, don't lint, target type has no niche and therefore a higher validity range + let _: Option = (v2 > NonZero::new(1u8).unwrap()).then_some(unsafe { std::mem::transmute(v2) }); - // NonZeroU8 -> NonMaxU8, do lint, different niche - let _: Option = (v2 < NonZeroU8::new(255).unwrap()).then_some(unsafe { std::mem::transmute(v2) }); + // NonZero -> NonMaxU8, do lint, different niche + let _: Option = (v2 < NonZero::new(255u8).unwrap()).then_some(unsafe { std::mem::transmute(v2) }); // NonZeroNonMaxU8 -> NonMaxU8, don't lint, target type has more validity let _: Option = (v3 < 255).then_some(unsafe { std::mem::transmute(v2) }); - // NonZeroU8 -> NonZeroNonMaxU8, do lint, target type has less validity - let _: Option = (v2 < NonZeroU8::new(255).unwrap()).then_some(unsafe { std::mem::transmute(v2) }); + // NonZero -> NonZeroNonMaxU8, do lint, target type has less validity + let _: Option = (v2 < NonZero::new(255u8).unwrap()).then_some(unsafe { std::mem::transmute(v2) }); } fn main() {} diff --git a/tests/ui/eager_transmute.stderr b/tests/ui/eager_transmute.stderr index b9a4321d99e..5cf7bd49a92 100644 --- a/tests/ui/eager_transmute.stderr +++ b/tests/ui/eager_transmute.stderr @@ -155,36 +155,36 @@ LL | (op < 4).then(|| std::mem::transmute::<_, Opcode>(op)); | ~~~~ ++ error: this transmute is always evaluated eagerly, even if the condition is false - --> tests/ui/eager_transmute.rs:90:60 + --> tests/ui/eager_transmute.rs:90:62 | -LL | let _: Option = (v1 > 0).then_some(unsafe { std::mem::transmute(v1) }); - | ^^^^^^^^^^^^^^^^^^^^^^^ +LL | let _: Option> = (v1 > 0).then_some(unsafe { std::mem::transmute(v1) }); + | ^^^^^^^^^^^^^^^^^^^^^^^ | help: consider using `bool::then` to only transmute if the condition holds | -LL | let _: Option = (v1 > 0).then(|| unsafe { std::mem::transmute(v1) }); - | ~~~~ ++ +LL | let _: Option> = (v1 > 0).then(|| unsafe { std::mem::transmute(v1) }); + | ~~~~ ++ error: this transmute is always evaluated eagerly, even if the condition is false --> tests/ui/eager_transmute.rs:96:86 | -LL | let _: Option = (v2 < NonZeroU8::new(255).unwrap()).then_some(unsafe { std::mem::transmute(v2) }); +LL | let _: Option = (v2 < NonZero::new(255u8).unwrap()).then_some(unsafe { std::mem::transmute(v2) }); | ^^^^^^^^^^^^^^^^^^^^^^^ | help: consider using `bool::then` to only transmute if the condition holds | -LL | let _: Option = (v2 < NonZeroU8::new(255).unwrap()).then(|| unsafe { std::mem::transmute(v2) }); +LL | let _: Option = (v2 < NonZero::new(255u8).unwrap()).then(|| unsafe { std::mem::transmute(v2) }); | ~~~~ ++ error: this transmute is always evaluated eagerly, even if the condition is false --> tests/ui/eager_transmute.rs:102:93 | -LL | let _: Option = (v2 < NonZeroU8::new(255).unwrap()).then_some(unsafe { std::mem::transmute(v2) }); +LL | let _: Option = (v2 < NonZero::new(255u8).unwrap()).then_some(unsafe { std::mem::transmute(v2) }); | ^^^^^^^^^^^^^^^^^^^^^^^ | help: consider using `bool::then` to only transmute if the condition holds | -LL | let _: Option = (v2 < NonZeroU8::new(255).unwrap()).then(|| unsafe { std::mem::transmute(v2) }); +LL | let _: Option = (v2 < NonZero::new(255u8).unwrap()).then(|| unsafe { std::mem::transmute(v2) }); | ~~~~ ++ error: aborting due to 17 previous errors