]> git.lizzy.rs Git - rust.git/blob - clippy_dev/src/update_lints.rs
Move code generated by `update_lints` to includes
[rust.git] / clippy_dev / src / update_lints.rs
1 use crate::{
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,
4 };
5 use std::fs;
6 use std::path::Path;
7
8 #[derive(Clone, Copy, PartialEq)]
9 pub enum UpdateMode {
10     Check,
11     Change,
12 }
13
14 /// Runs the `update_lints` command.
15 ///
16 /// This updates various generated values from the lint source code.
17 ///
18 /// `update_mode` indicates if the files should be updated or if updates should be checked for.
19 ///
20 /// # Panics
21 ///
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();
26
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());
32
33     let usable_lint_count = round_to_fifty(usable_lints.len());
34
35     let mut file_change = false;
36
37     file_change |= replace_region_in_file(
38         Path::new("README.md"),
39         &format!(
40             r#"\[There are over \d+ lints included in this crate!\]\({}\)"#,
41             DOCS_LINK
42         ),
43         "",
44         true,
45         update_mode == UpdateMode::Change,
46         || {
47             vec![format!(
48                 "[There are over {} lints included in this crate!]({})",
49                 usable_lint_count, DOCS_LINK
50             )]
51         },
52     )
53     .changed;
54
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 -->",
59         false,
60         update_mode == UpdateMode::Change,
61         || gen_changelog_lint_list(usable_lints.iter().chain(deprecated_lints.iter())),
62     )
63     .changed;
64
65     if file_change && update_mode == UpdateMode::Check {
66         exit_with_failure();
67     }
68
69     for (name, lines) in [
70         ("mods", gen_modules_list(usable_lints.iter())),
71         ("deprecated", gen_deprecated(deprecated_lints.iter())),
72         (
73             "register_lints",
74             gen_register_lint_list(internal_lints.iter(), usable_lints.iter()),
75         ),
76         ("register_all", {
77             let all_group_lints = usable_lints.iter().filter(|l| {
78                 matches!(
79                     &*l.group,
80                     "correctness" | "suspicious" | "style" | "complexity" | "perf"
81                 )
82             });
83
84             gen_lint_group_list("all", all_group_lints)
85         }),
86     ] {
87         process_file(&format!("clippy_lints/src/lib.{}.rs", name), update_mode, &lines[..]);
88     }
89
90     for (lint_group, lints) in Lint::by_lint_group(usable_lints.into_iter().chain(internal_lints)) {
91         process_file(
92             &format!("clippy_lints/src/lib.register_{}.rs", lint_group),
93             update_mode,
94             &gen_lint_group_list(&lints.get(0).expect("group non-empty").group, lints.iter())[..],
95         );
96     }
97 }
98
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());
104
105     for (lint_group, mut lints) in grouped_by_lint_group {
106         if lint_group == "Deprecated" {
107             continue;
108         }
109         println!("\n## {}", lint_group);
110
111         lints.sort_by_key(|l| l.name.clone());
112
113         for lint in lints {
114             println!("* [{}]({}#{}) ({})", lint.name, DOCS_LINK, lint.name, lint.desc);
115         }
116     }
117
118     println!("there are {} lints", usable_lint_count);
119 }
120
121 fn round_to_fifty(count: usize) -> usize {
122     count / 50 * 50
123 }
124
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"
129         .to_string();
130     new_content.push_str(&new_lines.join("\n"));
131
132     if update_mode == UpdateMode::Check {
133         let old_content =
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 {
136             exit_with_failure();
137         }
138     } else {
139         fs::write(&path, new_content.as_bytes())
140             .unwrap_or_else(|e| panic!("Cannot write to {}: {}", path.as_ref().display(), e));
141     }
142 }
143
144 fn exit_with_failure() {
145     println!(
146         "Not all lints defined properly. \
147                  Please run `cargo dev update_lints` to make sure all lints are defined properly."
148     );
149     std::process::exit(1);
150 }