@@ -2,6 +2,7 @@ use crate::{
2
2
gather_all, gen_changelog_lint_list, gen_deprecated, gen_lint_group_list, gen_modules_list, gen_register_lint_list,
3
3
replace_region_in_file, Lint , DOCS_LINK ,
4
4
} ;
5
+ use std:: fs;
5
6
use std:: path:: Path ;
6
7
7
8
#[ derive( Clone , Copy , PartialEq ) ]
@@ -10,6 +11,15 @@ pub enum UpdateMode {
10
11
Change ,
11
12
}
12
13
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
13
23
#[ allow( clippy:: too_many_lines) ]
14
24
pub fn run ( update_mode : UpdateMode ) {
15
25
let lint_list: Vec < Lint > = gather_all ( ) . collect ( ) ;
@@ -52,76 +62,37 @@ pub fn run(update_mode: UpdateMode) {
52
62
)
53
63
. changed ;
54
64
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
+ }
84
68
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" , {
94
77
let all_group_lints = usable_lints. iter ( ) . filter ( |l| {
95
78
matches ! (
96
79
& * l. group,
97
80
"correctness" | "suspicious" | "style" | "complexity" | "perf"
98
81
)
99
82
} ) ;
100
83
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[ ..] ) ;
117
88
}
118
89
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 ( ) ) [ ..] ,
123
95
) ;
124
- std:: process:: exit ( 1 ) ;
125
96
}
126
97
}
127
98
@@ -150,3 +121,30 @@ pub fn print_lints() {
150
121
fn round_to_fifty ( count : usize ) -> usize {
151
122
count / 50 * 50
152
123
}
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