Add support for sorting diagnostics without a range (#20257)

- Update ruff ordering compare to work with optional ranges (treating
  them as starting from zero)
This commit is contained in:
Amethyst Reese 2025-09-05 15:23:30 -07:00 committed by GitHub
parent 5d52902e18
commit a27c64811e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -501,13 +501,18 @@ impl Diagnostic {
/// Returns the ordering of diagnostics based on the start of their ranges, if they have any.
///
/// Panics if either diagnostic has no primary span, if the span has no range, or if its file is
/// not a `SourceFile`.
/// Panics if either diagnostic has no primary span, or if its file is not a `SourceFile`.
pub fn ruff_start_ordering(&self, other: &Self) -> std::cmp::Ordering {
(self.expect_ruff_source_file(), self.expect_range().start()).cmp(&(
let a = (
self.expect_ruff_source_file(),
self.range().map(|r| r.start()),
);
let b = (
other.expect_ruff_source_file(),
other.expect_range().start(),
))
other.range().map(|r| r.start()),
);
a.cmp(&b)
}
}