mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-02 22:54:42 +00:00
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
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:
parent
f2b7c82534
commit
e4ac9e9041
1 changed files with 2 additions and 10 deletions
|
@ -45,11 +45,7 @@ pub(super) fn generate_newtype_index(item: ItemStruct) -> syn::Result<proc_macro
|
||||||
// SAFETY:
|
// SAFETY:
|
||||||
// * The `value < u32::MAX` guarantees that the add doesn't overflow.
|
// * The `value < u32::MAX` guarantees that the add doesn't overflow.
|
||||||
// * The `+ 1` guarantees that the index is not zero
|
// * The `+ 1` guarantees that the index is not zero
|
||||||
//
|
Self(std::num::NonZeroU32::new((value as u32) + 1).unwrap())
|
||||||
// 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) })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#vis const fn from_u32(value: u32) -> Self {
|
#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:
|
// SAFETY:
|
||||||
// * The `value < u32::MAX` guarantees that the add doesn't overflow.
|
// * The `value < u32::MAX` guarantees that the add doesn't overflow.
|
||||||
// * The `+ 1` guarantees that the index is larger than zero.
|
// * The `+ 1` guarantees that the index is larger than zero.
|
||||||
//
|
Self(std::num::NonZeroU32::new(value + 1).unwrap())
|
||||||
// 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) })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the index as a `u32` value
|
/// Returns the index as a `u32` value
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue