mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-16 21:15:18 +03:00
25d319d1b2
changelog: [`useless-nonzero-new_unchecked`]: new lint Close #13991 ### What it does Checks for `NonZero*::new_unchecked(<literal>)` 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 with an integer literal in a `const` context, `NonZero*::new().unwrap()` will provide the same result with identical runtime performances while not requiring `unsafe`. ### Example ```no_run const PLAYERS: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(3) }; ``` Use instead: ```no_run const PLAYERS: NonZeroUsize = NonZeroUsize::new(3).unwrap(); ```