]> git.lizzy.rs Git - rust.git/blob - clippy_dev/src/main.rs
Remove all copyright license headers
[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
8 #[derive(PartialEq)]
9 enum UpdateMode {
10     Check,
11     Change,
12 }
13
14 fn main() {
15     let matches = App::new("Clippy developer tooling")
16         .subcommand(
17             SubCommand::with_name("update_lints")
18                 .about(
19                     "Makes sure that:\n \
20                      * the lint count in README.md is correct\n \
21                      * the changelog contains markdown link references at the bottom\n \
22                      * all lint groups include the correct lints\n \
23                      * lint modules in `clippy_lints/*` are visible in `src/lib.rs` via `pub mod`\n \
24                      * all lints are registered in the lint store",
25                 )
26                 .arg(Arg::with_name("print-only").long("print-only").help(
27                     "Print a table of lints to STDOUT. \
28                      This does not include deprecated and internal lints. \
29                      (Does not modify any files)",
30                 ))
31                 .arg(
32                     Arg::with_name("check")
33                         .long("check")
34                         .help("Checks that util/dev update_lints has been run. Used on CI."),
35                 ),
36         )
37         .get_matches();
38
39     if let Some(matches) = matches.subcommand_matches("update_lints") {
40         if matches.is_present("print-only") {
41             print_lints();
42         } else if matches.is_present("check") {
43             update_lints(&UpdateMode::Check);
44         } else {
45             update_lints(&UpdateMode::Change);
46         }
47     }
48 }
49
50 fn print_lints() {
51     let lint_list = gather_all();
52     let usable_lints: Vec<Lint> = Lint::usable_lints(lint_list).collect();
53     let lint_count = usable_lints.len();
54     let grouped_by_lint_group = Lint::by_lint_group(&usable_lints);
55
56     for (lint_group, mut lints) in grouped_by_lint_group {
57         if lint_group == "Deprecated" {
58             continue;
59         }
60         println!("\n## {}", lint_group);
61
62         lints.sort_by_key(|l| l.name.clone());
63
64         for lint in lints {
65             println!(
66                 "* [{}]({}#{}) ({})",
67                 lint.name,
68                 clippy_dev::DOCS_LINK.clone(),
69                 lint.name,
70                 lint.desc
71             );
72         }
73     }
74
75     println!("there are {} lints", lint_count);
76 }
77
78 fn update_lints(update_mode: &UpdateMode) {
79     let lint_list: Vec<Lint> = gather_all().collect();
80     let usable_lints: Vec<Lint> = Lint::usable_lints(lint_list.clone().into_iter()).collect();
81     let lint_count = usable_lints.len();
82
83     let mut file_change = replace_region_in_file(
84         "../README.md",
85         r#"\[There are \d+ lints included in this crate!\]\(https://rust-lang.github.io/rust-clippy/master/index.html\)"#,
86         "",
87         true,
88         update_mode == &UpdateMode::Change,
89         || {
90             vec![
91                 format!("[There are {} lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)", lint_count)
92             ]
93         }
94     ).changed;
95
96     file_change |= replace_region_in_file(
97         "../CHANGELOG.md",
98         "<!-- begin autogenerated links to lint list -->",
99         "<!-- end autogenerated links to lint list -->",
100         false,
101         update_mode == &UpdateMode::Change,
102         || gen_changelog_lint_list(lint_list.clone()),
103     )
104     .changed;
105
106     file_change |= replace_region_in_file(
107         "../clippy_lints/src/lib.rs",
108         "begin deprecated lints",
109         "end deprecated lints",
110         false,
111         update_mode == &UpdateMode::Change,
112         || gen_deprecated(&lint_list),
113     )
114     .changed;
115
116     file_change |= replace_region_in_file(
117         "../clippy_lints/src/lib.rs",
118         "begin lints modules",
119         "end lints modules",
120         false,
121         update_mode == &UpdateMode::Change,
122         || gen_modules_list(lint_list.clone()),
123     )
124     .changed;
125
126     // Generate lists of lints in the clippy::all lint group
127     file_change |= replace_region_in_file(
128         "../clippy_lints/src/lib.rs",
129         r#"reg.register_lint_group\("clippy::all""#,
130         r#"\]\);"#,
131         false,
132         update_mode == &UpdateMode::Change,
133         || {
134             // clippy::all should only include the following lint groups:
135             let all_group_lints = usable_lints
136                 .clone()
137                 .into_iter()
138                 .filter(|l| {
139                     l.group == "correctness" || l.group == "style" || l.group == "complexity" || l.group == "perf"
140                 })
141                 .collect();
142
143             gen_lint_group_list(all_group_lints)
144         },
145     )
146     .changed;
147
148     // Generate the list of lints for all other lint groups
149     for (lint_group, lints) in Lint::by_lint_group(&usable_lints) {
150         file_change |= replace_region_in_file(
151             "../clippy_lints/src/lib.rs",
152             &format!("reg.register_lint_group\\(\"clippy::{}\"", lint_group),
153             r#"\]\);"#,
154             false,
155             update_mode == &UpdateMode::Change,
156             || gen_lint_group_list(lints.clone()),
157         )
158         .changed;
159     }
160
161     if update_mode == &UpdateMode::Check && file_change {
162         println!(
163             "Not all lints defined properly. \
164              Please run `util/dev update_lints` to make sure all lints are defined properly."
165         );
166         std::process::exit(1);
167     }
168 }