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 {
70 "clippy_lints/src/lib.register_lints.rs",
72 &gen_register_lint_list(internal_lints.iter(), usable_lints.iter()),
75 "clippy_lints/src/lib.deprecated.rs",
77 &gen_deprecated(deprecated_lints.iter()),
80 "clippy_lints/src/lib.mods.rs",
82 &gen_modules_list(usable_lints.iter()),
85 let all_group_lints = usable_lints.iter().filter(|l| {
88 "correctness" | "suspicious" | "style" | "complexity" | "perf"
91 let content = gen_lint_group_list("all", all_group_lints);
92 process_file("clippy_lints/src/lib.register_all.rs", update_mode, &content);
94 for (lint_group, lints) in Lint::by_lint_group(usable_lints.into_iter().chain(internal_lints)) {
95 let content = gen_lint_group_list(&lint_group, lints.iter());
97 &format!("clippy_lints/src/lib.register_{}.rs", lint_group),
104 pub fn print_lints() {
105 let lint_list: Vec<Lint> = gather_all().collect();
106 let usable_lints = Lint::usable_lints(&lint_list);
107 let usable_lint_count = usable_lints.len();
108 let grouped_by_lint_group = Lint::by_lint_group(usable_lints.into_iter());
110 for (lint_group, mut lints) in grouped_by_lint_group {
111 if lint_group == "Deprecated" {
114 println!("\n## {}", lint_group);
116 lints.sort_by_key(|l| l.name.clone());
119 println!("* [{}]({}#{}) ({})", lint.name, DOCS_LINK, lint.name, lint.desc);
123 println!("there are {} lints", usable_lint_count);
126 fn round_to_fifty(count: usize) -> usize {
130 fn process_file(path: impl AsRef<Path>, update_mode: UpdateMode, content: &str) {
131 if update_mode == UpdateMode::Check {
133 fs::read_to_string(&path).unwrap_or_else(|e| panic!("Cannot read from {}: {}", path.as_ref().display(), e));
134 if content != old_content {
138 fs::write(&path, content.as_bytes())
139 .unwrap_or_else(|e| panic!("Cannot write to {}: {}", path.as_ref().display(), e));
143 fn exit_with_failure() {
145 "Not all lints defined properly. \
146 Please run `cargo dev update_lints` to make sure all lints are defined properly."
148 std::process::exit(1);