Replace two more uses of unsafe with const `Option::unwrap` (#20584)

I guess I missed these in #20007, but I found them today while grepping
for something else. `Option::unwrap` has been const since 1.83, so we
can use it here and avoid some unsafe code.
This commit is contained in:
Brent Westbrook 2025-09-25 15:35:13 -04:00 committed by GitHub
parent f2b7c82534
commit e4ac9e9041
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 2 additions and 10 deletions

View File

@ -45,11 +45,7 @@ pub(super) fn generate_newtype_index(item: ItemStruct) -> syn::Result<proc_macro
// SAFETY:
// * The `value < u32::MAX` guarantees that the add doesn't overflow.
// * The `+ 1` guarantees that the index is not zero
//
// N.B. We have to use the unchecked variant here because we're
// in a const context and Option::unwrap isn't const yet.
#[expect(unsafe_code)]
Self(unsafe { std::num::NonZeroU32::new_unchecked((value as u32) + 1) })
Self(std::num::NonZeroU32::new((value as u32) + 1).unwrap())
}
#vis const fn from_u32(value: u32) -> Self {
@ -58,11 +54,7 @@ pub(super) fn generate_newtype_index(item: ItemStruct) -> syn::Result<proc_macro
// SAFETY:
// * The `value < u32::MAX` guarantees that the add doesn't overflow.
// * The `+ 1` guarantees that the index is larger than zero.
//
// N.B. We have to use the unchecked variant here because we're
// in a const context and Option::unwrap isn't const yet.
#[expect(unsafe_code)]
Self(unsafe { std::num::NonZeroU32::new_unchecked(value + 1) })
Self(std::num::NonZeroU32::new(value + 1).unwrap())
}
/// Returns the index as a `u32` value