Repository: astral-sh/ruff
File: crates/ruff_linter/src/line_width.rs (Rust)

Code before:
}

impl fmt::Display for LineLength {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.0, f)
    }
}

impl<'de> serde::Deserialize<'de> for LineLength {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let value = u64::deserialize(deserializer)?;

        // Check if value exceeds u16::MAX
        if value > u64::from(u16::MAX) {
            return Err(serde::de::Error::custom(format!(
                "line-length must be between 1 and {} (got {value})",
                Self::MAX,
            )));
        }

        // Convert to u16 and validate range
        // Safe to unwrap since we've already checked value <= u16::MAX
        let value = u16::try_from(value).expect("value should fit in u16");
        Self::try_from(value).map_err(|_| {
            serde::de::Error::custom(format!(
                "line-length must be between 1 and {} (got {value})",
                Self::MAX,
            ))
        })
    }
}

impl CacheKey for LineLength {
    fn cache_key(&self, state: &mut CacheKeyHasher) {
        state.write_u16(self.0.get());
    }
}

/// Error type returned when parsing a [`LineLength`] from a string fails
pub enum ParseLineWidthError {
    /// The string could not be parsed as a valid [u16]
    ParseError(ParseIntError),
    /// The [u16] value of the string is not a valid [`LineLength`]
    TryFromIntError(LineLengthFromIntError),
}

impl std::fmt::Debug for ParseLineWidthError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {

Review comment (suggestion):
```suggestion
        // Convert to u16 and validate range
        // Safe to unwrap since we've already checked value <= u16::MAX
        let value = u16::try_from(value).map_err(|_| serde::de::Error::custom(format!(
            "line-length must be between 1 and {} (got {value})",
            Self::MAX,
        )))?;
```

Code after:
}

impl fmt::Display for LineLength {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.0, f)
    }
}

impl<'de> serde::Deserialize<'de> for LineLength {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let value = i64::deserialize(deserializer)?;

        u16::try_from(value)
            .ok()
            .and_then(|u16_value| Self::try_from(u16_value).ok())
            .ok_or_else(|| {
                serde::de::Error::custom(format!(
                    "line-length must be between 1 and {} (got {value})",
                    Self::MAX,
                ))
            })
    }
}

impl CacheKey for LineLength {
    fn cache_key(&self, state: &mut CacheKeyHasher) {
        state.write_u16(self.0.get());
    }
}

/// Error type returned when parsing a [`LineLength`] from a string fails
pub enum ParseLineWidthError {
    /// The string could not be parsed as a valid [u16]
    ParseError(ParseIntError),
    /// The [u16] value of the string is not a valid [`LineLength`]
    TryFromIntError(LineLengthFromIntError),
}

impl std::fmt::Debug for ParseLineWidthError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        std::fmt::Display::fmt(self, f)
    }
}

impl std::fmt::Display for ParseLineWidthError {
    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ParseLineWidthError::ParseError(err) => std::fmt::Display::fmt(err, fmt),