]> git.lizzy.rs Git - rust.git/blob - clippy_dev/src/main.rs
Deprecate util/dev in favor of cargo alias
[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(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.clone(),
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         r#"\[There are \d+ lints included in this crate!\]\(https://rust-lang.github.io/rust-clippy/master/index.html\)"#,
195         "",
196         true,
197         update_mode == &UpdateMode::Change,
198         || {
199             vec![
200                 format!("[There are {} lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)", lint_count)
201             ]
202         }
203     ).changed;
204
205     file_change |= replace_region_in_file(
206         Path::new("CHANGELOG.md"),
207         "<!-- begin autogenerated links to lint list -->",
208         "<!-- end autogenerated links to lint list -->",
209         false,
210         update_mode == &UpdateMode::Change,
211         || gen_changelog_lint_list(lint_list.clone()),
212     )
213     .changed;
214
215     file_change |= replace_region_in_file(
216         Path::new("clippy_lints/src/lib.rs"),
217         "begin deprecated lints",
218         "end deprecated lints",
219         false,
220         update_mode == &UpdateMode::Change,
221         || gen_deprecated(&lint_list),
222     )
223     .changed;
224
225     file_change |= replace_region_in_file(
226         Path::new("clippy_lints/src/lib.rs"),
227         "begin register lints",
228         "end register lints",
229         false,
230         update_mode == &UpdateMode::Change,
231         || gen_register_lint_list(&lint_list),
232     )
233     .changed;
234
235     file_change |= replace_region_in_file(
236         Path::new("clippy_lints/src/lib.rs"),
237         "begin lints modules",
238         "end lints modules",
239         false,
240         update_mode == &UpdateMode::Change,
241         || gen_modules_list(lint_list.clone()),
242     )
243     .changed;
244
245     // Generate lists of lints in the clippy::all lint group
246     file_change |= replace_region_in_file(
247         Path::new("clippy_lints/src/lib.rs"),
248         r#"store.register_group\(true, "clippy::all""#,
249         r#"\]\);"#,
250         false,
251         update_mode == &UpdateMode::Change,
252         || {
253             // clippy::all should only include the following lint groups:
254             let all_group_lints = usable_lints
255                 .clone()
256                 .into_iter()
257                 .filter(|l| {
258                     l.group == "correctness" || l.group == "style" || l.group == "complexity" || l.group == "perf"
259                 })
260                 .collect();
261
262             gen_lint_group_list(all_group_lints)
263         },
264     )
265     .changed;
266
267     // Generate the list of lints for all other lint groups
268     for (lint_group, lints) in Lint::by_lint_group(&usable_lints) {
269         file_change |= replace_region_in_file(
270             Path::new("clippy_lints/src/lib.rs"),
271             &format!("store.register_group\\(true, \"clippy::{}\"", lint_group),
272             r#"\]\);"#,
273             false,
274             update_mode == &UpdateMode::Change,
275             || gen_lint_group_list(lints.clone()),
276         )
277         .changed;
278     }
279
280     if update_mode == &UpdateMode::Check && file_change {
281         println!(
282             "Not all lints defined properly. \
283              Please run `cargo dev update_lints` to make sure all lints are defined properly."
284         );
285         std::process::exit(1);
286     }
287 }