]> git.lizzy.rs Git - rust.git/blob - clippy_dev/src/update_lints.rs
clippy: Remove now obsolete lintlist module
[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::path::Path;
6
7 #[derive(Clone, Copy, PartialEq)]
8 pub enum UpdateMode {
9     Check,
10     Change,
11 }
12
13 #[allow(clippy::too_many_lines)]
14 pub fn run(update_mode: UpdateMode) {
15     let lint_list: Vec<Lint> = gather_all().collect();
16
17     let internal_lints = Lint::internal_lints(&lint_list);
18     let deprecated_lints = Lint::deprecated_lints(&lint_list);
19     let usable_lints = Lint::usable_lints(&lint_list);
20     let mut sorted_usable_lints = usable_lints.clone();
21     sorted_usable_lints.sort_by_key(|lint| lint.name.clone());
22
23     let usable_lint_count = round_to_fifty(usable_lints.len());
24
25     let mut file_change = false;
26
27     file_change |= replace_region_in_file(
28         Path::new("README.md"),
29         &format!(
30             r#"\[There are over \d+ lints included in this crate!\]\({}\)"#,
31             DOCS_LINK
32         ),
33         "",
34         true,
35         update_mode == UpdateMode::Change,
36         || {
37             vec![format!(
38                 "[There are over {} lints included in this crate!]({})",
39                 usable_lint_count, DOCS_LINK
40             )]
41         },
42     )
43     .changed;
44
45     file_change |= replace_region_in_file(
46         Path::new("CHANGELOG.md"),
47         "<!-- begin autogenerated links to lint list -->",
48         "<!-- end autogenerated links to lint list -->",
49         false,
50         update_mode == UpdateMode::Change,
51         || gen_changelog_lint_list(usable_lints.iter().chain(deprecated_lints.iter())),
52     )
53     .changed;
54
55     file_change |= replace_region_in_file(
56         Path::new("clippy_lints/src/lib.rs"),
57         "begin deprecated lints",
58         "end deprecated lints",
59         false,
60         update_mode == UpdateMode::Change,
61         || gen_deprecated(deprecated_lints.iter()),
62     )
63     .changed;
64
65     file_change |= replace_region_in_file(
66         Path::new("clippy_lints/src/lib.rs"),
67         "begin register lints",
68         "end register lints",
69         false,
70         update_mode == UpdateMode::Change,
71         || gen_register_lint_list(usable_lints.iter().chain(internal_lints.iter())),
72     )
73     .changed;
74
75     file_change |= replace_region_in_file(
76         Path::new("clippy_lints/src/lib.rs"),
77         "begin lints modules",
78         "end lints modules",
79         false,
80         update_mode == UpdateMode::Change,
81         || gen_modules_list(usable_lints.iter()),
82     )
83     .changed;
84
85     // Generate lists of lints in the clippy::all lint group
86     file_change |= replace_region_in_file(
87         Path::new("clippy_lints/src/lib.rs"),
88         r#"store.register_group\(true, "clippy::all""#,
89         r#"\]\);"#,
90         false,
91         update_mode == UpdateMode::Change,
92         || {
93             // clippy::all should only include the following lint groups:
94             let all_group_lints = usable_lints.iter().filter(|l| {
95                 l.group == "correctness" || l.group == "style" || l.group == "complexity" || l.group == "perf"
96             });
97
98             gen_lint_group_list(all_group_lints)
99         },
100     )
101     .changed;
102
103     // Generate the list of lints for all other lint groups
104     for (lint_group, lints) in Lint::by_lint_group(usable_lints.into_iter().chain(internal_lints)) {
105         file_change |= replace_region_in_file(
106             Path::new("clippy_lints/src/lib.rs"),
107             &format!("store.register_group\\(true, \"clippy::{}\"", lint_group),
108             r#"\]\);"#,
109             false,
110             update_mode == UpdateMode::Change,
111             || gen_lint_group_list(lints.iter()),
112         )
113         .changed;
114     }
115
116     if update_mode == UpdateMode::Check && file_change {
117         println!(
118             "Not all lints defined properly. \
119              Please run `cargo dev update_lints` to make sure all lints are defined properly."
120         );
121         std::process::exit(1);
122     }
123 }
124
125 pub fn print_lints() {
126     let lint_list: Vec<Lint> = gather_all().collect();
127     let usable_lints = Lint::usable_lints(&lint_list);
128     let usable_lint_count = usable_lints.len();
129     let grouped_by_lint_group = Lint::by_lint_group(usable_lints.into_iter());
130
131     for (lint_group, mut lints) in grouped_by_lint_group {
132         if lint_group == "Deprecated" {
133             continue;
134         }
135         println!("\n## {}", lint_group);
136
137         lints.sort_by_key(|l| l.name.clone());
138
139         for lint in lints {
140             println!("* [{}]({}#{}) ({})", lint.name, DOCS_LINK, lint.name, lint.desc);
141         }
142     }
143
144     println!("there are {} lints", usable_lint_count);
145 }
146
147 fn round_to_fifty(count: usize) -> usize {
148     count / 50 * 50
149 }