@@ -7,6 +7,7 @@ use ruff_diagnostics::{IsolationLevel, SourceMap};
7
7
use ruff_text_size:: { Ranged , TextLen , TextRange , TextSize } ;
8
8
9
9
use crate :: Locator ;
10
+ use crate :: codes:: NoqaCode ;
10
11
use crate :: linter:: FixTable ;
11
12
use crate :: message:: OldDiagnostic ;
12
13
use crate :: registry:: Rule ;
@@ -63,7 +64,7 @@ fn apply_fixes<'a>(
63
64
let mut source_map = SourceMap :: default ( ) ;
64
65
65
66
for ( rule, fix) in diagnostics
66
- . filter_map ( |msg| msg. to_rule ( ) . map ( |rule| ( rule, msg) ) )
67
+ . filter_map ( |msg| msg. to_noqa_code ( ) . map ( |rule| ( rule, msg) ) )
67
68
. filter_map ( |( rule, diagnostic) | diagnostic. fix ( ) . map ( |fix| ( rule, fix) ) )
68
69
. sorted_by ( |( rule1, fix1) , ( rule2, fix2) | cmp_fix ( * rule1, * rule2, fix1, fix2) )
69
70
{
@@ -125,34 +126,44 @@ fn apply_fixes<'a>(
125
126
}
126
127
127
128
/// Compare two fixes.
128
- fn cmp_fix ( rule1 : Rule , rule2 : Rule , fix1 : & Fix , fix2 : & Fix ) -> std:: cmp:: Ordering {
129
+ fn cmp_fix ( rule1 : NoqaCode , rule2 : NoqaCode , fix1 : & Fix , fix2 : & Fix ) -> std:: cmp:: Ordering {
129
130
// Always apply `RedefinedWhileUnused` before `UnusedImport`, as the latter can end up fixing
130
131
// the former. But we can't apply this just for `RedefinedWhileUnused` and `UnusedImport` because it violates
131
132
// `< is transitive: a < b and b < c implies a < c. The same must hold for both == and >.`
132
133
// See https://github.com/astral-sh/ruff/issues/12469#issuecomment-2244392085
133
- match ( rule1, rule2) {
134
- ( Rule :: RedefinedWhileUnused , Rule :: RedefinedWhileUnused ) => std:: cmp:: Ordering :: Equal ,
135
- ( Rule :: RedefinedWhileUnused , _) => std:: cmp:: Ordering :: Less ,
136
- ( _, Rule :: RedefinedWhileUnused ) => std:: cmp:: Ordering :: Greater ,
137
- _ => std:: cmp:: Ordering :: Equal ,
134
+ let redefined_while_unused = Rule :: RedefinedWhileUnused . noqa_code ( ) ;
135
+ if ( rule1, rule2) == ( redefined_while_unused, redefined_while_unused) {
136
+ std:: cmp:: Ordering :: Equal
137
+ } else if rule1 == redefined_while_unused {
138
+ std:: cmp:: Ordering :: Less
139
+ } else if rule2 == redefined_while_unused {
140
+ std:: cmp:: Ordering :: Greater
141
+ } else {
142
+ std:: cmp:: Ordering :: Equal
138
143
}
139
144
// Apply fixes in order of their start position.
140
145
. then_with ( || fix1. min_start ( ) . cmp ( & fix2. min_start ( ) ) )
141
146
// Break ties in the event of overlapping rules, for some specific combinations.
142
- . then_with ( || match ( & rule1, & rule2) {
147
+ . then_with ( || {
148
+ let rules = ( rule1, rule2) ;
143
149
// Apply `MissingTrailingPeriod` fixes before `NewLineAfterLastParagraph` fixes.
144
- ( Rule :: MissingTrailingPeriod , Rule :: NewLineAfterLastParagraph ) => std:: cmp:: Ordering :: Less ,
145
- ( Rule :: NewLineAfterLastParagraph , Rule :: MissingTrailingPeriod ) => {
150
+ let missing_trailing_period = Rule :: MissingTrailingPeriod . noqa_code ( ) ;
151
+ let newline_after_last_paragraph = Rule :: NewLineAfterLastParagraph . noqa_code ( ) ;
152
+ let if_else_instead_of_dict_get = Rule :: IfElseBlockInsteadOfDictGet . noqa_code ( ) ;
153
+ let if_else_instead_of_if_exp = Rule :: IfElseBlockInsteadOfIfExp . noqa_code ( ) ;
154
+ if rules == ( missing_trailing_period, newline_after_last_paragraph) {
155
+ std:: cmp:: Ordering :: Less
156
+ } else if rules == ( newline_after_last_paragraph, missing_trailing_period) {
146
157
std:: cmp:: Ordering :: Greater
147
158
}
148
159
// Apply `IfElseBlockInsteadOfDictGet` fixes before `IfElseBlockInsteadOfIfExp` fixes.
149
- ( Rule :: IfElseBlockInsteadOfDictGet , Rule :: IfElseBlockInsteadOfIfExp ) => {
160
+ else if rules == ( if_else_instead_of_dict_get , if_else_instead_of_if_exp ) {
150
161
std:: cmp:: Ordering :: Less
151
- }
152
- ( Rule :: IfElseBlockInsteadOfIfExp , Rule :: IfElseBlockInsteadOfDictGet ) => {
162
+ } else if rules == ( if_else_instead_of_if_exp, if_else_instead_of_dict_get) {
153
163
std:: cmp:: Ordering :: Greater
164
+ } else {
165
+ std:: cmp:: Ordering :: Equal
154
166
}
155
- _ => std:: cmp:: Ordering :: Equal ,
156
167
} )
157
168
}
158
169
0 commit comments