]> git.lizzy.rs Git - rust.git/blob - clippy_dev/src/main.rs
switch to sorted usable lints
[rust.git] / clippy_dev / src / main.rs
1 extern crate clap;
2 extern crate clippy_dev;
3 extern crate regex;
4
5 use clap::{App, Arg, SubCommand};
6 use clippy_dev::*;
7 mod stderr_length_check;
8
9 #[derive(PartialEq)]
10 enum UpdateMode {
11     Check,
12     Change,
13 }
14
15 fn main() {
16     let matches = App::new("Clippy developer tooling")
17         .subcommand(
18             SubCommand::with_name("update_lints")
19                 .about("Updates lint registration and information from the source code")
20                 .long_about(
21                     "Makes sure that:\n \
22                      * the lint count in README.md is correct\n \
23                      * the changelog contains markdown link references at the bottom\n \
24                      * all lint groups include the correct lints\n \
25                      * lint modules in `clippy_lints/*` are visible in `src/lib.rs` via `pub mod`\n \
26                      * all lints are registered in the lint store",
27                 )
28                 .arg(Arg::with_name("print-only").long("print-only").help(
29                     "Print a table of lints to STDOUT. \
30                      This does not include deprecated and internal lints. \
31                      (Does not modify any files)",
32                 ))
33                 .arg(
34                     Arg::with_name("check")
35                         .long("check")
36                         .help("Checks that util/dev update_lints has been run. Used on CI."),
37                 ),
38         )
39         .arg(
40             Arg::with_name("limit-stderr-length")
41                 .long("limit-stderr-length")
42                 .help("Ensures that stderr files do not grow longer than a certain amount of lines."),
43         )
44         .get_matches();
45
46     if matches.is_present("limit-stderr-length") {
47         stderr_length_check::check();
48     }
49     if let Some(matches) = matches.subcommand_matches("update_lints") {
50         if matches.is_present("print-only") {
51             print_lints();
52         } else if matches.is_present("check") {
53             update_lints(&UpdateMode::Check);
54         } else {
55             update_lints(&UpdateMode::Change);
56         }
57     }
58 }
59
60 fn print_lints() {
61     let lint_list = gather_all();
62     let usable_lints: Vec<Lint> = Lint::usable_lints(lint_list).collect();
63     let lint_count = usable_lints.len();
64     let grouped_by_lint_group = Lint::by_lint_group(&usable_lints);
65
66     for (lint_group, mut lints) in grouped_by_lint_group {
67         if lint_group == "Deprecated" {
68             continue;
69         }
70         println!("\n## {}", lint_group);
71
72         lints.sort_by_key(|l| l.name.clone());
73
74         for lint in lints {
75             println!(
76                 "* [{}]({}#{}) ({})",
77                 lint.name,
78                 clippy_dev::DOCS_LINK.clone(),
79                 lint.name,
80                 lint.desc
81             );
82         }
83     }
84
85     println!("there are {} lints", lint_count);
86 }
87
88 fn update_lints(update_mode: &UpdateMode) {
89     let lint_list: Vec<Lint> = gather_all().collect();
90
91     let usable_lints: Vec<Lint> = Lint::usable_lints(lint_list.clone().into_iter()).collect();
92     let lint_count = usable_lints.len();
93
94     let mut sorted_usable_lints = usable_lints.clone();
95     sorted_usable_lints.sort_by_key(|lint| lint.name.clone());
96
97     std::fs::write(
98         "../src/lintlist.rs",
99         &format!(
100             "\
101 //! This file is managed by util/dev update_lints. Do not edit.
102
103 /// Lint data parsed from the Clippy source code.
104 #[derive(Clone, PartialEq, Debug)]
105 pub struct Lint {{
106     pub name: &'static str,
107     pub group: &'static str,
108     pub desc: &'static str,
109     pub deprecation: Option<&'static str>,
110     pub module: &'static str,
111 }}
112
113 pub const ALL_LINTS: [Lint; {}] = {:#?};\n",
114             sorted_usable_lints.len(),
115             sorted_usable_lints
116         ),
117     )
118     .expect("can write to file");
119
120     let mut file_change = replace_region_in_file(
121         "../README.md",
122         r#"\[There are \d+ lints included in this crate!\]\(https://rust-lang.github.io/rust-clippy/master/index.html\)"#,
123         "",
124         true,
125         update_mode == &UpdateMode::Change,
126         || {
127             vec![
128                 format!("[There are {} lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)", lint_count)
129             ]
130         }
131     ).changed;
132
133     file_change |= replace_region_in_file(
134         "../CHANGELOG.md",
135         "<!-- begin autogenerated links to lint list -->",
136         "<!-- end autogenerated links to lint list -->",
137         false,
138         update_mode == &UpdateMode::Change,
139         || gen_changelog_lint_list(lint_list.clone()),
140     )
141     .changed;
142
143     file_change |= replace_region_in_file(
144         "../clippy_lints/src/lib.rs",
145         "begin deprecated lints",
146         "end deprecated lints",
147         false,
148         update_mode == &UpdateMode::Change,
149         || gen_deprecated(&lint_list),
150     )
151     .changed;
152
153     file_change |= replace_region_in_file(
154         "../clippy_lints/src/lib.rs",
155         "begin lints modules",
156         "end lints modules",
157         false,
158         update_mode == &UpdateMode::Change,
159         || gen_modules_list(lint_list.clone()),
160     )
161     .changed;
162
163     // Generate lists of lints in the clippy::all lint group
164     file_change |= replace_region_in_file(
165         "../clippy_lints/src/lib.rs",
166         r#"reg.register_lint_group\("clippy::all""#,
167         r#"\]\);"#,
168         false,
169         update_mode == &UpdateMode::Change,
170         || {
171             // clippy::all should only include the following lint groups:
172             let all_group_lints = usable_lints
173                 .clone()
174                 .into_iter()
175                 .filter(|l| {
176                     l.group == "correctness" || l.group == "style" || l.group == "complexity" || l.group == "perf"
177                 })
178                 .collect();
179
180             gen_lint_group_list(all_group_lints)
181         },
182     )
183     .changed;
184
185     // Generate the list of lints for all other lint groups
186     for (lint_group, lints) in Lint::by_lint_group(&usable_lints) {
187         file_change |= replace_region_in_file(
188             "../clippy_lints/src/lib.rs",
189             &format!("reg.register_lint_group\\(\"clippy::{}\"", lint_group),
190             r#"\]\);"#,
191             false,
192             update_mode == &UpdateMode::Change,
193             || gen_lint_group_list(lints.clone()),
194         )
195         .changed;
196     }
197
198     if update_mode == &UpdateMode::Check && file_change {
199         println!(
200             "Not all lints defined properly. \
201              Please run `util/dev update_lints` to make sure all lints are defined properly."
202         );
203         std::process::exit(1);
204     }
205 }