Skip to content

Commit deeda56

Browse files
[configuration] Fix unclear error messages for line-length values exceeding u16::MAX (#21329)
Co-authored-by: Micha Reiser <[email protected]>
1 parent f63a9f2 commit deeda56

File tree

2 files changed

+67
-7
lines changed

2 files changed

+67
-7
lines changed

crates/ruff_linter/src/line_width.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,17 @@ impl<'de> serde::Deserialize<'de> for LineLength {
5151
where
5252
D: serde::Deserializer<'de>,
5353
{
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-
})
54+
let value = i64::deserialize(deserializer)?;
55+
56+
u16::try_from(value)
57+
.ok()
58+
.and_then(|u16_value| Self::try_from(u16_value).ok())
59+
.ok_or_else(|| {
60+
serde::de::Error::custom(format!(
61+
"line-length must be between 1 and {} (got {value})",
62+
Self::MAX,
63+
))
64+
})
6165
}
6266
}
6367

crates/ruff_workspace/src/pyproject.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,62 @@ line-length = 500
468468
"line-length must be between 1 and 320 (got 500)"
469469
);
470470

471+
// Test value at u16::MAX boundary (65535) - should show range error
472+
let invalid_line_length_65535 = toml::from_str::<Pyproject>(
473+
r"
474+
[tool.ruff]
475+
line-length = 65535
476+
",
477+
)
478+
.expect_err("Deserialization should have failed for line-length at u16::MAX");
479+
480+
assert_eq!(
481+
invalid_line_length_65535.message(),
482+
"line-length must be between 1 and 320 (got 65535)"
483+
);
484+
485+
// Test value exceeding u16::MAX (65536) - should show clear error
486+
let invalid_line_length_65536 = toml::from_str::<Pyproject>(
487+
r"
488+
[tool.ruff]
489+
line-length = 65536
490+
",
491+
)
492+
.expect_err("Deserialization should have failed for line-length exceeding u16::MAX");
493+
494+
assert_eq!(
495+
invalid_line_length_65536.message(),
496+
"line-length must be between 1 and 320 (got 65536)"
497+
);
498+
499+
// Test value far exceeding u16::MAX (99_999) - should show clear error
500+
let invalid_line_length_99999 = toml::from_str::<Pyproject>(
501+
r"
502+
[tool.ruff]
503+
line-length = 99_999
504+
",
505+
)
506+
.expect_err("Deserialization should have failed for line-length far exceeding u16::MAX");
507+
508+
assert_eq!(
509+
invalid_line_length_99999.message(),
510+
"line-length must be between 1 and 320 (got 99999)"
511+
);
512+
513+
// Test negative value - should show clear error
514+
let invalid_line_length_negative = toml::from_str::<Pyproject>(
515+
r"
516+
[tool.ruff]
517+
line-length = -5
518+
",
519+
)
520+
.expect_err("Deserialization should have failed for negative line-length");
521+
522+
assert_eq!(
523+
invalid_line_length_negative.message(),
524+
"line-length must be between 1 and 320 (got -5)"
525+
);
526+
471527
Ok(())
472528
}
473529

0 commit comments

Comments
 (0)