@@ -14,6 +14,7 @@ use clippy_utils::diagnostics::span_lint;
1414use clippy_utils:: ty:: { match_type, walk_ptrs_ty_depth} ;
1515use clippy_utils:: { last_path_segment, match_def_path, match_function_call, match_path, paths} ;
1616use if_chain:: if_chain;
17+ use itertools:: Itertools ;
1718use rustc_ast as ast;
1819use rustc_data_structures:: fx:: FxHashMap ;
1920use rustc_hir:: {
@@ -34,8 +35,10 @@ use std::path::Path;
3435use std:: path:: PathBuf ;
3536use std:: process:: Command ;
3637
37- /// This is the output file of the lint collector.
38- const OUTPUT_FILE : & str = "../util/gh-pages/lints.json" ;
38+ /// This is the json output file of the lint collector.
39+ const JSON_OUTPUT_FILE : & str = "../util/gh-pages/lints.json" ;
40+ /// This is the markdown output file of the lint collector.
41+ const MARKDOWN_OUTPUT_FILE : & str = "../book/src/lint_configuration.md" ;
3942/// These lints are excluded from the export.
4043const BLACK_LISTED_LINTS : & [ & str ] = & [ "lint_author" , "dump_hir" , "internal_metadata_collector" ] ;
4144/// These groups will be ignored by the lint group matcher. This is useful for collections like
@@ -176,6 +179,23 @@ This lint has the following configuration variables:
176179 )
177180 } )
178181 }
182+
183+ fn configs_to_markdown ( & self , map_fn : fn ( & ClippyConfiguration ) -> String ) -> String {
184+ self . config
185+ . iter ( )
186+ . filter ( |config| config. deprecation_reason . is_none ( ) )
187+ . filter ( |config| !config. lints . is_empty ( ) )
188+ . map ( map_fn)
189+ . join ( "\n " )
190+ }
191+
192+ fn get_markdown_docs ( & self ) -> String {
193+ format ! (
194+ "## Lint Configuration Options\n | <div style=\" width:290px\" >Option</div> | Default Value |\n |--|--|\n {}\n \n {}\n " ,
195+ self . configs_to_markdown( ClippyConfiguration :: to_markdown_table_entry) ,
196+ self . configs_to_markdown( ClippyConfiguration :: to_markdown_paragraph) ,
197+ )
198+ }
179199}
180200
181201impl Drop for MetadataCollector {
@@ -199,12 +219,37 @@ impl Drop for MetadataCollector {
199219
200220 collect_renames ( & mut lints) ;
201221
202- // Outputting
203- if Path :: new ( OUTPUT_FILE ) . exists ( ) {
204- fs:: remove_file ( OUTPUT_FILE ) . unwrap ( ) ;
222+ // Outputting json
223+ if Path :: new ( JSON_OUTPUT_FILE ) . exists ( ) {
224+ fs:: remove_file ( JSON_OUTPUT_FILE ) . unwrap ( ) ;
205225 }
206- let mut file = OpenOptions :: new ( ) . write ( true ) . create ( true ) . open ( OUTPUT_FILE ) . unwrap ( ) ;
226+ let mut file = OpenOptions :: new ( )
227+ . write ( true )
228+ . create ( true )
229+ . open ( JSON_OUTPUT_FILE )
230+ . unwrap ( ) ;
207231 writeln ! ( file, "{}" , serde_json:: to_string_pretty( & lints) . unwrap( ) ) . unwrap ( ) ;
232+
233+ // Outputting markdown
234+ if Path :: new ( MARKDOWN_OUTPUT_FILE ) . exists ( ) {
235+ fs:: remove_file ( MARKDOWN_OUTPUT_FILE ) . unwrap ( ) ;
236+ }
237+ let mut file = OpenOptions :: new ( )
238+ . write ( true )
239+ . create ( true )
240+ . open ( MARKDOWN_OUTPUT_FILE )
241+ . unwrap ( ) ;
242+ writeln ! (
243+ file,
244+ "<!--
245+ This file is generated by `cargo collect-metadata`.
246+ Please use that command to update the file and do not edit it by hand.
247+ -->
248+
249+ {}" ,
250+ self . get_markdown_docs( ) ,
251+ )
252+ . unwrap ( ) ;
208253 }
209254}
210255
@@ -505,6 +550,28 @@ impl ClippyConfiguration {
505550 deprecation_reason,
506551 }
507552 }
553+
554+ fn to_markdown_paragraph ( & self ) -> String {
555+ format ! (
556+ "### {}\n {}\n \n **Default Value:** `{}` (`{}`)\n \n {}\n \n " ,
557+ self . name,
558+ self . doc
559+ . lines( )
560+ . map( |line| line. strip_prefix( " " ) . unwrap_or( line) )
561+ . join( "\n " ) ,
562+ self . default ,
563+ self . config_type,
564+ self . lints
565+ . iter( )
566+ . map( |name| name. to_string( ) . split_whitespace( ) . next( ) . unwrap( ) . to_string( ) )
567+ . map( |name| format!( "* [{name}](https://rust-lang.github.io/rust-clippy/master/index.html#{name})" ) )
568+ . join( "\n " ) ,
569+ )
570+ }
571+
572+ fn to_markdown_table_entry ( & self ) -> String {
573+ format ! ( "| [{}](#{}) | `{}` |" , self . name, self . name, self . default )
574+ }
508575}
509576
510577fn collect_configs ( ) -> Vec < ClippyConfiguration > {
0 commit comments