]> git.lizzy.rs Git - rust.git/blob - clippy_dev/src/main.rs
Rename lint_count -> usable_lint_count
[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 usable_lint_count = usable_lints.len();
139     let grouped_by_lint_group = Lint::by_lint_group(usable_lints.into_iter());
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", usable_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 internal_lints = Lint::internal_lints(lint_list.clone().into_iter());
168
169     let usable_lints: Vec<Lint> = Lint::usable_lints(lint_list.clone().into_iter()).collect();
170     let usable_lint_count = usable_lints.len();
171
172     let mut sorted_usable_lints = usable_lints.clone();
173     sorted_usable_lints.sort_by_key(|lint| lint.name.clone());
174
175     let mut file_change = replace_region_in_file(
176         Path::new("src/lintlist/mod.rs"),
177         "begin lint list",
178         "end lint list",
179         false,
180         update_mode == UpdateMode::Change,
181         || {
182             format!(
183                 "pub const ALL_LINTS: [Lint; {}] = {:#?};",
184                 sorted_usable_lints.len(),
185                 sorted_usable_lints
186             )
187             .lines()
188             .map(ToString::to_string)
189             .collect::<Vec<_>>()
190         },
191     )
192     .changed;
193
194     file_change |= replace_region_in_file(
195         Path::new("README.md"),
196         &format!(r#"\[There are \d+ lints included in this crate!\]\({}\)"#, DOCS_LINK),
197         "",
198         true,
199         update_mode == UpdateMode::Change,
200         || {
201             vec![format!(
202                 "[There are {} lints included in this crate!]({})",
203                 usable_lint_count, DOCS_LINK
204             )]
205         },
206     )
207     .changed;
208
209     file_change |= replace_region_in_file(
210         Path::new("CHANGELOG.md"),
211         "<!-- begin autogenerated links to lint list -->",
212         "<!-- end autogenerated links to lint list -->",
213         false,
214         update_mode == UpdateMode::Change,
215         || gen_changelog_lint_list(lint_list.clone()),
216     )
217     .changed;
218
219     file_change |= replace_region_in_file(
220         Path::new("clippy_lints/src/lib.rs"),
221         "begin deprecated lints",
222         "end deprecated lints",
223         false,
224         update_mode == UpdateMode::Change,
225         || gen_deprecated(&lint_list),
226     )
227     .changed;
228
229     file_change |= replace_region_in_file(
230         Path::new("clippy_lints/src/lib.rs"),
231         "begin register lints",
232         "end register lints",
233         false,
234         update_mode == UpdateMode::Change,
235         || gen_register_lint_list(&lint_list),
236     )
237     .changed;
238
239     file_change |= replace_region_in_file(
240         Path::new("clippy_lints/src/lib.rs"),
241         "begin lints modules",
242         "end lints modules",
243         false,
244         update_mode == UpdateMode::Change,
245         || gen_modules_list(lint_list.clone()),
246     )
247     .changed;
248
249     // Generate lists of lints in the clippy::all lint group
250     file_change |= replace_region_in_file(
251         Path::new("clippy_lints/src/lib.rs"),
252         r#"store.register_group\(true, "clippy::all""#,
253         r#"\]\);"#,
254         false,
255         update_mode == UpdateMode::Change,
256         || {
257             // clippy::all should only include the following lint groups:
258             let all_group_lints = usable_lints
259                 .clone()
260                 .into_iter()
261                 .filter(|l| {
262                     l.group == "correctness" || l.group == "style" || l.group == "complexity" || l.group == "perf"
263                 })
264                 .collect();
265
266             gen_lint_group_list(all_group_lints)
267         },
268     )
269     .changed;
270
271     // Generate the list of lints for all other lint groups
272     for (lint_group, lints) in Lint::by_lint_group(usable_lints.into_iter().chain(internal_lints)) {
273         file_change |= replace_region_in_file(
274             Path::new("clippy_lints/src/lib.rs"),
275             &format!("store.register_group\\(true, \"clippy::{}\"", lint_group),
276             r#"\]\);"#,
277             false,
278             update_mode == UpdateMode::Change,
279             || gen_lint_group_list(lints.clone()),
280         )
281         .changed;
282     }
283
284     if update_mode == UpdateMode::Check && file_change {
285         println!(
286             "Not all lints defined properly. \
287              Please run `cargo dev update_lints` to make sure all lints are defined properly."
288         );
289         std::process::exit(1);
290     }
291 }