@@ -3,6 +3,7 @@ mod transmute_float_to_int;
33mod transmute_int_to_bool;
44mod transmute_int_to_char;
55mod transmute_int_to_float;
6+ mod transmute_int_to_non_zero;
67mod transmute_null_to_fn;
78mod transmute_num_to_bytes;
89mod transmute_ptr_to_ptr;
@@ -253,6 +254,31 @@ declare_clippy_lint! {
253254 "transmutes from an integer to a float"
254255}
255256
257+ declare_clippy_lint ! {
258+ /// ### What it does
259+ /// Checks for transmutes from integers to `NonZero*` types, and suggests their `new_unchecked`
260+ /// method instead.
261+ ///
262+ /// ### Why is this bad?
263+ /// Transmutes work on any types and thus might cause unsoundness when those types change
264+ /// elsewhere. `new_unchecked` only works for the appropriate types instead.
265+ ///
266+ /// ### Example
267+ /// ```rust
268+ /// # use core::num::NonZeroU32;
269+ /// let _non_zero: NonZeroU32 = unsafe { std::mem::transmute(123) };
270+ /// ```
271+ /// Use instead:
272+ /// ```rust
273+ /// # use core::num::NonZeroU32;
274+ /// let _non_zero = unsafe { NonZeroU32::new_unchecked(123) };
275+ /// ```
276+ #[ clippy:: version = "1.69.0" ]
277+ pub TRANSMUTE_INT_TO_NON_ZERO ,
278+ complexity,
279+ "transmutes from an integer to a non-zero wrapper"
280+ }
281+
256282declare_clippy_lint ! {
257283 /// ### What it does
258284 /// Checks for transmutes from a float to an integer.
@@ -451,6 +477,7 @@ impl_lint_pass!(Transmute => [
451477 TRANSMUTE_BYTES_TO_STR ,
452478 TRANSMUTE_INT_TO_BOOL ,
453479 TRANSMUTE_INT_TO_FLOAT ,
480+ TRANSMUTE_INT_TO_NON_ZERO ,
454481 TRANSMUTE_FLOAT_TO_INT ,
455482 TRANSMUTE_NUM_TO_BYTES ,
456483 UNSOUND_COLLECTION_TRANSMUTE ,
@@ -501,6 +528,7 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
501528 | transmute_ptr_to_ptr:: check( cx, e, from_ty, to_ty, arg)
502529 | transmute_int_to_bool:: check( cx, e, from_ty, to_ty, arg)
503530 | transmute_int_to_float:: check( cx, e, from_ty, to_ty, arg, const_context)
531+ | transmute_int_to_non_zero:: check( cx, e, from_ty, to_ty, arg)
504532 | transmute_float_to_int:: check( cx, e, from_ty, to_ty, arg, const_context)
505533 | transmute_num_to_bytes:: check( cx, e, from_ty, to_ty, arg, const_context)
506534 | (
0 commit comments