ruff/crates
Brent Westbrook c433865801
Avoid underflow in default ranges before a BOM (#19839)
Summary
--

This fixes a regression caused by the BOM handling in #19806. Most
diagnostics already account for the BOM in their ranges, but those that
use `TextRange::default` to mean the beginning of the file do not,
causing an underflow in `RenderableAnnotation::new` when subtracting the
BOM-shifted `snippet_start` from the annotation range.

I ran into this when trying to run benchmarks on CPython in preparation
for caching work. The file `cpython/Lib/test/bad_coding2.py` was causing
a crash because it had a default-range `I002` diagnostic, with a BOM.


7cc3f1ebe9/crates/ruff_linter/src/rules/isort/rules/add_required_imports.rs (L122-L126)

The fix here is just to saturate to zero instead of panicking. I
considered adding a `TextRange::saturating_sub` method, but I wasn't
sure it was worth it for this one use. I'm happy to do that if
preferred, though.

Saturating seemed easier than shifting the affected annotations over,
but that could be another solution.

Test Plan
--

A new `ruff_db` test that reproduced the issue and manual testing
against the CPython file mentioned above
2025-08-11 08:52:27 -04:00
..
ruff Move full diagnostic rendering to ruff_db (#19415) 2025-08-08 12:56:23 -04:00
ruff_annotate_snippets Move full diagnostic rendering to ruff_db (#19415) 2025-08-08 12:56:23 -04:00
ruff_benchmark [ty] Remove special casing for tuple addition (#19636) 2025-07-30 16:25:42 +00:00
ruff_cache
ruff_db Avoid underflow in default ranges before a BOM (#19839) 2025-08-11 08:52:27 -04:00
ruff_dev Update Rust toolchain to 1.89 (#19807) 2025-08-07 18:21:50 +02:00
ruff_diagnostics [ty] Implement diagnostic caching (#19605) 2025-07-30 11:04:34 +01:00
ruff_formatter Update Rust toolchain to 1.89 (#19807) 2025-08-07 18:21:50 +02:00
ruff_graph [ty] Remove KnownModule::is_enum (#19681) 2025-08-01 10:31:12 +02:00
ruff_index Update Rust toolchain to 1.88 and MSRV to 1.86 (#19011) 2025-06-28 20:24:00 +02:00
ruff_linter SIM905: Fix handling of U+001C..U+001F whitespace (#19849) 2025-08-11 03:43:04 +00:00
ruff_macros [ty] Support LSP client settings (#19614) 2025-08-06 18:37:21 +05:30
ruff_memory_usage [ty] Avoid overcounting shared memory usage (#19773) 2025-08-06 15:32:02 -04:00
ruff_notebook
ruff_options_metadata Update Rust toolchain to 1.89 (#19807) 2025-08-07 18:21:50 +02:00
ruff_python_ast Update Rust toolchain to 1.89 (#19807) 2025-08-07 18:21:50 +02:00
ruff_python_ast_integration_tests Disallow implicit concatenation of t-strings and other string types (#19485) 2025-07-27 12:41:03 +00:00
ruff_python_codegen Update Rust toolchain to 1.89 (#19807) 2025-08-07 18:21:50 +02:00
ruff_python_formatter Update Rust toolchain to 1.89 (#19807) 2025-08-07 18:21:50 +02:00
ruff_python_index
ruff_python_literal
ruff_python_parser Update Rust toolchain to 1.89 (#19807) 2025-08-07 18:21:50 +02:00
ruff_python_semantic Update Rust toolchain to 1.89 (#19807) 2025-08-07 18:21:50 +02:00
ruff_python_stdlib
ruff_python_trivia Treat ty: comments as pragma comments (#18532) 2025-06-07 16:02:43 +02:00
ruff_python_trivia_integration_tests
ruff_server Update Rust toolchain to 1.89 (#19807) 2025-08-07 18:21:50 +02:00
ruff_source_file Update Rust toolchain to 1.89 (#19807) 2025-08-07 18:21:50 +02:00
ruff_text_size [ty] Fix a few more diagnostic differences from Ruff (#19806) 2025-08-08 11:31:19 -04:00
ruff_wasm Bump 0.12.8 (#19813) 2025-08-07 13:52:16 -05:00
ruff_workspace [flake8_import_conventions] Avoid false positives for NFKC-normalized __debug__ import aliases in ICN001 (#19411) 2025-08-06 06:42:51 +00:00
ty Update Rust toolchain to 1.89 (#19807) 2025-08-07 18:21:50 +02:00
ty_combine [ty] Support LSP client settings (#19614) 2025-08-06 18:37:21 +05:30
ty_ide [ty] fix goto-definition on imports (#19834) 2025-08-08 16:46:28 -04:00
ty_project [ty] Implement stdlib stub mapping (#19529) 2025-08-08 15:52:15 -04:00
ty_python_semantic [ty] Add Salsa caching to TupleType::to_class_type (#19840) 2025-08-09 09:29:26 +01:00
ty_server [ty] Send a single request for registrations/unregistrations (#19822) 2025-08-08 08:42:48 +00:00
ty_static Update Rust toolchain to 1.89 (#19807) 2025-08-07 18:21:50 +02:00
ty_test [ty] Implement stdlib stub mapping (#19529) 2025-08-08 15:52:15 -04:00
ty_vendored [ty] Improve debuggability of protocol types (#19662) 2025-08-01 15:16:13 +01:00
ty_wasm [ty] Add ty.inlayHints.variableTypes server option (#19780) 2025-08-07 19:16:51 +05:30