Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Remove unsafe from rome_diagnostics::display::frame #4235

Merged
merged 1 commit into from
Feb 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 15 additions & 18 deletions crates/rome_diagnostics/src/display/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,17 @@ use crate::{
LineIndexBuf, Location,
};

// SAFETY: These constants `NonZeroUsize` are being initialized with non-zero values
const ONE: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(1) };
pub(super) const CODE_FRAME_CONTEXT_LINES: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(2) };
/// A const Option::unwrap without nightly features:
/// https://github.com/rust-lang/rust/issues/67441
const fn unwrap<T: Copy>(option: Option<T>) -> T {
match option {
Some(value) => value,
None => panic!("unwrapping None"),
}
}

const ONE: NonZeroUsize = unwrap(NonZeroUsize::new(1));
pub(super) const CODE_FRAME_CONTEXT_LINES: NonZeroUsize = unwrap(NonZeroUsize::new(2));

const MAX_CODE_FRAME_LINES: usize = 8;
const HALF_MAX_CODE_FRAME_LINES: usize = MAX_CODE_FRAME_LINES / 2;
Expand Down Expand Up @@ -249,7 +257,7 @@ pub(super) fn print_frame(fmt: &mut fmt::Formatter<'_>, location: Location<'_>)
/// Calculate the length of the string representation of `value`
pub(super) fn calculate_print_width(mut value: OneIndexed) -> NonZeroUsize {
// SAFETY: Constant is being initialized with a non-zero value
const TEN: OneIndexed = unsafe { OneIndexed::new_unchecked(10) };
const TEN: OneIndexed = unwrap(OneIndexed::new(10));

let mut width = ONE;

Expand Down Expand Up @@ -515,9 +523,9 @@ pub struct OneIndexed(NonZeroUsize);
impl OneIndexed {
// SAFETY: These constants are being initialized with non-zero values
/// The smallest value that can be represented by this integer type.
pub const MIN: Self = unsafe { Self::new_unchecked(1) };
pub const MIN: Self = unwrap(Self::new(1));
/// The largest value that can be represented by this integer type
pub const MAX: Self = unsafe { Self::new_unchecked(usize::MAX) };
pub const MAX: Self = unwrap(Self::new(usize::MAX));

/// Creates a non-zero if the given value is not zero.
pub const fn new(value: usize) -> Option<Self> {
Expand All @@ -527,20 +535,9 @@ impl OneIndexed {
}
}

/// Creates a non-zero without checking whether the value is non-zero.
/// This results in undefined behaviour if the value is zero.
///
/// # Safety
///
/// The value must not be zero.
pub const unsafe fn new_unchecked(value: usize) -> Self {
Self(NonZeroUsize::new_unchecked(value))
}

/// Construct a new [OneIndexed] from a zero-indexed value
pub const fn from_zero_indexed(value: usize) -> Self {
// SAFETY: Adding `1` to `value` ensures it's non-zero
Self(unsafe { NonZeroUsize::new_unchecked(value.saturating_add(1)) })
Self(ONE.saturating_add(value))
}

/// Returns the value as a primitive type.
Expand Down