From 3af6ccb7207aef6a802a9cda27e5f6b67217b040 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Tue, 23 Jul 2024 15:14:22 +0200 Subject: [PATCH] Fix `Ord` of `cmp_fix` (#12471) --- crates/ruff_linter/src/fix/mod.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/ruff_linter/src/fix/mod.rs b/crates/ruff_linter/src/fix/mod.rs index 17a6018fa2..d558736ff2 100644 --- a/crates/ruff_linter/src/fix/mod.rs +++ b/crates/ruff_linter/src/fix/mod.rs @@ -130,13 +130,13 @@ fn apply_fixes<'a>( /// Compare two fixes. fn cmp_fix(rule1: Rule, rule2: Rule, fix1: &Fix, fix2: &Fix) -> std::cmp::Ordering { // Always apply `RedefinedWhileUnused` before `UnusedImport`, as the latter can end up fixing - // the former. - { - match (rule1, rule2) { - (Rule::RedefinedWhileUnused, Rule::UnusedImport) => return std::cmp::Ordering::Less, - (Rule::UnusedImport, Rule::RedefinedWhileUnused) => return std::cmp::Ordering::Greater, - _ => std::cmp::Ordering::Equal, - } + // the former. But we can't apply this just for `RedefinedWhileUnused` and `UnusedImport` because it violates + // `< is transitive: a < b and b < c implies a < c. The same must hold for both == and >.` + // See https://github.com/astral-sh/ruff/issues/12469#issuecomment-2244392085 + match (rule1, rule2) { + (Rule::RedefinedWhileUnused, _) => std::cmp::Ordering::Less, + (_, Rule::RedefinedWhileUnused) => std::cmp::Ordering::Greater, + _ => std::cmp::Ordering::Equal, } // Apply fixes in order of their start position. .then_with(|| fix1.min_start().cmp(&fix2.min_start()))