11use std:: cmp:: Reverse ;
2- use std:: fmt:: Display ;
32use std:: hash:: Hash ;
43use std:: io:: Write ;
54
65use anyhow:: Result ;
76use bitflags:: bitflags;
87use colored:: Colorize ;
98use itertools:: { Itertools , iterate} ;
9+ use ruff_linter:: codes:: NoqaCode ;
1010use serde:: Serialize ;
1111
1212use ruff_linter:: fs:: relativize_path;
1313use ruff_linter:: logging:: LogLevel ;
1414use ruff_linter:: message:: {
1515 AzureEmitter , Emitter , EmitterContext , GithubEmitter , GitlabEmitter , GroupedEmitter ,
16- JsonEmitter , JsonLinesEmitter , JunitEmitter , Message , MessageKind , PylintEmitter ,
17- RdjsonEmitter , SarifEmitter , TextEmitter ,
16+ JsonEmitter , JsonLinesEmitter , JunitEmitter , Message , PylintEmitter , RdjsonEmitter ,
17+ SarifEmitter , TextEmitter ,
1818} ;
1919use ruff_linter:: notify_user;
20- use ruff_linter:: registry:: Rule ;
2120use ruff_linter:: settings:: flags:: { self } ;
2221use ruff_linter:: settings:: types:: { OutputFormat , UnsafeFixes } ;
2322
@@ -37,59 +36,12 @@ bitflags! {
3736
3837#[ derive( Serialize ) ]
3938struct ExpandedStatistics {
40- code : Option < SerializeRuleAsCode > ,
41- name : SerializeMessageKindAsTitle ,
39+ code : Option < NoqaCode > ,
40+ name : & ' static str ,
4241 count : usize ,
4342 fixable : bool ,
4443}
4544
46- #[ derive( Copy , Clone ) ]
47- struct SerializeRuleAsCode ( Rule ) ;
48-
49- impl Serialize for SerializeRuleAsCode {
50- fn serialize < S > ( & self , serializer : S ) -> std:: result:: Result < S :: Ok , S :: Error >
51- where
52- S : serde:: Serializer ,
53- {
54- serializer. serialize_str ( & self . 0 . noqa_code ( ) . to_string ( ) )
55- }
56- }
57-
58- impl Display for SerializeRuleAsCode {
59- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
60- write ! ( f, "{}" , self . 0 . noqa_code( ) )
61- }
62- }
63-
64- impl From < Rule > for SerializeRuleAsCode {
65- fn from ( rule : Rule ) -> Self {
66- Self ( rule)
67- }
68- }
69-
70- struct SerializeMessageKindAsTitle ( MessageKind ) ;
71-
72- impl Serialize for SerializeMessageKindAsTitle {
73- fn serialize < S > ( & self , serializer : S ) -> std:: result:: Result < S :: Ok , S :: Error >
74- where
75- S : serde:: Serializer ,
76- {
77- serializer. serialize_str ( self . 0 . as_str ( ) )
78- }
79- }
80-
81- impl Display for SerializeMessageKindAsTitle {
82- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
83- f. write_str ( self . 0 . as_str ( ) )
84- }
85- }
86-
87- impl From < MessageKind > for SerializeMessageKindAsTitle {
88- fn from ( kind : MessageKind ) -> Self {
89- Self ( kind)
90- }
91- }
92-
9345pub ( crate ) struct Printer {
9446 format : OutputFormat ,
9547 log_level : LogLevel ,
@@ -350,21 +302,25 @@ impl Printer {
350302 let statistics: Vec < ExpandedStatistics > = diagnostics
351303 . messages
352304 . iter ( )
353- . sorted_by_key ( |message| ( message. rule ( ) , message. fixable ( ) ) )
354- . fold ( vec ! [ ] , |mut acc : Vec < ( & Message , usize ) > , message| {
355- if let Some ( ( prev_message, count) ) = acc. last_mut ( ) {
356- if prev_message. rule ( ) == message. rule ( ) {
357- * count += 1 ;
358- return acc;
305+ . map ( |message| ( message. to_noqa_code ( ) , message) )
306+ . sorted_by_key ( |( code, message) | ( * code, message. fixable ( ) ) )
307+ . fold (
308+ vec ! [ ] ,
309+ |mut acc : Vec < ( ( Option < NoqaCode > , & Message ) , usize ) > , ( code, message) | {
310+ if let Some ( ( ( prev_code, _prev_message) , count) ) = acc. last_mut ( ) {
311+ if * prev_code == code {
312+ * count += 1 ;
313+ return acc;
314+ }
359315 }
360- }
361- acc . push ( ( message , 1 ) ) ;
362- acc
363- } )
316+ acc . push ( ( ( code , message ) , 1 ) ) ;
317+ acc
318+ } ,
319+ )
364320 . iter ( )
365- . map ( |& ( message, count) | ExpandedStatistics {
366- code : message . rule ( ) . map ( std :: convert :: Into :: into ) ,
367- name : message. kind ( ) . into ( ) ,
321+ . map ( |& ( ( code , message) , count) | ExpandedStatistics {
322+ code,
323+ name : message. name ( ) ,
368324 count,
369325 fixable : if let Some ( fix) = message. fix ( ) {
370326 fix. applies ( self . unsafe_fixes . required_applicability ( ) )
0 commit comments