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