@@ -14,6 +14,7 @@ use clippy_utils::diagnostics::span_lint;
14
14
use clippy_utils:: ty:: { match_type, walk_ptrs_ty_depth} ;
15
15
use clippy_utils:: { last_path_segment, match_def_path, match_function_call, match_path, paths} ;
16
16
use if_chain:: if_chain;
17
+ use itertools:: Itertools ;
17
18
use rustc_ast as ast;
18
19
use rustc_data_structures:: fx:: FxHashMap ;
19
20
use rustc_hir:: {
@@ -34,8 +35,10 @@ use std::path::Path;
34
35
use std:: path:: PathBuf ;
35
36
use std:: process:: Command ;
36
37
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" ;
39
42
/// These lints are excluded from the export.
40
43
const BLACK_LISTED_LINTS : & [ & str ] = & [ "lint_author" , "dump_hir" , "internal_metadata_collector" ] ;
41
44
/// 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:
176
179
)
177
180
} )
178
181
}
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
+ }
179
199
}
180
200
181
201
impl Drop for MetadataCollector {
@@ -199,12 +219,37 @@ impl Drop for MetadataCollector {
199
219
200
220
collect_renames ( & mut lints) ;
201
221
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 ( ) ;
205
225
}
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 ( ) ;
207
231
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 ( ) ;
208
253
}
209
254
}
210
255
@@ -505,6 +550,28 @@ impl ClippyConfiguration {
505
550
deprecation_reason,
506
551
}
507
552
}
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
+ }
508
575
}
509
576
510
577
fn collect_configs ( ) -> Vec < ClippyConfiguration > {
0 commit comments