]> git.lizzy.rs Git - rust.git/blob - clippy_dev/src/main.rs
Auto merge of #9295 - Guilherme-Vasconcelos:manual-empty-string-creation, r=dswij
[rust.git] / clippy_dev / src / main.rs
1 #![cfg_attr(feature = "deny-warnings", deny(warnings))]
2 // warn on lints, that are included in `rust-lang/rust`s bootstrap
3 #![warn(rust_2018_idioms, unused_lifetimes)]
4
5 use clap::{Arg, ArgAction, ArgMatches, Command, PossibleValue};
6 use clippy_dev::{bless, dogfood, fmt, lint, new_lint, serve, setup, update_lints};
7 use indoc::indoc;
8
9 fn main() {
10     let matches = get_clap_config();
11
12     match matches.subcommand() {
13         Some(("bless", matches)) => {
14             bless::bless(matches.contains_id("ignore-timestamp"));
15         },
16         Some(("dogfood", matches)) => {
17             dogfood::dogfood(
18                 matches.contains_id("fix"),
19                 matches.contains_id("allow-dirty"),
20                 matches.contains_id("allow-staged"),
21             );
22         },
23         Some(("fmt", matches)) => {
24             fmt::run(matches.contains_id("check"), matches.contains_id("verbose"));
25         },
26         Some(("update_lints", matches)) => {
27             if matches.contains_id("print-only") {
28                 update_lints::print_lints();
29             } else if matches.contains_id("check") {
30                 update_lints::update(update_lints::UpdateMode::Check);
31             } else {
32                 update_lints::update(update_lints::UpdateMode::Change);
33             }
34         },
35         Some(("new_lint", matches)) => {
36             match new_lint::create(
37                 matches.get_one::<String>("pass"),
38                 matches.get_one::<String>("name"),
39                 matches.get_one::<String>("category").map(String::as_str),
40                 matches.get_one::<String>("type").map(String::as_str),
41                 matches.contains_id("msrv"),
42             ) {
43                 Ok(_) => update_lints::update(update_lints::UpdateMode::Change),
44                 Err(e) => eprintln!("Unable to create lint: {}", e),
45             }
46         },
47         Some(("setup", sub_command)) => match sub_command.subcommand() {
48             Some(("intellij", matches)) => {
49                 if matches.contains_id("remove") {
50                     setup::intellij::remove_rustc_src();
51                 } else {
52                     setup::intellij::setup_rustc_src(
53                         matches
54                             .get_one::<String>("rustc-repo-path")
55                             .expect("this field is mandatory and therefore always valid"),
56                     );
57                 }
58             },
59             Some(("git-hook", matches)) => {
60                 if matches.contains_id("remove") {
61                     setup::git_hook::remove_hook();
62                 } else {
63                     setup::git_hook::install_hook(matches.contains_id("force-override"));
64                 }
65             },
66             Some(("vscode-tasks", matches)) => {
67                 if matches.contains_id("remove") {
68                     setup::vscode::remove_tasks();
69                 } else {
70                     setup::vscode::install_tasks(matches.contains_id("force-override"));
71                 }
72             },
73             _ => {},
74         },
75         Some(("remove", sub_command)) => match sub_command.subcommand() {
76             Some(("git-hook", _)) => setup::git_hook::remove_hook(),
77             Some(("intellij", _)) => setup::intellij::remove_rustc_src(),
78             Some(("vscode-tasks", _)) => setup::vscode::remove_tasks(),
79             _ => {},
80         },
81         Some(("serve", matches)) => {
82             let port = *matches.get_one::<u16>("port").unwrap();
83             let lint = matches.get_one::<String>("lint");
84             serve::run(port, lint);
85         },
86         Some(("lint", matches)) => {
87             let path = matches.get_one::<String>("path").unwrap();
88             let args = matches.get_many::<String>("args").into_iter().flatten();
89             lint::run(path, args);
90         },
91         Some(("rename_lint", matches)) => {
92             let old_name = matches.get_one::<String>("old_name").unwrap();
93             let new_name = matches.get_one::<String>("new_name").unwrap_or(old_name);
94             let uplift = matches.contains_id("uplift");
95             update_lints::rename(old_name, new_name, uplift);
96         },
97         Some(("deprecate", matches)) => {
98             let name = matches.get_one::<String>("name").unwrap();
99             let reason = matches.get_one("reason");
100             update_lints::deprecate(name, reason);
101         },
102         _ => {},
103     }
104 }
105
106 fn get_clap_config() -> ArgMatches {
107     Command::new("Clippy developer tooling")
108         .arg_required_else_help(true)
109         .subcommands([
110             Command::new("bless").about("bless the test output changes").arg(
111                 Arg::new("ignore-timestamp")
112                     .long("ignore-timestamp")
113                     .help("Include files updated before clippy was built"),
114             ),
115             Command::new("dogfood").about("Runs the dogfood test").args([
116                 Arg::new("fix").long("fix").help("Apply the suggestions when possible"),
117                 Arg::new("allow-dirty")
118                     .long("allow-dirty")
119                     .help("Fix code even if the working directory has changes")
120                     .requires("fix"),
121                 Arg::new("allow-staged")
122                     .long("allow-staged")
123                     .help("Fix code even if the working directory has staged changes")
124                     .requires("fix"),
125             ]),
126             Command::new("fmt")
127                 .about("Run rustfmt on all projects and tests")
128                 .args([
129                     Arg::new("check").long("check").help("Use the rustfmt --check option"),
130                     Arg::new("verbose").short('v').long("verbose").help("Echo commands run"),
131                 ]),
132             Command::new("update_lints")
133                 .about("Updates lint registration and information from the source code")
134                 .long_about(
135                     "Makes sure that:\n \
136                     * the lint count in README.md is correct\n \
137                     * the changelog contains markdown link references at the bottom\n \
138                     * all lint groups include the correct lints\n \
139                     * lint modules in `clippy_lints/*` are visible in `src/lib.rs` via `pub mod`\n \
140                     * all lints are registered in the lint store",
141                 )
142                 .args([
143                     Arg::new("print-only").long("print-only").help(
144                         "Print a table of lints to STDOUT. \
145                         This does not include deprecated and internal lints. \
146                         (Does not modify any files)",
147                     ),
148                     Arg::new("check")
149                         .long("check")
150                         .help("Checks that `cargo dev update_lints` has been run. Used on CI."),
151                 ]),
152             Command::new("new_lint")
153                 .about("Create new lint and run `cargo dev update_lints`")
154                 .args([
155                     Arg::new("pass")
156                         .short('p')
157                         .long("pass")
158                         .help("Specify whether the lint runs during the early or late pass")
159                         .takes_value(true)
160                         .value_parser([PossibleValue::new("early"), PossibleValue::new("late")])
161                         .conflicts_with("type")
162                         .required_unless_present("type"),
163                     Arg::new("name")
164                         .short('n')
165                         .long("name")
166                         .help("Name of the new lint in snake case, ex: fn_too_long")
167                         .takes_value(true)
168                         .required(true),
169                     Arg::new("category")
170                         .short('c')
171                         .long("category")
172                         .help("What category the lint belongs to")
173                         .default_value("nursery")
174                         .value_parser([
175                             PossibleValue::new("style"),
176                             PossibleValue::new("correctness"),
177                             PossibleValue::new("suspicious"),
178                             PossibleValue::new("complexity"),
179                             PossibleValue::new("perf"),
180                             PossibleValue::new("pedantic"),
181                             PossibleValue::new("restriction"),
182                             PossibleValue::new("cargo"),
183                             PossibleValue::new("nursery"),
184                             PossibleValue::new("internal"),
185                             PossibleValue::new("internal_warn"),
186                         ])
187                         .takes_value(true),
188                     Arg::new("type")
189                         .long("type")
190                         .help("What directory the lint belongs in")
191                         .takes_value(true)
192                         .required(false),
193                     Arg::new("msrv").long("msrv").help("Add MSRV config code to the lint"),
194                 ]),
195             Command::new("setup")
196                 .about("Support for setting up your personal development environment")
197                 .arg_required_else_help(true)
198                 .subcommands([
199                     Command::new("intellij")
200                         .about("Alter dependencies so Intellij Rust can find rustc internals")
201                         .args([
202                             Arg::new("remove")
203                                 .long("remove")
204                                 .help("Remove the dependencies added with 'cargo dev setup intellij'")
205                                 .required(false),
206                             Arg::new("rustc-repo-path")
207                                 .long("repo-path")
208                                 .short('r')
209                                 .help("The path to a rustc repo that will be used for setting the dependencies")
210                                 .takes_value(true)
211                                 .value_name("path")
212                                 .conflicts_with("remove")
213                                 .required(true),
214                         ]),
215                     Command::new("git-hook")
216                         .about("Add a pre-commit git hook that formats your code to make it look pretty")
217                         .args([
218                             Arg::new("remove")
219                                 .long("remove")
220                                 .help("Remove the pre-commit hook added with 'cargo dev setup git-hook'")
221                                 .required(false),
222                             Arg::new("force-override")
223                                 .long("force-override")
224                                 .short('f')
225                                 .help("Forces the override of an existing git pre-commit hook")
226                                 .required(false),
227                         ]),
228                     Command::new("vscode-tasks")
229                         .about("Add several tasks to vscode for formatting, validation and testing")
230                         .args([
231                             Arg::new("remove")
232                                 .long("remove")
233                                 .help("Remove the tasks added with 'cargo dev setup vscode-tasks'")
234                                 .required(false),
235                             Arg::new("force-override")
236                                 .long("force-override")
237                                 .short('f')
238                                 .help("Forces the override of existing vscode tasks")
239                                 .required(false),
240                         ]),
241                 ]),
242             Command::new("remove")
243                 .about("Support for undoing changes done by the setup command")
244                 .arg_required_else_help(true)
245                 .subcommands([
246                     Command::new("git-hook").about("Remove any existing pre-commit git hook"),
247                     Command::new("vscode-tasks").about("Remove any existing vscode tasks"),
248                     Command::new("intellij").about("Removes rustc source paths added via `cargo dev setup intellij`"),
249                 ]),
250             Command::new("serve")
251                 .about("Launch a local 'ALL the Clippy Lints' website in a browser")
252                 .args([
253                     Arg::new("port")
254                         .long("port")
255                         .short('p')
256                         .help("Local port for the http server")
257                         .default_value("8000")
258                         .value_parser(clap::value_parser!(u16)),
259                     Arg::new("lint").help("Which lint's page to load initially (optional)"),
260                 ]),
261             Command::new("lint")
262                 .about("Manually run clippy on a file or package")
263                 .after_help(indoc! {"
264                     EXAMPLES
265                         Lint a single file:
266                             cargo dev lint tests/ui/attrs.rs
267
268                         Lint a package directory:
269                             cargo dev lint tests/ui-cargo/wildcard_dependencies/fail
270                             cargo dev lint ~/my-project
271
272                         Run rustfix:
273                             cargo dev lint ~/my-project -- --fix
274
275                         Set lint levels:
276                             cargo dev lint file.rs -- -W clippy::pedantic
277                             cargo dev lint ~/my-project -- -- -W clippy::pedantic
278                 "})
279                 .args([
280                     Arg::new("path")
281                         .required(true)
282                         .help("The path to a file or package directory to lint"),
283                     Arg::new("args")
284                         .action(ArgAction::Append)
285                         .help("Pass extra arguments to cargo/clippy-driver"),
286                 ]),
287             Command::new("rename_lint").about("Renames the given lint").args([
288                 Arg::new("old_name")
289                     .index(1)
290                     .required(true)
291                     .help("The name of the lint to rename"),
292                 Arg::new("new_name")
293                     .index(2)
294                     .required_unless_present("uplift")
295                     .help("The new name of the lint"),
296                 Arg::new("uplift")
297                     .long("uplift")
298                     .help("This lint will be uplifted into rustc"),
299             ]),
300             Command::new("deprecate").about("Deprecates the given lint").args([
301                 Arg::new("name")
302                     .index(1)
303                     .required(true)
304                     .help("The name of the lint to deprecate"),
305                 Arg::new("reason")
306                     .long("reason")
307                     .short('r')
308                     .required(false)
309                     .takes_value(true)
310                     .help("The reason for deprecation"),
311             ]),
312         ])
313         .get_matches()
314 }