[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:

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:

  1. Deserialize as u64 first: Accept any valid TOML integer value
  2. Validate u16 bounds: Check if the value exceeds u16::MAX before conversion
  3. 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:

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).