]> git.lizzy.rs Git - rust.git/blob - clippy_dev/src/main.rs
Auto merge of #5140 - lzutao:cleanup-replace, r=flip1995
[rust.git] / clippy_dev / src / main.rs
1 #![cfg_attr(feature = "deny-warnings", deny(warnings))]
2
3 use clap::{App, Arg, SubCommand};
4 use clippy_dev::*;
5 use std::path::Path;
6
7 mod fmt;
8 mod new_lint;
9 mod stderr_length_check;
10
11 #[derive(Clone, Copy, PartialEq)]
12 enum UpdateMode {
13     Check,
14     Change,
15 }
16
17 fn main() {
18     let matches = App::new("Clippy developer tooling")
19         .subcommand(
20             SubCommand::with_name("fmt")
21                 .about("Run rustfmt on all projects and tests")
22                 .arg(
23                     Arg::with_name("check")
24                         .long("check")
25                         .help("Use the rustfmt --check option"),
26                 )
27                 .arg(
28                     Arg::with_name("verbose")
29                         .short("v")
30                         .long("verbose")
31                         .help("Echo commands run"),
32                 ),
33         )
34         .subcommand(
35             SubCommand::with_name("update_lints")
36                 .about("Updates lint registration and information from the source code")
37                 .long_about(
38                     "Makes sure that:\n \
39                      * the lint count in README.md is correct\n \
40                      * the changelog contains markdown link references at the bottom\n \
41                      * all lint groups include the correct lints\n \
42                      * lint modules in `clippy_lints/*` are visible in `src/lib.rs` via `pub mod`\n \
43                      * all lints are registered in the lint store",
44                 )
45                 .arg(Arg::with_name("print-only").long("print-only").help(
46                     "Print a table of lints to STDOUT. \
47                      This does not include deprecated and internal lints. \
48                      (Does not modify any files)",
49                 ))
50                 .arg(
51                     Arg::with_name("check")
52                         .long("check")
53                         .help("Checks that `cargo dev update_lints` has been run. Used on CI."),
54                 ),
55         )
56         .subcommand(
57             SubCommand::with_name("new_lint")
58                 .about("Create new lint and run `cargo dev update_lints`")
59                 .arg(
60                     Arg::with_name("pass")
61                         .short("p")
62                         .long("pass")
63                         .help("Specify whether the lint runs during the early or late pass")
64                         .takes_value(true)
65                         .possible_values(&["early", "late"])
66                         .required(true),
67                 )
68                 .arg(
69                     Arg::with_name("name")
70                         .short("n")
71                         .long("name")
72                         .help("Name of the new lint in snake case, ex: fn_too_long")
73                         .takes_value(true)
74                         .required(true),
75                 )
76                 .arg(
77                     Arg::with_name("category")
78                         .short("c")
79                         .long("category")
80                         .help("What category the lint belongs to")
81                         .default_value("nursery")
82                         .possible_values(&[
83                             "style",
84                             "correctness",
85                             "complexity",
86                             "perf",
87                             "pedantic",
88                             "restriction",
89                             "cargo",
90                             "nursery",
91                             "internal",
92                             "internal_warn",
93                         ])
94                         .takes_value(true),
95                 ),
96         )
97         .arg(
98             Arg::with_name("limit-stderr-length")
99                 .long("limit-stderr-length")
100                 .help("Ensures that stderr files do not grow longer than a certain amount of lines."),
101         )
102         .get_matches();
103
104     if matches.is_present("limit-stderr-length") {
105         stderr_length_check::check();
106     }
107
108     match matches.subcommand() {
109         ("fmt", Some(matches)) => {
110             fmt::run(matches.is_present("check"), matches.is_present("verbose"));
111         },
112         ("update_lints", Some(matches)) => {
113             if matches.is_present("print-only") {
114                 print_lints();
115             } else if matches.is_present("check") {
116                 update_lints(UpdateMode::Check);
117             } else {
118                 update_lints(UpdateMode::Change);
119             }
120         },
121         ("new_lint", Some(matches)) => {
122             match new_lint::create(
123                 matches.value_of("pass"),
124                 matches.value_of("name"),
125                 matches.value_of("category"),
126             ) {
127                 Ok(_) => update_lints(UpdateMode::Change),
128                 Err(e) => eprintln!("Unable to create lint: {}", e),
129             }
130         },
131         _ => {},
132     }
133 }
134
135 fn print_lints() {
136     let lint_list = gather_all();
137     let usable_lints: Vec<Lint> = Lint::usable_lints(lint_list).collect();
138     let lint_count = usable_lints.len();
139     let grouped_by_lint_group = Lint::by_lint_group(&usable_lints);
140
141     for (lint_group, mut lints) in grouped_by_lint_group {
142         if lint_group == "Deprecated" {
143             continue;
144         }
145         println!("\n## {}", lint_group);
146
147         lints.sort_by_key(|l| l.name.clone());
148
149         for lint in lints {
150             println!(
151                 "* [{}]({}#{}) ({})",
152                 lint.name,
153                 clippy_dev::DOCS_LINK,
154                 lint.name,
155                 lint.desc
156             );
157         }
158     }
159
160     println!("there are {} lints", lint_count);
161 }
162
163 #[allow(clippy::too_many_lines)]
164 fn update_lints(update_mode: UpdateMode) {
165     let lint_list: Vec<Lint> = gather_all().collect();
166
167     let usable_lints: Vec<Lint> = Lint::usable_lints(lint_list.clone().into_iter()).collect();
168     let lint_count = usable_lints.len();
169
170     let mut sorted_usable_lints = usable_lints.clone();
171     sorted_usable_lints.sort_by_key(|lint| lint.name.clone());
172
173     let mut file_change = replace_region_in_file(
174         Path::new("src/lintlist/mod.rs"),
175         "begin lint list",
176         "end lint list",
177         false,
178         update_mode == UpdateMode::Change,
179         || {
180             format!(
181                 "pub const ALL_LINTS: [Lint; {}] = {:#?};",
182                 sorted_usable_lints.len(),
183                 sorted_usable_lints
184             )
185             .lines()
186             .map(ToString::to_string)
187             .collect::<Vec<_>>()
188         },
189     )
190     .changed;
191
192     file_change |= replace_region_in_file(
193         Path::new("README.md"),
194         &format!(r#"\[There are \d+ lints included in this crate!\]\({}\)"#, DOCS_LINK),
195         "",
196         true,
197         update_mode == UpdateMode::Change,
198         || {
199             vec![format!(
200                 "[There are {} lints included in this crate!]({})",
201                 lint_count, DOCS_LINK
202             )]
203         },
204     )
205     .changed;
206
207     file_change |= replace_region_in_file(
208         Path::new("CHANGELOG.md"),
209         "<!-- begin autogenerated links to lint list -->",
210         "<!-- end autogenerated links to lint list -->",
211         false,
212         update_mode == UpdateMode::Change,
213         || gen_changelog_lint_list(lint_list.clone()),
214     )
215     .changed;
216
217     file_change |= replace_region_in_file(
218         Path::new("clippy_lints/src/lib.rs"),
219         "begin deprecated lints",
220         "end deprecated lints",
221         false,
222         update_mode == UpdateMode::Change,
223         || gen_deprecated(&lint_list),
224     )
225     .changed;
226
227     file_change |= replace_region_in_file(
228         Path::new("clippy_lints/src/lib.rs"),
229         "begin register lints",
230         "end register lints",
231         false,
232         update_mode == UpdateMode::Change,
233         || gen_register_lint_list(&lint_list),
234     )
235     .changed;
236
237     file_change |= replace_region_in_file(
238         Path::new("clippy_lints/src/lib.rs"),
239         "begin lints modules",
240         "end lints modules",
241         false,
242         update_mode == UpdateMode::Change,
243         || gen_modules_list(lint_list.clone()),
244     )
245     .changed;
246
247     // Generate lists of lints in the clippy::all lint group
248     file_change |= replace_region_in_file(
249         Path::new("clippy_lints/src/lib.rs"),
250         r#"store.register_group\(true, "clippy::all""#,
251         r#"\]\);"#,
252         false,
253         update_mode == UpdateMode::Change,
254         || {
255             // clippy::all should only include the following lint groups:
256             let all_group_lints = usable_lints
257                 .clone()
258                 .into_iter()
259                 .filter(|l| {
260                     l.group == "correctness" || l.group == "style" || l.group == "complexity" || l.group == "perf"
261                 })
262                 .collect();
263
264             gen_lint_group_list(all_group_lints)
265         },
266     )
267     .changed;
268
269     // Generate the list of lints for all other lint groups
270     for (lint_group, lints) in Lint::by_lint_group(&usable_lints) {
271         file_change |= replace_region_in_file(
272             Path::new("clippy_lints/src/lib.rs"),
273             &format!("store.register_group\\(true, \"clippy::{}\"", lint_group),
274             r#"\]\);"#,
275             false,
276             update_mode == UpdateMode::Change,
277             || gen_lint_group_list(lints.clone()),
278         )
279         .changed;
280     }
281
282     if update_mode == UpdateMode::Check && file_change {
283         println!(
284             "Not all lints defined properly. \
285              Please run `cargo dev update_lints` to make sure all lints are defined properly."
286         );
287         std::process::exit(1);
288     }
289 }