1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
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.
11 extern crate clippy_dev;
14 use clap::{App, Arg, SubCommand};
24 let matches = App::new("Clippy developer tooling")
26 SubCommand::with_name("update_lints")
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",
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)",
41 Arg::with_name("check")
43 .help("Checks that util/dev update_lints has been run. Used on CI."),
48 if let Some(matches) = matches.subcommand_matches("update_lints") {
49 if matches.is_present("print-only") {
51 } else if matches.is_present("check") {
52 update_lints(&UpdateMode::Check);
54 update_lints(&UpdateMode::Change);
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);
65 for (lint_group, mut lints) in grouped_by_lint_group {
66 if lint_group == "Deprecated" {
69 println!("\n## {}", lint_group);
71 lints.sort_by_key(|l| l.name.clone());
77 clippy_dev::DOCS_LINK.clone(),
84 println!("there are {} lints", lint_count);
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();
92 let mut file_change = replace_region_in_file(
94 r#"\[There are \d+ lints included in this crate!\]\(https://rust-lang.github.io/rust-clippy/master/index.html\)"#,
97 update_mode == &UpdateMode::Change,
100 format!("[There are {} lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)", lint_count)
105 file_change |= replace_region_in_file(
107 "<!-- begin autogenerated links to lint list -->",
108 "<!-- end autogenerated links to lint list -->",
110 update_mode == &UpdateMode::Change,
111 || gen_changelog_lint_list(lint_list.clone()),
115 file_change |= replace_region_in_file(
116 "../clippy_lints/src/lib.rs",
117 "begin deprecated lints",
118 "end deprecated lints",
120 update_mode == &UpdateMode::Change,
121 || gen_deprecated(&lint_list),
125 file_change |= replace_region_in_file(
126 "../clippy_lints/src/lib.rs",
127 "begin lints modules",
130 update_mode == &UpdateMode::Change,
131 || gen_modules_list(lint_list.clone()),
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""#,
141 update_mode == &UpdateMode::Change,
143 // clippy::all should only include the following lint groups:
144 let all_group_lints = usable_lints
148 l.group == "correctness" || l.group == "style" || l.group == "complexity" || l.group == "perf"
152 gen_lint_group_list(all_group_lints)
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),
164 update_mode == &UpdateMode::Change,
165 || gen_lint_group_list(lints.clone()),
170 if update_mode == &UpdateMode::Check && file_change {
172 "Not all lints defined properly. \
173 Please run `util/dev update_lints` to make sure all lints are defined properly."
175 std::process::exit(1);