Skip to content

Commit fa12fd0

Browse files
Clearer error message when line-length goes beyond threshold (#21072)
Co-authored-by: Micha Reiser <micha@reiser.io>
1 parent fdb8ea4 commit fa12fd0

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

crates/ruff_linter/src/line_width.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use ruff_text_size::TextSize;
1414
/// The length of a line of text that is considered too long.
1515
///
1616
/// The allowed range of values is 1..=320
17-
#[derive(Clone, Copy, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
17+
#[derive(Clone, Copy, Debug, Eq, PartialEq, serde::Serialize)]
1818
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
1919
pub struct LineLength(
2020
#[cfg_attr(feature = "schemars", schemars(range(min = 1, max = 320)))] NonZeroU16,
@@ -46,6 +46,21 @@ impl fmt::Display for LineLength {
4646
}
4747
}
4848

49+
impl<'de> serde::Deserialize<'de> for LineLength {
50+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
51+
where
52+
D: serde::Deserializer<'de>,
53+
{
54+
let value = u16::deserialize(deserializer)?;
55+
Self::try_from(value).map_err(|_| {
56+
serde::de::Error::custom(format!(
57+
"line-length must be between 1 and {} (got {value})",
58+
Self::MAX,
59+
))
60+
})
61+
}
62+
}
63+
4964
impl CacheKey for LineLength {
5065
fn cache_key(&self, state: &mut CacheKeyHasher) {
5166
state.write_u16(self.0.get());

crates/ruff_workspace/src/pyproject.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,6 @@ mod tests {
266266
use crate::pyproject::{Pyproject, Tools, find_settings_toml, parse_pyproject_toml};
267267

268268
#[test]
269-
270269
fn deserialize() -> Result<()> {
271270
let pyproject: Pyproject = toml::from_str(r"")?;
272271
assert_eq!(pyproject.tool, None);
@@ -456,6 +455,19 @@ other-attribute = 1
456455
.is_err()
457456
);
458457

458+
let invalid_line_length = toml::from_str::<Pyproject>(
459+
r"
460+
[tool.ruff]
461+
line-length = 500
462+
",
463+
)
464+
.expect_err("Deserialization should have failed for a too large line-length");
465+
466+
assert_eq!(
467+
invalid_line_length.message(),
468+
"line-length must be between 1 and 320 (got 500)"
469+
);
470+
459471
Ok(())
460472
}
461473

0 commit comments

Comments
 (0)