2019-11-25 17:23:48 +01:00
|
|
|
#![cfg_attr(feature = "deny-warnings", deny(warnings))]
|
|
|
|
|
|
2019-05-16 07:31:56 +02:00
|
|
|
use clap::{App, Arg, SubCommand};
|
2020-02-21 09:39:38 +01:00
|
|
|
use clippy_dev::{
|
|
|
|
|
gather_all, gen_changelog_lint_list, gen_deprecated, gen_lint_group_list, gen_modules_list, gen_register_lint_list,
|
|
|
|
|
replace_region_in_file, Lint, DOCS_LINK,
|
|
|
|
|
};
|
2020-01-30 08:33:48 +01:00
|
|
|
use std::path::Path;
|
2019-06-25 06:43:38 +02:00
|
|
|
|
|
|
|
|
mod fmt;
|
2019-12-31 18:07:39 -07:00
|
|
|
mod new_lint;
|
2019-05-16 07:31:56 +02:00
|
|
|
mod stderr_length_check;
|
2018-07-17 22:50:17 +02:00
|
|
|
|
2020-02-06 21:28:50 +07:00
|
|
|
#[derive(Clone, Copy, PartialEq)]
|
2018-11-04 09:41:28 +01:00
|
|
|
enum UpdateMode {
|
|
|
|
|
Check,
|
2018-11-27 21:13:08 +01:00
|
|
|
Change,
|
2018-11-04 09:41:28 +01:00
|
|
|
}
|
|
|
|
|
|
2018-07-17 22:50:17 +02:00
|
|
|
fn main() {
|
|
|
|
|
let matches = App::new("Clippy developer tooling")
|
2019-06-25 06:43:38 +02:00
|
|
|
.subcommand(
|
|
|
|
|
SubCommand::with_name("fmt")
|
|
|
|
|
.about("Run rustfmt on all projects and tests")
|
|
|
|
|
.arg(
|
|
|
|
|
Arg::with_name("check")
|
|
|
|
|
.long("check")
|
|
|
|
|
.help("Use the rustfmt --check option"),
|
|
|
|
|
)
|
|
|
|
|
.arg(
|
|
|
|
|
Arg::with_name("verbose")
|
|
|
|
|
.short("v")
|
|
|
|
|
.long("verbose")
|
|
|
|
|
.help("Echo commands run"),
|
|
|
|
|
),
|
|
|
|
|
)
|
2018-07-17 22:50:17 +02:00
|
|
|
.subcommand(
|
|
|
|
|
SubCommand::with_name("update_lints")
|
2019-03-08 07:45:31 +02:00
|
|
|
.about("Updates lint registration and information from the source code")
|
|
|
|
|
.long_about(
|
2018-11-27 21:13:08 +01:00
|
|
|
"Makes sure that:\n \
|
|
|
|
|
* the lint count in README.md is correct\n \
|
|
|
|
|
* the changelog contains markdown link references at the bottom\n \
|
|
|
|
|
* all lint groups include the correct lints\n \
|
|
|
|
|
* lint modules in `clippy_lints/*` are visible in `src/lib.rs` via `pub mod`\n \
|
|
|
|
|
* all lints are registered in the lint store",
|
2018-11-04 09:41:28 +01:00
|
|
|
)
|
2018-11-27 21:13:08 +01:00
|
|
|
.arg(Arg::with_name("print-only").long("print-only").help(
|
|
|
|
|
"Print a table of lints to STDOUT. \
|
|
|
|
|
This does not include deprecated and internal lints. \
|
|
|
|
|
(Does not modify any files)",
|
|
|
|
|
))
|
2018-11-04 09:41:28 +01:00
|
|
|
.arg(
|
|
|
|
|
Arg::with_name("check")
|
|
|
|
|
.long("check")
|
2020-01-30 08:33:48 +01:00
|
|
|
.help("Checks that `cargo dev update_lints` has been run. Used on CI."),
|
2018-11-27 21:13:08 +01:00
|
|
|
),
|
|
|
|
|
)
|
2019-12-31 18:07:39 -07:00
|
|
|
.subcommand(
|
|
|
|
|
SubCommand::with_name("new_lint")
|
2020-01-30 08:33:48 +01:00
|
|
|
.about("Create new lint and run `cargo dev update_lints`")
|
2019-12-31 18:07:39 -07:00
|
|
|
.arg(
|
|
|
|
|
Arg::with_name("pass")
|
|
|
|
|
.short("p")
|
|
|
|
|
.long("pass")
|
|
|
|
|
.help("Specify whether the lint runs during the early or late pass")
|
|
|
|
|
.takes_value(true)
|
|
|
|
|
.possible_values(&["early", "late"])
|
|
|
|
|
.required(true),
|
|
|
|
|
)
|
|
|
|
|
.arg(
|
|
|
|
|
Arg::with_name("name")
|
|
|
|
|
.short("n")
|
|
|
|
|
.long("name")
|
|
|
|
|
.help("Name of the new lint in snake case, ex: fn_too_long")
|
|
|
|
|
.takes_value(true)
|
|
|
|
|
.required(true),
|
|
|
|
|
)
|
|
|
|
|
.arg(
|
|
|
|
|
Arg::with_name("category")
|
|
|
|
|
.short("c")
|
|
|
|
|
.long("category")
|
|
|
|
|
.help("What category the lint belongs to")
|
|
|
|
|
.default_value("nursery")
|
|
|
|
|
.possible_values(&[
|
|
|
|
|
"style",
|
|
|
|
|
"correctness",
|
|
|
|
|
"complexity",
|
|
|
|
|
"perf",
|
|
|
|
|
"pedantic",
|
|
|
|
|
"restriction",
|
|
|
|
|
"cargo",
|
|
|
|
|
"nursery",
|
|
|
|
|
"internal",
|
|
|
|
|
"internal_warn",
|
|
|
|
|
])
|
|
|
|
|
.takes_value(true),
|
|
|
|
|
),
|
|
|
|
|
)
|
2019-05-16 07:31:56 +02:00
|
|
|
.arg(
|
|
|
|
|
Arg::with_name("limit-stderr-length")
|
|
|
|
|
.long("limit-stderr-length")
|
|
|
|
|
.help("Ensures that stderr files do not grow longer than a certain amount of lines."),
|
|
|
|
|
)
|
2018-11-27 21:13:08 +01:00
|
|
|
.get_matches();
|
2018-07-17 22:50:17 +02:00
|
|
|
|
2019-05-16 07:31:56 +02:00
|
|
|
if matches.is_present("limit-stderr-length") {
|
|
|
|
|
stderr_length_check::check();
|
|
|
|
|
}
|
2019-06-25 06:43:38 +02:00
|
|
|
|
|
|
|
|
match matches.subcommand() {
|
|
|
|
|
("fmt", Some(matches)) => {
|
|
|
|
|
fmt::run(matches.is_present("check"), matches.is_present("verbose"));
|
2019-10-24 11:55:22 +02:00
|
|
|
},
|
2019-06-25 06:43:38 +02:00
|
|
|
("update_lints", Some(matches)) => {
|
|
|
|
|
if matches.is_present("print-only") {
|
|
|
|
|
print_lints();
|
|
|
|
|
} else if matches.is_present("check") {
|
2020-02-06 21:28:50 +07:00
|
|
|
update_lints(UpdateMode::Check);
|
2019-06-25 06:43:38 +02:00
|
|
|
} else {
|
2020-02-06 21:28:50 +07:00
|
|
|
update_lints(UpdateMode::Change);
|
2019-06-25 06:43:38 +02:00
|
|
|
}
|
2019-10-24 11:55:22 +02:00
|
|
|
},
|
2019-12-31 18:07:39 -07:00
|
|
|
("new_lint", Some(matches)) => {
|
|
|
|
|
match new_lint::create(
|
|
|
|
|
matches.value_of("pass"),
|
|
|
|
|
matches.value_of("name"),
|
|
|
|
|
matches.value_of("category"),
|
|
|
|
|
) {
|
2020-02-06 21:28:50 +07:00
|
|
|
Ok(_) => update_lints(UpdateMode::Change),
|
2019-12-31 18:07:39 -07:00
|
|
|
Err(e) => eprintln!("Unable to create lint: {}", e),
|
|
|
|
|
}
|
|
|
|
|
},
|
2019-10-24 11:55:22 +02:00
|
|
|
_ => {},
|
2018-07-17 22:50:17 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn print_lints() {
|
2018-10-16 07:24:32 +02:00
|
|
|
let lint_list = gather_all();
|
|
|
|
|
let usable_lints: Vec<Lint> = Lint::usable_lints(lint_list).collect();
|
2020-02-12 09:12:19 +01:00
|
|
|
let usable_lint_count = usable_lints.len();
|
2020-02-11 11:07:38 +01:00
|
|
|
let grouped_by_lint_group = Lint::by_lint_group(usable_lints.into_iter());
|
2018-09-02 09:45:13 +02:00
|
|
|
|
|
|
|
|
for (lint_group, mut lints) in grouped_by_lint_group {
|
2018-11-27 21:13:08 +01:00
|
|
|
if lint_group == "Deprecated" {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2018-09-02 09:45:13 +02:00
|
|
|
println!("\n## {}", lint_group);
|
|
|
|
|
|
2018-10-15 21:02:38 +02:00
|
|
|
lints.sort_by_key(|l| l.name.clone());
|
2018-09-02 09:45:13 +02:00
|
|
|
|
|
|
|
|
for lint in lints {
|
2018-11-27 21:13:08 +01:00
|
|
|
println!(
|
|
|
|
|
"* [{}]({}#{}) ({})",
|
|
|
|
|
lint.name,
|
2020-02-07 08:33:01 +07:00
|
|
|
clippy_dev::DOCS_LINK,
|
2018-11-27 21:13:08 +01:00
|
|
|
lint.name,
|
|
|
|
|
lint.desc
|
|
|
|
|
);
|
2018-07-17 22:50:17 +02:00
|
|
|
}
|
|
|
|
|
}
|
2018-09-02 09:45:13 +02:00
|
|
|
|
2020-02-12 09:12:19 +01:00
|
|
|
println!("there are {} lints", usable_lint_count);
|
2018-07-17 22:50:17 +02:00
|
|
|
}
|
2018-10-16 08:00:31 +02:00
|
|
|
|
2019-10-24 14:33:14 +02:00
|
|
|
#[allow(clippy::too_many_lines)]
|
2020-02-06 21:28:50 +07:00
|
|
|
fn update_lints(update_mode: UpdateMode) {
|
2018-10-17 08:18:05 +02:00
|
|
|
let lint_list: Vec<Lint> = gather_all().collect();
|
2019-06-04 17:32:03 -07:00
|
|
|
|
2020-02-11 11:11:43 +01:00
|
|
|
let internal_lints = Lint::internal_lints(lint_list.clone().into_iter());
|
|
|
|
|
|
2019-06-08 11:29:27 -07:00
|
|
|
let usable_lints: Vec<Lint> = Lint::usable_lints(lint_list.clone().into_iter()).collect();
|
2020-02-12 09:12:19 +01:00
|
|
|
let usable_lint_count = usable_lints.len();
|
2019-06-08 11:29:27 -07:00
|
|
|
|
|
|
|
|
let mut sorted_usable_lints = usable_lints.clone();
|
|
|
|
|
sorted_usable_lints.sort_by_key(|lint| lint.name.clone());
|
|
|
|
|
|
2019-06-14 10:36:43 +02:00
|
|
|
let mut file_change = replace_region_in_file(
|
2020-01-30 08:33:48 +01:00
|
|
|
Path::new("src/lintlist/mod.rs"),
|
2019-06-14 10:36:43 +02:00
|
|
|
"begin lint list",
|
|
|
|
|
"end lint list",
|
|
|
|
|
false,
|
2020-02-06 21:28:50 +07:00
|
|
|
update_mode == UpdateMode::Change,
|
2019-06-14 10:36:43 +02:00
|
|
|
|| {
|
|
|
|
|
format!(
|
|
|
|
|
"pub const ALL_LINTS: [Lint; {}] = {:#?};",
|
|
|
|
|
sorted_usable_lints.len(),
|
|
|
|
|
sorted_usable_lints
|
|
|
|
|
)
|
|
|
|
|
.lines()
|
|
|
|
|
.map(ToString::to_string)
|
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
|
},
|
2019-06-04 17:32:03 -07:00
|
|
|
)
|
2019-06-14 10:36:43 +02:00
|
|
|
.changed;
|
2019-06-04 17:32:03 -07:00
|
|
|
|
2019-06-14 10:36:43 +02:00
|
|
|
file_change |= replace_region_in_file(
|
2020-01-30 08:33:48 +01:00
|
|
|
Path::new("README.md"),
|
2020-02-07 08:33:01 +07:00
|
|
|
&format!(r#"\[There are \d+ lints included in this crate!\]\({}\)"#, DOCS_LINK),
|
2018-10-16 08:00:31 +02:00
|
|
|
"",
|
|
|
|
|
true,
|
2020-02-06 21:28:50 +07:00
|
|
|
update_mode == UpdateMode::Change,
|
2018-10-16 08:00:31 +02:00
|
|
|
|| {
|
2020-02-07 08:33:01 +07:00
|
|
|
vec",
|
2020-02-12 09:12:19 +01:00
|
|
|
usable_lint_count, DOCS_LINK
|
2020-02-07 08:33:01 +07:00
|
|
|
)]
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.changed;
|
2018-10-17 08:18:05 +02:00
|
|
|
|
2018-11-04 09:41:28 +01:00
|
|
|
file_change |= replace_region_in_file(
|
2020-01-30 08:33:48 +01:00
|
|
|
Path::new("CHANGELOG.md"),
|
2018-10-17 08:18:05 +02:00
|
|
|
"<!-- begin autogenerated links to lint list -->",
|
|
|
|
|
"<!-- end autogenerated links to lint list -->",
|
|
|
|
|
false,
|
2020-02-06 21:28:50 +07:00
|
|
|
update_mode == UpdateMode::Change,
|
2018-11-27 21:13:08 +01:00
|
|
|
|| gen_changelog_lint_list(lint_list.clone()),
|
|
|
|
|
)
|
|
|
|
|
.changed;
|
2018-10-31 08:03:50 +01:00
|
|
|
|
2018-11-04 09:41:28 +01:00
|
|
|
file_change |= replace_region_in_file(
|
2020-01-30 08:33:48 +01:00
|
|
|
Path::new("clippy_lints/src/lib.rs"),
|
2018-10-31 08:03:50 +01:00
|
|
|
"begin deprecated lints",
|
|
|
|
|
"end deprecated lints",
|
|
|
|
|
false,
|
2020-02-06 21:28:50 +07:00
|
|
|
update_mode == UpdateMode::Change,
|
2018-11-27 21:13:08 +01:00
|
|
|
|| gen_deprecated(&lint_list),
|
|
|
|
|
)
|
|
|
|
|
.changed;
|
2018-11-01 20:16:38 +01:00
|
|
|
|
2019-10-11 08:33:42 -04:00
|
|
|
file_change |= replace_region_in_file(
|
2020-01-30 08:33:48 +01:00
|
|
|
Path::new("clippy_lints/src/lib.rs"),
|
2019-10-11 08:33:42 -04:00
|
|
|
"begin register lints",
|
|
|
|
|
"end register lints",
|
|
|
|
|
false,
|
2020-02-06 21:28:50 +07:00
|
|
|
update_mode == UpdateMode::Change,
|
2019-10-11 08:33:42 -04:00
|
|
|
|| gen_register_lint_list(&lint_list),
|
|
|
|
|
)
|
|
|
|
|
.changed;
|
|
|
|
|
|
2018-11-04 09:41:28 +01:00
|
|
|
file_change |= replace_region_in_file(
|
2020-01-30 08:33:48 +01:00
|
|
|
Path::new("clippy_lints/src/lib.rs"),
|
2018-11-01 20:16:38 +01:00
|
|
|
"begin lints modules",
|
|
|
|
|
"end lints modules",
|
|
|
|
|
false,
|
2020-02-06 21:28:50 +07:00
|
|
|
update_mode == UpdateMode::Change,
|
2018-11-27 21:13:08 +01:00
|
|
|
|| gen_modules_list(lint_list.clone()),
|
|
|
|
|
)
|
|
|
|
|
.changed;
|
2018-11-03 12:59:13 +01:00
|
|
|
|
|
|
|
|
// Generate lists of lints in the clippy::all lint group
|
2018-11-04 09:41:28 +01:00
|
|
|
file_change |= replace_region_in_file(
|
2020-01-30 08:33:48 +01:00
|
|
|
Path::new("clippy_lints/src/lib.rs"),
|
2019-10-11 08:33:42 -04:00
|
|
|
r#"store.register_group\(true, "clippy::all""#,
|
2018-11-03 12:59:13 +01:00
|
|
|
r#"\]\);"#,
|
|
|
|
|
false,
|
2020-02-06 21:28:50 +07:00
|
|
|
update_mode == UpdateMode::Change,
|
2018-11-03 12:59:13 +01:00
|
|
|
|| {
|
|
|
|
|
// clippy::all should only include the following lint groups:
|
2018-11-27 21:13:08 +01:00
|
|
|
let all_group_lints = usable_lints
|
|
|
|
|
.clone()
|
|
|
|
|
.into_iter()
|
|
|
|
|
.filter(|l| {
|
|
|
|
|
l.group == "correctness" || l.group == "style" || l.group == "complexity" || l.group == "perf"
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
2018-11-03 12:59:13 +01:00
|
|
|
|
|
|
|
|
gen_lint_group_list(all_group_lints)
|
2018-11-27 21:13:08 +01:00
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.changed;
|
2018-11-03 12:59:13 +01:00
|
|
|
|
|
|
|
|
// Generate the list of lints for all other lint groups
|
2020-02-11 11:11:43 +01:00
|
|
|
for (lint_group, lints) in Lint::by_lint_group(usable_lints.into_iter().chain(internal_lints)) {
|
2018-11-04 09:41:28 +01:00
|
|
|
file_change |= replace_region_in_file(
|
2020-01-30 08:33:48 +01:00
|
|
|
Path::new("clippy_lints/src/lib.rs"),
|
2019-10-11 08:33:42 -04:00
|
|
|
&format!("store.register_group\\(true, \"clippy::{}\"", lint_group),
|
2018-11-03 12:59:13 +01:00
|
|
|
r#"\]\);"#,
|
|
|
|
|
false,
|
2020-02-06 21:28:50 +07:00
|
|
|
update_mode == UpdateMode::Change,
|
2018-11-27 21:13:08 +01:00
|
|
|
|| gen_lint_group_list(lints.clone()),
|
|
|
|
|
)
|
|
|
|
|
.changed;
|
2018-11-04 09:41:28 +01:00
|
|
|
}
|
|
|
|
|
|
2020-02-06 21:28:50 +07:00
|
|
|
if update_mode == UpdateMode::Check && file_change {
|
2018-11-27 21:13:08 +01:00
|
|
|
println!(
|
|
|
|
|
"Not all lints defined properly. \
|
2020-01-30 08:33:48 +01:00
|
|
|
Please run `cargo dev update_lints` to make sure all lints are defined properly."
|
2018-11-27 21:13:08 +01:00
|
|
|
);
|
|
|
|
|
std::process::exit(1);
|
2018-11-03 12:59:13 +01:00
|
|
|
}
|
2018-10-16 08:00:31 +02:00
|
|
|
}
|