]> git.lizzy.rs Git - rust.git/blob - clippy_dev/src/update_lints.rs
Fix lint register code format
[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     process_file(
70         "clippy_lints/src/lib.register_lints.rs",
71         update_mode,
72         &gen_register_lint_list(internal_lints.iter(), usable_lints.iter()),
73     );
74     process_file(
75         "clippy_lints/src/lib.deprecated.rs",
76         update_mode,
77         &gen_deprecated(deprecated_lints.iter()),
78     );
79     process_file(
80         "clippy_lints/src/lib.mods.rs",
81         update_mode,
82         &gen_modules_list(usable_lints.iter()),
83     );
84
85     let all_group_lints = usable_lints.iter().filter(|l| {
86         matches!(
87             &*l.group,
88             "correctness" | "suspicious" | "style" | "complexity" | "perf"
89         )
90     });
91     let content = gen_lint_group_list("all", all_group_lints);
92     process_file("clippy_lints/src/lib.register_all.rs", update_mode, &content);
93
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());
96         process_file(
97             &format!("clippy_lints/src/lib.register_{}.rs", lint_group),
98             update_mode,
99             &content,
100         );
101     }
102 }
103
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());
109
110     for (lint_group, mut lints) in grouped_by_lint_group {
111         if lint_group == "Deprecated" {
112             continue;
113         }
114         println!("\n## {}", lint_group);
115
116         lints.sort_by_key(|l| l.name.clone());
117
118         for lint in lints {
119             println!("* [{}]({}#{}) ({})", lint.name, DOCS_LINK, lint.name, lint.desc);
120         }
121     }
122
123     println!("there are {} lints", usable_lint_count);
124 }
125
126 fn round_to_fifty(count: usize) -> usize {
127     count / 50 * 50
128 }
129
130 fn process_file(path: impl AsRef<Path>, update_mode: UpdateMode, content: &str) {
131     if update_mode == UpdateMode::Check {
132         let old_content =
133             fs::read_to_string(&path).unwrap_or_else(|e| panic!("Cannot read from {}: {}", path.as_ref().display(), e));
134         if content != old_content {
135             exit_with_failure();
136         }
137     } else {
138         fs::write(&path, content.as_bytes())
139             .unwrap_or_else(|e| panic!("Cannot write to {}: {}", path.as_ref().display(), e));
140     }
141 }
142
143 fn exit_with_failure() {
144     println!(
145         "Not all lints defined properly. \
146                  Please run `cargo dev update_lints` to make sure all lints are defined properly."
147     );
148     std::process::exit(1);
149 }