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 lint_count = usable_lints.len();
139 let grouped_by_lint_group = Lint::by_lint_group(&usable_lints);
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", 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 usable_lints: Vec<Lint> = Lint::usable_lints(lint_list.clone().into_iter()).collect();
168 let lint_count = usable_lints.len();
170 let mut sorted_usable_lints = usable_lints.clone();
171 sorted_usable_lints.sort_by_key(|lint| lint.name.clone());
173 let mut file_change = replace_region_in_file(
174 Path::new("src/lintlist/mod.rs"),
178 update_mode == UpdateMode::Change,
181 "pub const ALL_LINTS: [Lint; {}] = {:#?};",
182 sorted_usable_lints.len(),
186 .map(ToString::to_string)
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),
197 update_mode == UpdateMode::Change,
200 "[There are {} lints included in this crate!]({})",
201 lint_count, DOCS_LINK
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 -->",
212 update_mode == UpdateMode::Change,
213 || gen_changelog_lint_list(lint_list.clone()),
217 file_change |= replace_region_in_file(
218 Path::new("clippy_lints/src/lib.rs"),
219 "begin deprecated lints",
220 "end deprecated lints",
222 update_mode == UpdateMode::Change,
223 || gen_deprecated(&lint_list),
227 file_change |= replace_region_in_file(
228 Path::new("clippy_lints/src/lib.rs"),
229 "begin register lints",
230 "end register lints",
232 update_mode == UpdateMode::Change,
233 || gen_register_lint_list(&lint_list),
237 file_change |= replace_region_in_file(
238 Path::new("clippy_lints/src/lib.rs"),
239 "begin lints modules",
242 update_mode == UpdateMode::Change,
243 || gen_modules_list(lint_list.clone()),
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""#,
253 update_mode == UpdateMode::Change,
255 // clippy::all should only include the following lint groups:
256 let all_group_lints = usable_lints
260 l.group == "correctness" || l.group == "style" || l.group == "complexity" || l.group == "perf"
264 gen_lint_group_list(all_group_lints)
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),
276 update_mode == UpdateMode::Change,
277 || gen_lint_group_list(lints.clone()),
282 if update_mode == UpdateMode::Check && file_change {
284 "Not all lints defined properly. \
285 Please run `cargo dev update_lints` to make sure all lints are defined properly."
287 std::process::exit(1);