1
1
use std:: borrow:: Cow ;
2
- use std:: collections:: hash_map:: Entry ;
3
2
use std:: path:: Path ;
4
3
5
4
use anyhow:: { Result , anyhow} ;
6
5
use colored:: Colorize ;
7
6
use itertools:: Itertools ;
8
7
use ruff_python_parser:: semantic_errors:: SemanticSyntaxError ;
9
- use rustc_hash:: FxHashMap ;
8
+ use rustc_hash:: FxBuildHasher ;
10
9
11
10
use ruff_notebook:: Notebook ;
12
11
use ruff_python_ast:: { ModModule , PySourceType , PythonVersion } ;
@@ -23,10 +22,10 @@ use crate::checkers::imports::check_imports;
23
22
use crate :: checkers:: noqa:: check_noqa;
24
23
use crate :: checkers:: physical_lines:: check_physical_lines;
25
24
use crate :: checkers:: tokens:: check_tokens;
26
- use crate :: codes:: NoqaCode ;
27
25
use crate :: directives:: Directives ;
28
26
use crate :: doc_lines:: { doc_lines_from_ast, doc_lines_from_tokens} ;
29
27
use crate :: fix:: { FixResult , fix_file} ;
28
+ use crate :: message:: SecondaryCode ;
30
29
use crate :: noqa:: add_noqa;
31
30
use crate :: package:: PackageRoot ;
32
31
use crate :: preview:: is_py314_support_enabled;
@@ -95,33 +94,35 @@ struct FixCount {
95
94
96
95
/// A mapping from a noqa code to the corresponding lint name and a count of applied fixes.
97
96
#[ derive( Debug , Default , PartialEq ) ]
98
- pub struct FixTable ( FxHashMap < NoqaCode , FixCount > ) ;
97
+ pub struct FixTable ( hashbrown :: HashMap < SecondaryCode , FixCount , rustc_hash :: FxBuildHasher > ) ;
99
98
100
99
impl FixTable {
101
100
pub fn counts ( & self ) -> impl Iterator < Item = usize > {
102
101
self . 0 . values ( ) . map ( |fc| fc. count )
103
102
}
104
103
105
- pub fn entry ( & mut self , code : NoqaCode ) -> FixTableEntry {
106
- FixTableEntry ( self . 0 . entry ( code) )
104
+ pub fn entry < ' a > ( & ' a mut self , code : & ' a SecondaryCode ) -> FixTableEntry < ' a > {
105
+ FixTableEntry ( self . 0 . entry_ref ( code) )
107
106
}
108
107
109
- pub fn iter ( & self ) -> impl Iterator < Item = ( NoqaCode , & ' static str , usize ) > {
108
+ pub fn iter ( & self ) -> impl Iterator < Item = ( & SecondaryCode , & ' static str , usize ) > {
110
109
self . 0
111
110
. iter ( )
112
- . map ( |( code, FixCount { rule_name, count } ) | ( * code, * rule_name, * count) )
111
+ . map ( |( code, FixCount { rule_name, count } ) | ( code, * rule_name, * count) )
113
112
}
114
113
115
- pub fn keys ( & self ) -> impl Iterator < Item = NoqaCode > {
116
- self . 0 . keys ( ) . copied ( )
114
+ pub fn keys ( & self ) -> impl Iterator < Item = & SecondaryCode > {
115
+ self . 0 . keys ( )
117
116
}
118
117
119
118
pub fn is_empty ( & self ) -> bool {
120
119
self . 0 . is_empty ( )
121
120
}
122
121
}
123
122
124
- pub struct FixTableEntry < ' a > ( Entry < ' a , NoqaCode , FixCount > ) ;
123
+ pub struct FixTableEntry < ' a > (
124
+ hashbrown:: hash_map:: EntryRef < ' a , ' a , SecondaryCode , SecondaryCode , FixCount , FxBuildHasher > ,
125
+ ) ;
125
126
126
127
impl < ' a > FixTableEntry < ' a > {
127
128
pub fn or_default ( self , rule_name : & ' static str ) -> & ' a mut usize {
@@ -678,18 +679,16 @@ pub fn lint_fix<'a>(
678
679
}
679
680
}
680
681
681
- fn collect_rule_codes ( rules : impl IntoIterator < Item = NoqaCode > ) -> String {
682
- rules
683
- . into_iter ( )
684
- . map ( |rule| rule. to_string ( ) )
685
- . sorted_unstable ( )
686
- . dedup ( )
687
- . join ( ", " )
682
+ fn collect_rule_codes < T > ( rules : impl IntoIterator < Item = T > ) -> String
683
+ where
684
+ T : Ord + PartialEq + std:: fmt:: Display ,
685
+ {
686
+ rules. into_iter ( ) . sorted_unstable ( ) . dedup ( ) . join ( ", " )
688
687
}
689
688
690
689
#[ expect( clippy:: print_stderr) ]
691
690
fn report_failed_to_converge_error ( path : & Path , transformed : & str , diagnostics : & [ OldDiagnostic ] ) {
692
- let codes = collect_rule_codes ( diagnostics. iter ( ) . filter_map ( OldDiagnostic :: noqa_code ) ) ;
691
+ let codes = collect_rule_codes ( diagnostics. iter ( ) . filter_map ( OldDiagnostic :: secondary_code ) ) ;
693
692
if cfg ! ( debug_assertions) {
694
693
eprintln ! (
695
694
"{}{} Failed to converge after {} iterations in `{}` with rule codes {}:---\n {}\n ---" ,
@@ -721,11 +720,11 @@ This indicates a bug in Ruff. If you could open an issue at:
721
720
}
722
721
723
722
#[ expect( clippy:: print_stderr) ]
724
- fn report_fix_syntax_error (
723
+ fn report_fix_syntax_error < ' a > (
725
724
path : & Path ,
726
725
transformed : & str ,
727
726
error : & ParseError ,
728
- rules : impl IntoIterator < Item = NoqaCode > ,
727
+ rules : impl IntoIterator < Item = & ' a SecondaryCode > ,
729
728
) {
730
729
let codes = collect_rule_codes ( rules) ;
731
730
if cfg ! ( debug_assertions) {
0 commit comments