@@ -3,6 +3,7 @@ mod transmute_float_to_int;
3
3
mod transmute_int_to_bool;
4
4
mod transmute_int_to_char;
5
5
mod transmute_int_to_float;
6
+ mod transmute_int_to_non_zero;
6
7
mod transmute_null_to_fn;
7
8
mod transmute_num_to_bytes;
8
9
mod transmute_ptr_to_ptr;
@@ -253,6 +254,31 @@ declare_clippy_lint! {
253
254
"transmutes from an integer to a float"
254
255
}
255
256
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
+
256
282
declare_clippy_lint ! {
257
283
/// ### What it does
258
284
/// Checks for transmutes from a float to an integer.
@@ -451,6 +477,7 @@ impl_lint_pass!(Transmute => [
451
477
TRANSMUTE_BYTES_TO_STR ,
452
478
TRANSMUTE_INT_TO_BOOL ,
453
479
TRANSMUTE_INT_TO_FLOAT ,
480
+ TRANSMUTE_INT_TO_NON_ZERO ,
454
481
TRANSMUTE_FLOAT_TO_INT ,
455
482
TRANSMUTE_NUM_TO_BYTES ,
456
483
UNSOUND_COLLECTION_TRANSMUTE ,
@@ -501,6 +528,7 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
501
528
| transmute_ptr_to_ptr:: check( cx, e, from_ty, to_ty, arg)
502
529
| transmute_int_to_bool:: check( cx, e, from_ty, to_ty, arg)
503
530
| 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)
504
532
| transmute_float_to_int:: check( cx, e, from_ty, to_ty, arg, const_context)
505
533
| transmute_num_to_bytes:: check( cx, e, from_ty, to_ty, arg, const_context)
506
534
| (
0 commit comments