1- use std:: fmt:: { self , Write } ;
1+ use itertools:: Itertools ;
2+ use std:: fmt;
23
34#[ derive( Debug , Clone , Default ) ]
45pub struct ClippyConfiguration {
56 pub name : String ,
67 pub default : String ,
7- pub lints : Vec < String > ,
8- pub doc : String ,
8+ pub lints : & ' static [ & ' static str ] ,
9+ pub doc : & ' static str ,
910 pub deprecation_reason : Option < & ' static str > ,
1011}
1112
@@ -20,102 +21,20 @@ impl fmt::Display for ClippyConfiguration {
2021}
2122
2223impl ClippyConfiguration {
23- pub fn new (
24- name : & ' static str ,
25- default : String ,
26- doc_comment : & ' static str ,
27- deprecation_reason : Option < & ' static str > ,
28- ) -> Self {
29- let ( mut lints, doc) = parse_config_field_doc ( doc_comment)
30- . unwrap_or_else ( || ( vec ! [ ] , "[ERROR] MALFORMED DOC COMMENT" . to_string ( ) ) ) ;
31-
32- lints. sort ( ) ;
33-
34- Self {
35- name : to_kebab ( name) ,
36- lints,
37- doc,
38- default,
39- deprecation_reason,
40- }
41- }
42-
4324 pub fn to_markdown_paragraph ( & self ) -> String {
44- let mut out = format ! (
45- "## `{}`\n {}\n \n " ,
25+ format ! (
26+ "## `{}`\n {}\n \n **Default Value:** `{}` \n \n --- \n **Affected lints:** \n {} \n \n " ,
4627 self . name,
47- self . doc
48- . lines( )
49- . map( |line| line. strip_prefix( " " ) . unwrap_or( line) )
50- . collect:: <Vec <_>>( )
51- . join( "\n " ) ,
52- ) ;
53-
54- if !self . default . is_empty ( ) {
55- write ! ( out, "**Default Value:** `{}`\n \n " , self . default ) . unwrap ( ) ;
56- }
57-
58- write ! (
59- out,
60- "---\n **Affected lints:**\n {}\n \n " ,
61- self . lints
62- . iter( )
63- . map( |name| name. to_string( ) . split_whitespace( ) . next( ) . unwrap( ) . to_string( ) )
64- . map( |name| format!( "* [`{name}`](https://rust-lang.github.io/rust-clippy/master/index.html#{name})" ) )
65- . collect:: <Vec <_>>( )
66- . join( "\n " ) ,
28+ self . doc. lines( ) . map( |x| x. strip_prefix( ' ' ) . unwrap_or( x) ) . join( "\n " ) ,
29+ self . default ,
30+ self . lints. iter( ) . format_with( "\n " , |name, f| f( & format_args!(
31+ "* [`{name}`](https://rust-lang.github.io/rust-clippy/master/index.html#{name})"
32+ ) ) ) ,
6733 )
68- . unwrap ( ) ;
69-
70- out
7134 }
7235
7336 pub fn to_markdown_link ( & self ) -> String {
7437 const BOOK_CONFIGS_PATH : & str = "https://doc.rust-lang.org/clippy/lint_configuration.html" ;
7538 format ! ( "[`{}`]: {BOOK_CONFIGS_PATH}#{}" , self . name, self . name)
7639 }
7740}
78-
79- /// This parses the field documentation of the config struct.
80- ///
81- /// ```rust, ignore
82- /// parse_config_field_doc(cx, "Lint: LINT_NAME_1, LINT_NAME_2. Papa penguin, papa penguin")
83- /// ```
84- ///
85- /// Would yield:
86- /// ```rust, ignore
87- /// Some(["lint_name_1", "lint_name_2"], "Papa penguin, papa penguin")
88- /// ```
89- fn parse_config_field_doc ( doc_comment : & str ) -> Option < ( Vec < String > , String ) > {
90- const DOC_START : & str = " Lint: " ;
91- if doc_comment. starts_with ( DOC_START )
92- && let Some ( split_pos) = doc_comment. find ( '.' )
93- {
94- let mut doc_comment = doc_comment. to_string ( ) ;
95- let mut documentation = doc_comment. split_off ( split_pos) ;
96-
97- // Extract lints
98- doc_comment. make_ascii_lowercase ( ) ;
99- let lints: Vec < String > = doc_comment
100- . split_off ( DOC_START . len ( ) )
101- . lines ( )
102- . next ( )
103- . unwrap ( )
104- . split ( ", " )
105- . map ( str:: to_string)
106- . collect ( ) ;
107-
108- // Format documentation correctly
109- // split off leading `.` from lint name list and indent for correct formatting
110- documentation = documentation. trim_start_matches ( '.' ) . trim ( ) . replace ( "\n " , "\n " ) ;
111-
112- Some ( ( lints, documentation) )
113- } else {
114- None
115- }
116- }
117-
118- /// Transforms a given `snake_case_string` to a tasty `kebab-case-string`
119- fn to_kebab ( config_name : & str ) -> String {
120- config_name. replace ( '_' , "-" )
121- }
0 commit comments