]> git.lizzy.rs Git - rust.git/blob - clippy_dev/src/main.rs
Rollup merge of #3852 - phansch:refactor_assign_ops, r=flip1995
[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, AppSettings, 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         .setting(AppSettings::SubcommandRequiredElseHelp)
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         .get_matches();
40
41     if let Some(matches) = matches.subcommand_matches("update_lints") {
42         if matches.is_present("print-only") {
43             print_lints();
44         } else if matches.is_present("check") {
45             update_lints(&UpdateMode::Check);
46         } else {
47             update_lints(&UpdateMode::Change);
48         }
49     }
50 }
51
52 fn print_lints() {
53     let lint_list = gather_all();
54     let usable_lints: Vec<Lint> = Lint::usable_lints(lint_list).collect();
55     let lint_count = usable_lints.len();
56     let grouped_by_lint_group = Lint::by_lint_group(&usable_lints);
57
58     for (lint_group, mut lints) in grouped_by_lint_group {
59         if lint_group == "Deprecated" {
60             continue;
61         }
62         println!("\n## {}", lint_group);
63
64         lints.sort_by_key(|l| l.name.clone());
65
66         for lint in lints {
67             println!(
68                 "* [{}]({}#{}) ({})",
69                 lint.name,
70                 clippy_dev::DOCS_LINK.clone(),
71                 lint.name,
72                 lint.desc
73             );
74         }
75     }
76
77     println!("there are {} lints", lint_count);
78 }
79
80 fn update_lints(update_mode: &UpdateMode) {
81     let lint_list: Vec<Lint> = gather_all().collect();
82     let usable_lints: Vec<Lint> = Lint::usable_lints(lint_list.clone().into_iter()).collect();
83     let lint_count = usable_lints.len();
84
85     let mut file_change = replace_region_in_file(
86         "../README.md",
87         r#"\[There are \d+ lints included in this crate!\]\(https://rust-lang.github.io/rust-clippy/master/index.html\)"#,
88         "",
89         true,
90         update_mode == &UpdateMode::Change,
91         || {
92             vec![
93                 format!("[There are {} lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)", lint_count)
94             ]
95         }
96     ).changed;
97
98     file_change |= replace_region_in_file(
99         "../CHANGELOG.md",
100         "<!-- begin autogenerated links to lint list -->",
101         "<!-- end autogenerated links to lint list -->",
102         false,
103         update_mode == &UpdateMode::Change,
104         || gen_changelog_lint_list(lint_list.clone()),
105     )
106     .changed;
107
108     file_change |= replace_region_in_file(
109         "../clippy_lints/src/lib.rs",
110         "begin deprecated lints",
111         "end deprecated lints",
112         false,
113         update_mode == &UpdateMode::Change,
114         || gen_deprecated(&lint_list),
115     )
116     .changed;
117
118     file_change |= replace_region_in_file(
119         "../clippy_lints/src/lib.rs",
120         "begin lints modules",
121         "end lints modules",
122         false,
123         update_mode == &UpdateMode::Change,
124         || gen_modules_list(lint_list.clone()),
125     )
126     .changed;
127
128     // Generate lists of lints in the clippy::all lint group
129     file_change |= replace_region_in_file(
130         "../clippy_lints/src/lib.rs",
131         r#"reg.register_lint_group\("clippy::all""#,
132         r#"\]\);"#,
133         false,
134         update_mode == &UpdateMode::Change,
135         || {
136             // clippy::all should only include the following lint groups:
137             let all_group_lints = usable_lints
138                 .clone()
139                 .into_iter()
140                 .filter(|l| {
141                     l.group == "correctness" || l.group == "style" || l.group == "complexity" || l.group == "perf"
142                 })
143                 .collect();
144
145             gen_lint_group_list(all_group_lints)
146         },
147     )
148     .changed;
149
150     // Generate the list of lints for all other lint groups
151     for (lint_group, lints) in Lint::by_lint_group(&usable_lints) {
152         file_change |= replace_region_in_file(
153             "../clippy_lints/src/lib.rs",
154             &format!("reg.register_lint_group\\(\"clippy::{}\"", lint_group),
155             r#"\]\);"#,
156             false,
157             update_mode == &UpdateMode::Change,
158             || gen_lint_group_list(lints.clone()),
159         )
160         .changed;
161     }
162
163     if update_mode == &UpdateMode::Check && file_change {
164         println!(
165             "Not all lints defined properly. \
166              Please run `util/dev update_lints` to make sure all lints are defined properly."
167         );
168         std::process::exit(1);
169     }
170 }