Skip to content

Commit 03bc041

Browse files
committed
refactor(syntax): remove some unsafe code creating IDs (#6324)
1 parent 3a4bcc7 commit 03bc041

File tree

4 files changed

+12
-14
lines changed

4 files changed

+12
-14
lines changed

crates/oxc_syntax/src/node.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@ impl NodeId {
1616
/// # Panics
1717
/// Panics if `idx` is `u32::MAX`.
1818
pub const fn new(idx: u32) -> Self {
19-
// We could use `NonMaxU32::new(idx).unwrap()` but `Option::unwrap` is not a const function
20-
// and we want this function to be
21-
assert!(idx != u32::MAX);
22-
// SAFETY: We have checked that `idx` is not `u32::MAX`
23-
unsafe { Self::new_unchecked(idx) }
19+
if let Some(idx) = NonMaxU32::new(idx) {
20+
return Self(idx);
21+
}
22+
panic!();
2423
}
2524

2625
/// Create `NodeId` from `u32` unchecked.
@@ -38,7 +37,7 @@ impl Idx for NodeId {
3837
#[allow(clippy::cast_possible_truncation)]
3938
fn from_usize(idx: usize) -> Self {
4039
assert!(idx < u32::MAX as usize);
41-
// SAFETY: We just checked `idx` is valid for `NonMaxU32`
40+
// SAFETY: We just checked `idx` is a legal value for `NonMaxU32`
4241
Self(unsafe { NonMaxU32::new_unchecked(idx as u32) })
4342
}
4443

crates/oxc_syntax/src/reference.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ impl Idx for ReferenceId {
1212
#[allow(clippy::cast_possible_truncation)]
1313
fn from_usize(idx: usize) -> Self {
1414
assert!(idx < u32::MAX as usize);
15-
// SAFETY: We just checked `idx` is valid for `NonMaxU32`
15+
// SAFETY: We just checked `idx` is a legal value for `NonMaxU32`
1616
Self(unsafe { NonMaxU32::new_unchecked(idx as u32) })
1717
}
1818

crates/oxc_syntax/src/scope.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ impl ScopeId {
1313
/// # Panics
1414
/// Panics if `idx` is `u32::MAX`.
1515
pub const fn new(idx: u32) -> Self {
16-
// We could use `NonMaxU32::new(idx).unwrap()` but `Option::unwrap` is not a const function
17-
// and we want this function to be
18-
assert!(idx != u32::MAX);
19-
// SAFETY: We have checked that `idx` is not `u32::MAX`
20-
unsafe { Self::new_unchecked(idx) }
16+
if let Some(idx) = NonMaxU32::new(idx) {
17+
return Self(idx);
18+
}
19+
panic!();
2120
}
2221

2322
/// Create `ScopeId` from `u32` unchecked.
@@ -35,7 +34,7 @@ impl Idx for ScopeId {
3534
#[allow(clippy::cast_possible_truncation)]
3635
fn from_usize(idx: usize) -> Self {
3736
assert!(idx < u32::MAX as usize);
38-
// SAFETY: We just checked `idx` is valid for `NonMaxU32`
37+
// SAFETY: We just checked `idx` is a legal value for `NonMaxU32`
3938
Self(unsafe { NonMaxU32::new_unchecked(idx as u32) })
4039
}
4140

crates/oxc_syntax/src/symbol.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ impl Idx for SymbolId {
1111
#[allow(clippy::cast_possible_truncation)]
1212
fn from_usize(idx: usize) -> Self {
1313
assert!(idx < u32::MAX as usize);
14-
// SAFETY: We just checked `idx` is valid for `NonMaxU32`
14+
// SAFETY: We just checked `idx` is a legal value for `NonMaxU32`
1515
Self(unsafe { NonMaxU32::new_unchecked(idx as u32) })
1616
}
1717

0 commit comments

Comments
 (0)