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