RS.CLIPPY.USELESS_NONZERO_NEW_UNCHECKED

Using `NonZero::new_unchecked()` in a `const` context

This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: useless_nonzero_new_unchecked. Copyright ©2025 The Rust Team. All rights reserved.

What it does

Checks for NonZero*::new_unchecked() being used in a const context.

Why is this bad?

Using NonZero*::new_unchecked() is an unsafe function and requires an unsafe context. When used in a context evaluated at compilation time, NonZero*::new().unwrap() will provide the same result with identical runtime performances while not requiring unsafe.

Example

use std::num::NonZeroUsize;
const PLAYERS: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(3) };

Use instead:

use std::num::NonZeroUsize;
const PLAYERS: NonZeroUsize = NonZeroUsize::new(3).unwrap();