Skip to content
Merged
Show file tree
Hide file tree
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
18 changes: 11 additions & 7 deletions crates/ruff_linter/src/line_width.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,17 @@ impl<'de> serde::Deserialize<'de> for LineLength {
where
D: serde::Deserializer<'de>,
{
let value = u16::deserialize(deserializer)?;
Self::try_from(value).map_err(|_| {
serde::de::Error::custom(format!(
"line-length must be between 1 and {} (got {value})",
Self::MAX,
))
})
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,
))
})
}
}

Expand Down
56 changes: 56 additions & 0 deletions crates/ruff_workspace/src/pyproject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,62 @@ line-length = 500
"line-length must be between 1 and 320 (got 500)"
);

// Test value at u16::MAX boundary (65535) - should show range error
let invalid_line_length_65535 = toml::from_str::<Pyproject>(
r"
[tool.ruff]
line-length = 65535
",
)
.expect_err("Deserialization should have failed for line-length at u16::MAX");

assert_eq!(
invalid_line_length_65535.message(),
"line-length must be between 1 and 320 (got 65535)"
);

// Test value exceeding u16::MAX (65536) - should show clear error
let invalid_line_length_65536 = toml::from_str::<Pyproject>(
r"
[tool.ruff]
line-length = 65536
",
)
.expect_err("Deserialization should have failed for line-length exceeding u16::MAX");

assert_eq!(
invalid_line_length_65536.message(),
"line-length must be between 1 and 320 (got 65536)"
);

// Test value far exceeding u16::MAX (99_999) - should show clear error
let invalid_line_length_99999 = toml::from_str::<Pyproject>(
r"
[tool.ruff]
line-length = 99_999
",
)
.expect_err("Deserialization should have failed for line-length far exceeding u16::MAX");

assert_eq!(
invalid_line_length_99999.message(),
"line-length must be between 1 and 320 (got 99999)"
);

// Test negative value - should show clear error
let invalid_line_length_negative = toml::from_str::<Pyproject>(
r"
[tool.ruff]
line-length = -5
",
)
.expect_err("Deserialization should have failed for negative line-length");

assert_eq!(
invalid_line_length_negative.message(),
"line-length must be between 1 and 320 (got -5)"
);

Ok(())
}

Expand Down