2 gather_all, gen_changelog_lint_list, gen_deprecated, gen_lint_group_list, gen_modules_list, gen_register_lint_list,
3 replace_region_in_file, Lint, DOCS_LINK,
8 #[derive(Clone, Copy, PartialEq)]
14 /// Runs the `update_lints` command.
16 /// This updates various generated values from the lint source code.
18 /// `update_mode` indicates if the files should be updated or if updates should be checked for.
22 /// Panics if a file path could not read from or then written to
23 #[allow(clippy::too_many_lines)]
24 pub fn run(update_mode: UpdateMode) {
25 let lint_list: Vec<Lint> = gather_all().collect();
27 let internal_lints = Lint::internal_lints(&lint_list);
28 let deprecated_lints = Lint::deprecated_lints(&lint_list);
29 let usable_lints = Lint::usable_lints(&lint_list);
30 let mut sorted_usable_lints = usable_lints.clone();
31 sorted_usable_lints.sort_by_key(|lint| lint.name.clone());
33 let usable_lint_count = round_to_fifty(usable_lints.len());
35 let mut file_change = false;
37 file_change |= replace_region_in_file(
38 Path::new("README.md"),
40 r#"\[There are over \d+ lints included in this crate!\]\({}\)"#,
45 update_mode == UpdateMode::Change,
48 "[There are over {} lints included in this crate!]({})",
49 usable_lint_count, DOCS_LINK
55 file_change |= replace_region_in_file(
56 Path::new("CHANGELOG.md"),
57 "<!-- begin autogenerated links to lint list -->",
58 "<!-- end autogenerated links to lint list -->",
60 update_mode == UpdateMode::Change,
61 || gen_changelog_lint_list(usable_lints.iter().chain(deprecated_lints.iter())),
65 if file_change && update_mode == UpdateMode::Check {
69 for (name, lines) in [
70 ("mods", gen_modules_list(usable_lints.iter())),
71 ("deprecated", gen_deprecated(deprecated_lints.iter())),
74 gen_register_lint_list(internal_lints.iter(), usable_lints.iter()),
77 let all_group_lints = usable_lints.iter().filter(|l| {
80 "correctness" | "suspicious" | "style" | "complexity" | "perf"
84 gen_lint_group_list("all", all_group_lints)
87 process_file(&format!("clippy_lints/src/lib.{}.rs", name), update_mode, &lines[..]);
90 for (lint_group, lints) in Lint::by_lint_group(usable_lints.into_iter().chain(internal_lints)) {
92 &format!("clippy_lints/src/lib.register_{}.rs", lint_group),
94 &gen_lint_group_list(&lints.get(0).expect("group non-empty").group, lints.iter())[..],
99 pub fn print_lints() {
100 let lint_list: Vec<Lint> = gather_all().collect();
101 let usable_lints = Lint::usable_lints(&lint_list);
102 let usable_lint_count = usable_lints.len();
103 let grouped_by_lint_group = Lint::by_lint_group(usable_lints.into_iter());
105 for (lint_group, mut lints) in grouped_by_lint_group {
106 if lint_group == "Deprecated" {
109 println!("\n## {}", lint_group);
111 lints.sort_by_key(|l| l.name.clone());
114 println!("* [{}]({}#{}) ({})", lint.name, DOCS_LINK, lint.name, lint.desc);
118 println!("there are {} lints", usable_lint_count);
121 fn round_to_fifty(count: usize) -> usize {
125 fn process_file(path: impl AsRef<Path>, update_mode: UpdateMode, new_lines: &[String]) {
126 let mut new_content = "// This file was generated by `cargo dev update_lints`.\n\
127 // Use that command to update this file and do not edit by hand.\n\
128 // Manual edits will be overwritten.\n\n"
130 new_content.push_str(&new_lines.join("\n"));
132 if update_mode == UpdateMode::Check {
134 fs::read_to_string(&path).unwrap_or_else(|e| panic!("Cannot read from {}: {}", path.as_ref().display(), e));
135 if new_content != old_content {
139 fs::write(&path, new_content.as_bytes())
140 .unwrap_or_else(|e| panic!("Cannot write to {}: {}", path.as_ref().display(), e));
144 fn exit_with_failure() {
146 "Not all lints defined properly. \
147 Please run `cargo dev update_lints` to make sure all lints are defined properly."
149 std::process::exit(1);