1 #![cfg_attr(feature = "deny-warnings", deny(warnings))]
3 use clap::{App, Arg, SubCommand};
9 mod stderr_length_check;
11 #[derive(Clone, Copy, PartialEq)]
18 let matches = App::new("Clippy developer tooling")
20 SubCommand::with_name("fmt")
21 .about("Run rustfmt on all projects and tests")
23 Arg::with_name("check")
25 .help("Use the rustfmt --check option"),
28 Arg::with_name("verbose")
31 .help("Echo commands run"),
35 SubCommand::with_name("update_lints")
36 .about("Updates lint registration and information from the source code")
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",
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)",
51 Arg::with_name("check")
53 .help("Checks that `cargo dev update_lints` has been run. Used on CI."),
57 SubCommand::with_name("new_lint")
58 .about("Create new lint and run `cargo dev update_lints`")
60 Arg::with_name("pass")
63 .help("Specify whether the lint runs during the early or late pass")
65 .possible_values(&["early", "late"])
69 Arg::with_name("name")
72 .help("Name of the new lint in snake case, ex: fn_too_long")
77 Arg::with_name("category")
80 .help("What category the lint belongs to")
81 .default_value("nursery")
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."),
104 if matches.is_present("limit-stderr-length") {
105 stderr_length_check::check();
108 match matches.subcommand() {
109 ("fmt", Some(matches)) => {
110 fmt::run(matches.is_present("check"), matches.is_present("verbose"));
112 ("update_lints", Some(matches)) => {
113 if matches.is_present("print-only") {
115 } else if matches.is_present("check") {
116 update_lints(UpdateMode::Check);
118 update_lints(UpdateMode::Change);
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"),
127 Ok(_) => update_lints(UpdateMode::Change),
128 Err(e) => eprintln!("Unable to create lint: {}", e),
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());
141 for (lint_group, mut lints) in grouped_by_lint_group {
142 if lint_group == "Deprecated" {
145 println!("\n## {}", lint_group);
147 lints.sort_by_key(|l| l.name.clone());
151 "* [{}]({}#{}) ({})",
153 clippy_dev::DOCS_LINK,
160 println!("there are {} lints", usable_lint_count);
163 #[allow(clippy::too_many_lines)]
164 fn update_lints(update_mode: UpdateMode) {
165 let lint_list: Vec<Lint> = gather_all().collect();
167 let internal_lints = Lint::internal_lints(lint_list.clone().into_iter());
169 let usable_lints: Vec<Lint> = Lint::usable_lints(lint_list.clone().into_iter()).collect();
170 let usable_lint_count = usable_lints.len();
172 let mut sorted_usable_lints = usable_lints.clone();
173 sorted_usable_lints.sort_by_key(|lint| lint.name.clone());
175 let mut file_change = replace_region_in_file(
176 Path::new("src/lintlist/mod.rs"),
180 update_mode == UpdateMode::Change,
183 "pub const ALL_LINTS: [Lint; {}] = {:#?};",
184 sorted_usable_lints.len(),
188 .map(ToString::to_string)
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),
199 update_mode == UpdateMode::Change,
202 "[There are {} lints included in this crate!]({})",
203 usable_lint_count, DOCS_LINK
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 -->",
214 update_mode == UpdateMode::Change,
215 || gen_changelog_lint_list(lint_list.clone()),
219 file_change |= replace_region_in_file(
220 Path::new("clippy_lints/src/lib.rs"),
221 "begin deprecated lints",
222 "end deprecated lints",
224 update_mode == UpdateMode::Change,
225 || gen_deprecated(&lint_list),
229 file_change |= replace_region_in_file(
230 Path::new("clippy_lints/src/lib.rs"),
231 "begin register lints",
232 "end register lints",
234 update_mode == UpdateMode::Change,
235 || gen_register_lint_list(&lint_list),
239 file_change |= replace_region_in_file(
240 Path::new("clippy_lints/src/lib.rs"),
241 "begin lints modules",
244 update_mode == UpdateMode::Change,
245 || gen_modules_list(lint_list.clone()),
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""#,
255 update_mode == UpdateMode::Change,
257 // clippy::all should only include the following lint groups:
258 let all_group_lints = usable_lints
262 l.group == "correctness" || l.group == "style" || l.group == "complexity" || l.group == "perf"
266 gen_lint_group_list(all_group_lints)
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),
278 update_mode == UpdateMode::Change,
279 || gen_lint_group_list(lints.clone()),
284 if update_mode == UpdateMode::Check && file_change {
286 "Not all lints defined properly. \
287 Please run `cargo dev update_lints` to make sure all lints are defined properly."
289 std::process::exit(1);