rust-lang / rust-lang/rust-clippy

transmute_int_to_non_zero does not work in const context

Open
#16,694 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

I-suggestion-causes-error
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

--force-warn clippy::transmute_int_to_non_zero

this code:

// check-pass

// Some constants that *are* valid

use std::mem;
use std::ptr::NonNull;
use std::num::{NonZeroU8, NonZeroUsize};

const NON_NULL_PTR1: NonNull<u8> = unsafe { mem::transmute(1usize) };
const NON_NULL_PTR2: NonNull<u8> = unsafe { mem::transmute(&0) };

const NON_NULL_U8: NonZeroU8 = unsafe { mem::transmute(1u8) };
const NON_NULL_USIZE: NonZeroUsize = unsafe { mem::transmute(1usize) };

const UNIT: () = ();

fn main() {}

caused the following diagnostics:

    Checking _177158189332677763 v0.1.0 (/tmp/icemaker_global_tempdir.gPgWiqot4T9Z/icemaker_clippyfix_tempdir.XzfiozsNr9XH/_177158189332677763)
warning: transmute from a `u8` to a `NonZero<u8>`
  --> src/main.rs:12:41
   |
12 | const NON_NULL_U8: NonZeroU8 = unsafe { mem::transmute(1u8) };
   |                                         ^^^^^^^^^^^^^^^^^^^ help: consider using: `NonZero::new_unchecked(1u8)`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#transmute_int_to_non_zero
   = note: requested on the command line with `--force-warn clippy::transmute-int-to-non-zero`

warning: transmute from a `usize` to a `NonZero<usize>`
  --> src/main.rs:13:47
   |
13 | const NON_NULL_USIZE: NonZeroUsize = unsafe { mem::transmute(1usize) };
   |                                               ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `NonZero::new_unchecked(1usize)`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#transmute_int_to_non_zero

warning: `_177158189332677763` (bin "_177158189332677763") generated 2 warnings
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.15s

However after applying these diagnostics, the resulting code:

// check-pass

// Some constants that *are* valid

use std::num::NonZero;
use std::mem;
use std::ptr::NonNull;
use std::num::{NonZeroU8, NonZeroUsize};

const NON_NULL_PTR1: NonNull<u8> = unsafe { mem::transmute(1usize) };
const NON_NULL_PTR2: NonNull<u8> = unsafe { mem::transmute(&0) };

const NON_NULL_U8: NonZeroU8 = unsafe { NonZero::new_unchecked(1u8) };
const NON_NULL_USIZE: NonZeroUsize = unsafe { NonZeroU8::new_unchecked(1usize) };

const UNIT: () = ();

fn main() {}

no longer compiled:

    Checking _177158189332677763 v0.1.0 (/tmp/icemaker_global_tempdir.gPgWiqot4T9Z/icemaker_clippyfix_tempdir.XzfiozsNr9XH/_177158189332677763)
warning: failed to automatically apply fixes suggested by rustc to crate `_177158189332677763`

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0658]: cannot call conditionally-const method `<std::num::NonZero<u8> as std::convert::Into<std::num::NonZero<usize>>>::into` in constants
  --> src/main.rs:14:77
   |
14 | const NON_NULL_USIZE: NonZeroUsize = unsafe { NonZeroU8::new_unchecked(1u8).into() };
   |                                                                             ^^^^^^
   |
   = note: calls in constants are limited to constant functions, tuple structs and tuple variants
   = note: see issue #143874 <https://github.com/rust-lang/rust/issues/143874> for more information
   = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
   = note: this compiler was built on 2026-03-07; consider upgrading it if it is out of date

error: `std::convert::Into` is not yet stable as a const trait
  --> src/main.rs:14:47
   |
14 | const NON_NULL_USIZE: NonZeroUsize = unsafe { NonZeroU8::new_unchecked(1u8).into() };
   |                                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
help: add `#![feature(const_convert)]` to the crate attributes to enable
   |
 5 + #![feature(const_convert)]
   |

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0658`.
Original diagnostics will follow.

error[E0308]: mismatched types
   --> src/main.rs:14:72
    |
 14 | const NON_NULL_USIZE: NonZeroUsize = unsafe { NonZeroU8::new_unchecked(1usize) };
    |                                               ------------------------ ^^^^^^ expected `u8`, found `usize`
    |                                               |
    |                                               arguments to this function are incorrect
    |
note: associated function defined here
   --> /home/gh-matthiaskrgr/.rustup/toolchains/master/lib/rustlib/src/rust/library/core/src/num/nonzero.rs:418:25
    |
418 |     pub const unsafe fn new_unchecked(n: T) -> Self {
    |                         ^^^^^^^^^^^^^
help: change the type of the numeric literal from `usize` to `u8`
    |
 14 - const NON_NULL_USIZE: NonZeroUsize = unsafe { NonZeroU8::new_unchecked(1usize) };
 14 + const NON_NULL_USIZE: NonZeroUsize = unsafe { NonZeroU8::new_unchecked(1u8) };
    |

error[E0308]: mismatched types
  --> src/main.rs:14:47
   |
14 | const NON_NULL_USIZE: NonZeroUsize = unsafe { NonZeroU8::new_unchecked(1usize) };
   |                                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `NonZero<usize>`, found `NonZero<u8>`
   |
   = note: expected struct `std::num::NonZero<usize>`
              found struct `std::num::NonZero<u8>`
help: call `Into::into` on this expression to convert `std::num::NonZero<u8>` into `std::num::NonZero<usize>`
   |
14 | const NON_NULL_USIZE: NonZeroUsize = unsafe { NonZeroU8::new_unchecked(1usize).into() };
   |                                                                               +++++++

For more information about this error, try `rustc --explain E0308`.
error: could not compile `_177158189332677763` (bin "_177158189332677763" test) due to 2 previous errors
warning: build failed, waiting for other jobs to finish...
warning: failed to automatically apply fixes suggested by rustc to crate `_177158189332677763`

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0658]: cannot call conditionally-const method `<std::num::NonZero<u8> as std::convert::Into<std::num::NonZero<usize>>>::into` in constants
  --> src/main.rs:15:77
   |
15 | const NON_NULL_USIZE: NonZeroUsize = unsafe { NonZeroU8::new_unchecked(1u8).into() };
   |                                                                             ^^^^^^
   |
   = note: calls in constants are limited to constant functions, tuple structs and tuple variants
   = note: see issue #143874 <https://github.com/rust-lang/rust/issues/143874> for more information
   = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
   = note: this compiler was built on 2026-03-07; consider upgrading it if it is out of date

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0658`.
Original diagnostics will follow.

error[E0658]: cannot call conditionally-const method `<std::num::NonZero<u8> as std::convert::Into<std::num::NonZero<usize>>>::into` in constants
  --> src/main.rs:14:77
   |
14 | const NON_NULL_USIZE: NonZeroUsize = unsafe { NonZeroU8::new_unchecked(1u8).into() };
   |                                                                             ^^^^^^
   |
   = note: calls in constants are limited to constant functions, tuple structs and tuple variants
   = note: see issue #143874 <https://github.com/rust-lang/rust/issues/143874> for more information
   = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
   = note: this compiler was built on 2026-03-07; consider upgrading it if it is out of date

error: `std::convert::Into` is not yet stable as a const trait
  --> src/main.rs:14:47
   |
14 | const NON_NULL_USIZE: NonZeroUsize = unsafe { NonZeroU8::new_unchecked(1u8).into() };
   |                                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
help: add `#![feature(const_convert)]` to the crate attributes to enable
   |
 5 + #![feature(const_convert)]
   |

For more information about this error, try `rustc --explain E0658`.
error: could not compile `_177158189332677763` (bin "_177158189332677763") due to 2 previous errors

Version:

rustc 1.96.0-nightly (e3d66fe39 2026-03-07)
binary: rustc
commit-hash: e3d66fe39ae70380fa2365c008e2927479114844
commit-date: 2026-03-07
host: x86_64-unknown-linux-gnu
release: 1.96.0-nightly
LLVM version: 22.1.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided Rust const-context example and the transmute_int_to_non_zero lint entry point. Reproduce the diagnostic and suggested replacement, then verify that the resulting code preserves the original target types and compiles in a const context. Done when the suggestion is valid for both NonZeroU8 and NonZeroUsize cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.