[configuration] Fix unclear error messages for line-length values exceeding u16::MAX by danparizher · Pull Request #21329 · astral-sh/ruff (original) (raw)
Summary
Fixed unclear error messages when line-length configuration values exceed u16::MAX (65535). Previously, values > 65535 would produce a cryptic TOML parsing error "expected u16" instead of a clear validation message. Now all invalid values produce consistent, user-friendly error messages.
Fixes #21328
Problem Analysis
The LineLength type's Deserialize implementation attempted to deserialize directly as u16. When TOML contained values exceeding u16::MAX (65535), the TOML deserializer would fail before reaching the validation logic, resulting in unclear error messages:
- Values > 65535:
"invalid value: integer65536, expected u16"(unclear) - Values 321-65535:
"line-length must be between 1 and 320 (got 500)"(clear)
This inconsistency violated the TOML spec's requirement to accept arbitrary 64-bit signed integers and handle validation errors gracefully.
Approach
Modified the Deserialize implementation for LineLength in crates/ruff_linter/src/line_width.rs to:
- Deserialize as
u64first: Accept any valid TOML integer value - Validate u16 bounds: Check if the value exceeds
u16::MAXbefore conversion - Provide consistent errors: All out-of-range values now produce the same clear error message format:
"line-length must be between 1 and 320 (got {value})"
Added test cases covering:
- Boundary case:
line-length = 65535(at u16::MAX) - Exceeds u16::MAX:
line-length = 65536 - Far exceeds u16::MAX:
line-length = 99_999
All tests verify that the error messages are clear and consistent regardless of whether the value exceeds u16::MAX or just exceeds the valid range (1-320).