11#![ cfg_attr( feature = "deny-warnings" , deny( warnings) ) ]
22
33use clap:: { App , Arg , SubCommand } ;
4- use clippy_dev:: {
5- gather_all, gen_changelog_lint_list, gen_deprecated, gen_lint_group_list, gen_modules_list, gen_register_lint_list,
6- replace_region_in_file, Lint , DOCS_LINK ,
7- } ;
8- use std:: path:: Path ;
9-
10- mod fmt;
11- mod new_lint;
12- mod stderr_length_check;
13-
14- #[ derive( Clone , Copy , PartialEq ) ]
15- enum UpdateMode {
16- Check ,
17- Change ,
18- }
4+ use clippy_dev:: { fmt, new_lint, stderr_length_check, update_lints} ;
195
206fn main ( ) {
217 let matches = App :: new ( "Clippy developer tooling" )
@@ -97,28 +83,23 @@ fn main() {
9783 . takes_value ( true ) ,
9884 ) ,
9985 )
100- . arg (
101- Arg :: with_name ( "limit-stderr-length" )
102- . long ( "limit-stderr-length" )
103- . help ( "Ensures that stderr files do not grow longer than a certain amount of lines." ) ,
86+ . subcommand (
87+ SubCommand :: with_name ( "limit_stderr_length" )
88+ . about ( "Ensures that stderr files do not grow longer than a certain amount of lines." ) ,
10489 )
10590 . get_matches ( ) ;
10691
107- if matches. is_present ( "limit-stderr-length" ) {
108- stderr_length_check:: check ( ) ;
109- }
110-
11192 match matches. subcommand ( ) {
11293 ( "fmt" , Some ( matches) ) => {
11394 fmt:: run ( matches. is_present ( "check" ) , matches. is_present ( "verbose" ) ) ;
11495 } ,
11596 ( "update_lints" , Some ( matches) ) => {
11697 if matches. is_present ( "print-only" ) {
117- print_lints ( ) ;
98+ update_lints :: print_lints ( ) ;
11899 } else if matches. is_present ( "check" ) {
119- update_lints ( UpdateMode :: Check ) ;
100+ update_lints:: run ( update_lints :: UpdateMode :: Check ) ;
120101 } else {
121- update_lints ( UpdateMode :: Change ) ;
102+ update_lints:: run ( update_lints :: UpdateMode :: Change ) ;
122103 }
123104 } ,
124105 ( "new_lint" , Some ( matches) ) => {
@@ -127,168 +108,13 @@ fn main() {
127108 matches. value_of ( "name" ) ,
128109 matches. value_of ( "category" ) ,
129110 ) {
130- Ok ( _) => update_lints ( UpdateMode :: Change ) ,
111+ Ok ( _) => update_lints:: run ( update_lints :: UpdateMode :: Change ) ,
131112 Err ( e) => eprintln ! ( "Unable to create lint: {}" , e) ,
132113 }
133114 } ,
134- _ => { } ,
135- }
136- }
137-
138- fn print_lints ( ) {
139- let lint_list = gather_all ( ) ;
140- let usable_lints: Vec < Lint > = Lint :: usable_lints ( lint_list) . collect ( ) ;
141- let usable_lint_count = usable_lints. len ( ) ;
142- let grouped_by_lint_group = Lint :: by_lint_group ( usable_lints. into_iter ( ) ) ;
143-
144- for ( lint_group, mut lints) in grouped_by_lint_group {
145- if lint_group == "Deprecated" {
146- continue ;
147- }
148- println ! ( "\n ## {}" , lint_group) ;
149-
150- lints. sort_by_key ( |l| l. name . clone ( ) ) ;
151-
152- for lint in lints {
153- println ! (
154- "* [{}]({}#{}) ({})" ,
155- lint. name,
156- clippy_dev:: DOCS_LINK ,
157- lint. name,
158- lint. desc
159- ) ;
160- }
161- }
162-
163- println ! ( "there are {} lints" , usable_lint_count) ;
164- }
165-
166- #[ allow( clippy:: too_many_lines) ]
167- fn update_lints ( update_mode : UpdateMode ) {
168- let lint_list: Vec < Lint > = gather_all ( ) . collect ( ) ;
169-
170- let internal_lints = Lint :: internal_lints ( lint_list. clone ( ) . into_iter ( ) ) ;
171-
172- let usable_lints: Vec < Lint > = Lint :: usable_lints ( lint_list. clone ( ) . into_iter ( ) ) . collect ( ) ;
173- let usable_lint_count = usable_lints. len ( ) ;
174-
175- let mut sorted_usable_lints = usable_lints. clone ( ) ;
176- sorted_usable_lints. sort_by_key ( |lint| lint. name . clone ( ) ) ;
177-
178- let mut file_change = replace_region_in_file (
179- Path :: new ( "src/lintlist/mod.rs" ) ,
180- "begin lint list" ,
181- "end lint list" ,
182- false ,
183- update_mode == UpdateMode :: Change ,
184- || {
185- format ! (
186- "pub const ALL_LINTS: [Lint; {}] = {:#?};" ,
187- sorted_usable_lints. len( ) ,
188- sorted_usable_lints
189- )
190- . lines ( )
191- . map ( ToString :: to_string)
192- . collect :: < Vec < _ > > ( )
193- } ,
194- )
195- . changed ;
196-
197- file_change |= replace_region_in_file (
198- Path :: new ( "README.md" ) ,
199- & format ! ( r#"\[There are \d+ lints included in this crate!\]\({}\)"# , DOCS_LINK ) ,
200- "" ,
201- true ,
202- update_mode == UpdateMode :: Change ,
203- || {
204- vec ! [ format!(
205- "[There are {} lints included in this crate!]({})" ,
206- usable_lint_count, DOCS_LINK
207- ) ]
115+ ( "limit_stderr_length" , _) => {
116+ stderr_length_check:: check ( ) ;
208117 } ,
209- )
210- . changed ;
211-
212- file_change |= replace_region_in_file (
213- Path :: new ( "CHANGELOG.md" ) ,
214- "<!-- begin autogenerated links to lint list -->" ,
215- "<!-- end autogenerated links to lint list -->" ,
216- false ,
217- update_mode == UpdateMode :: Change ,
218- || gen_changelog_lint_list ( lint_list. clone ( ) ) ,
219- )
220- . changed ;
221-
222- file_change |= replace_region_in_file (
223- Path :: new ( "clippy_lints/src/lib.rs" ) ,
224- "begin deprecated lints" ,
225- "end deprecated lints" ,
226- false ,
227- update_mode == UpdateMode :: Change ,
228- || gen_deprecated ( & lint_list) ,
229- )
230- . changed ;
231-
232- file_change |= replace_region_in_file (
233- Path :: new ( "clippy_lints/src/lib.rs" ) ,
234- "begin register lints" ,
235- "end register lints" ,
236- false ,
237- update_mode == UpdateMode :: Change ,
238- || gen_register_lint_list ( & lint_list) ,
239- )
240- . changed ;
241-
242- file_change |= replace_region_in_file (
243- Path :: new ( "clippy_lints/src/lib.rs" ) ,
244- "begin lints modules" ,
245- "end lints modules" ,
246- false ,
247- update_mode == UpdateMode :: Change ,
248- || gen_modules_list ( lint_list. clone ( ) ) ,
249- )
250- . changed ;
251-
252- // Generate lists of lints in the clippy::all lint group
253- file_change |= replace_region_in_file (
254- Path :: new ( "clippy_lints/src/lib.rs" ) ,
255- r#"store.register_group\(true, "clippy::all""# ,
256- r#"\]\);"# ,
257- false ,
258- update_mode == UpdateMode :: Change ,
259- || {
260- // clippy::all should only include the following lint groups:
261- let all_group_lints = usable_lints
262- . clone ( )
263- . into_iter ( )
264- . filter ( |l| {
265- l. group == "correctness" || l. group == "style" || l. group == "complexity" || l. group == "perf"
266- } )
267- . collect ( ) ;
268-
269- gen_lint_group_list ( all_group_lints)
270- } ,
271- )
272- . changed ;
273-
274- // Generate the list of lints for all other lint groups
275- for ( lint_group, lints) in Lint :: by_lint_group ( usable_lints. into_iter ( ) . chain ( internal_lints) ) {
276- file_change |= replace_region_in_file (
277- Path :: new ( "clippy_lints/src/lib.rs" ) ,
278- & format ! ( "store.register_group\\ (true, \" clippy::{}\" " , lint_group) ,
279- r#"\]\);"# ,
280- false ,
281- update_mode == UpdateMode :: Change ,
282- || gen_lint_group_list ( lints. clone ( ) ) ,
283- )
284- . changed ;
285- }
286-
287- if update_mode == UpdateMode :: Check && file_change {
288- println ! (
289- "Not all lints defined properly. \
290- Please run `cargo dev update_lints` to make sure all lints are defined properly."
291- ) ;
292- std:: process:: exit ( 1 ) ;
118+ _ => { } ,
293119 }
294120}
0 commit comments