@@ -2,6 +2,7 @@ use crate::{
22 gather_all, gen_changelog_lint_list, gen_deprecated, gen_lint_group_list, gen_modules_list, gen_register_lint_list,
33 replace_region_in_file, Lint , DOCS_LINK ,
44} ;
5+ use std:: fs;
56use std:: path:: Path ;
67
78#[ derive( Clone , Copy , PartialEq ) ]
@@ -10,6 +11,15 @@ pub enum UpdateMode {
1011 Change ,
1112}
1213
14+ /// Runs the `update_lints` command.
15+ ///
16+ /// This updates various generated values from the lint source code.
17+ ///
18+ /// `update_mode` indicates if the files should be updated or if updates should be checked for.
19+ ///
20+ /// # Panics
21+ ///
22+ /// Panics if a file path could not read from or then written to
1323#[ allow( clippy:: too_many_lines) ]
1424pub fn run ( update_mode : UpdateMode ) {
1525 let lint_list: Vec < Lint > = gather_all ( ) . collect ( ) ;
@@ -52,76 +62,37 @@ pub fn run(update_mode: UpdateMode) {
5262 )
5363 . changed ;
5464
55- file_change |= replace_region_in_file (
56- Path :: new ( "clippy_lints/src/lib.rs" ) ,
57- "begin deprecated lints" ,
58- "end deprecated lints" ,
59- false ,
60- update_mode == UpdateMode :: Change ,
61- || gen_deprecated ( deprecated_lints. iter ( ) ) ,
62- )
63- . changed ;
64-
65- file_change |= replace_region_in_file (
66- Path :: new ( "clippy_lints/src/lib.rs" ) ,
67- "begin register lints" ,
68- "end register lints" ,
69- false ,
70- update_mode == UpdateMode :: Change ,
71- || gen_register_lint_list ( internal_lints. iter ( ) , usable_lints. iter ( ) ) ,
72- )
73- . changed ;
74-
75- file_change |= replace_region_in_file (
76- Path :: new ( "clippy_lints/src/lib.rs" ) ,
77- "begin lints modules" ,
78- "end lints modules" ,
79- false ,
80- update_mode == UpdateMode :: Change ,
81- || gen_modules_list ( usable_lints. iter ( ) ) ,
82- )
83- . changed ;
65+ if file_change && update_mode == UpdateMode :: Check {
66+ exit_with_failure ( ) ;
67+ }
8468
85- // Generate lists of lints in the clippy::all lint group
86- file_change |= replace_region_in_file (
87- Path :: new ( "clippy_lints/src/lib.rs" ) ,
88- r#"store.register_group\(true, "clippy::all""# ,
89- r#"\]\);"# ,
90- false ,
91- update_mode == UpdateMode :: Change ,
92- || {
93- // clippy::all should only include the following lint groups:
69+ for ( name, lines) in [
70+ ( "mods" , gen_modules_list ( usable_lints. iter ( ) ) ) ,
71+ ( "deprecated" , gen_deprecated ( deprecated_lints. iter ( ) ) ) ,
72+ (
73+ "register_lints" ,
74+ gen_register_lint_list ( internal_lints. iter ( ) , usable_lints. iter ( ) ) ,
75+ ) ,
76+ ( "register_all" , {
9477 let all_group_lints = usable_lints. iter ( ) . filter ( |l| {
9578 matches ! (
9679 & * l. group,
9780 "correctness" | "suspicious" | "style" | "complexity" | "perf"
9881 )
9982 } ) ;
10083
101- gen_lint_group_list ( all_group_lints)
102- } ,
103- )
104- . changed ;
105-
106- // Generate the list of lints for all other lint groups
107- for ( lint_group, lints) in Lint :: by_lint_group ( usable_lints. into_iter ( ) . chain ( internal_lints) ) {
108- file_change |= replace_region_in_file (
109- Path :: new ( "clippy_lints/src/lib.rs" ) ,
110- & format ! ( "store.register_group\\ (true, \" clippy::{}\" " , lint_group) ,
111- r#"\]\);"# ,
112- false ,
113- update_mode == UpdateMode :: Change ,
114- || gen_lint_group_list ( lints. iter ( ) ) ,
115- )
116- . changed ;
84+ gen_lint_group_list ( "all" , all_group_lints)
85+ } ) ,
86+ ] {
87+ process_file ( & format ! ( "clippy_lints/src/lib.{}.rs" , name) , update_mode, & lines[ ..] ) ;
11788 }
11889
119- if update_mode == UpdateMode :: Check && file_change {
120- println ! (
121- "Not all lints defined properly. \
122- Please run `cargo dev update_lints` to make sure all lints are defined properly."
90+ for ( lint_group, lints) in Lint :: by_lint_group ( usable_lints. into_iter ( ) . chain ( internal_lints) ) {
91+ process_file (
92+ & format ! ( "clippy_lints/src/lib.register_{}.rs" , lint_group) ,
93+ update_mode,
94+ & gen_lint_group_list ( & lints. get ( 0 ) . expect ( "group non-empty" ) . group , lints. iter ( ) ) [ ..] ,
12395 ) ;
124- std:: process:: exit ( 1 ) ;
12596 }
12697}
12798
@@ -150,3 +121,30 @@ pub fn print_lints() {
150121fn round_to_fifty ( count : usize ) -> usize {
151122 count / 50 * 50
152123}
124+
125+ fn process_file ( path : impl AsRef < Path > , update_mode : UpdateMode , new_lines : & [ String ] ) {
126+ let mut new_content = "// This file was generated by `cargo dev update_lints`.\n \
127+ // Use that command to update this file and do not edit by hand.\n \
128+ // Manual edits will be overwritten.\n \n "
129+ . to_string ( ) ;
130+ new_content. push_str ( & new_lines. join ( "\n " ) ) ;
131+
132+ if update_mode == UpdateMode :: Check {
133+ let old_content =
134+ fs:: read_to_string ( & path) . unwrap_or_else ( |e| panic ! ( "Cannot read from {}: {}" , path. as_ref( ) . display( ) , e) ) ;
135+ if new_content != old_content {
136+ exit_with_failure ( ) ;
137+ }
138+ } else {
139+ fs:: write ( & path, new_content. as_bytes ( ) )
140+ . unwrap_or_else ( |e| panic ! ( "Cannot write to {}: {}" , path. as_ref( ) . display( ) , e) ) ;
141+ }
142+ }
143+
144+ fn exit_with_failure ( ) {
145+ println ! (
146+ "Not all lints defined properly. \
147+ Please run `cargo dev update_lints` to make sure all lints are defined properly."
148+ ) ;
149+ std:: process:: exit ( 1 ) ;
150+ }
0 commit comments