Replace two more uses of unsafe with const Option::unwrap (#20584)
Some checks are pending
CI / cargo test (linux) (push) Blocked by required conditions
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks instrumented (ruff) (push) Blocked by required conditions
CI / benchmarks instrumented (ty) (push) Blocked by required conditions
CI / benchmarks-walltime (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run

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

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