]> git.lizzy.rs Git - rust.git/blob - src/main.rs
Auto merge of #3869 - taiki-e:use_self, r=flip1995
[rust.git] / src / main.rs
1 use rustc_tools_util::*;
2
3 const CARGO_CLIPPY_HELP: &str = r#"Checks a package to catch common mistakes and improve your Rust code.
4
5 Usage:
6     cargo clippy [options] [--] [<opts>...]
7
8 Common options:
9     -h, --help               Print this message
10     -V, --version            Print version info and exit
11
12 Other options are the same as `cargo check`.
13
14 To allow or deny a lint from the command line you can use `cargo clippy --`
15 with:
16
17     -W --warn OPT       Set lint warnings
18     -A --allow OPT      Set lint allowed
19     -D --deny OPT       Set lint denied
20     -F --forbid OPT     Set lint forbidden
21
22 You can use tool lints to allow or deny lints from your code, eg.:
23
24     #[allow(clippy::needless_lifetimes)]
25 "#;
26
27 fn show_help() {
28     println!("{}", CARGO_CLIPPY_HELP);
29 }
30
31 fn show_version() {
32     let version_info = rustc_tools_util::get_version_info!();
33     println!("{}", version_info);
34 }
35
36 pub fn main() {
37     // Check for version and help flags even when invoked as 'cargo-clippy'
38     if std::env::args().any(|a| a == "--help" || a == "-h") {
39         show_help();
40         return;
41     }
42
43     if std::env::args().any(|a| a == "--version" || a == "-V") {
44         show_version();
45         return;
46     }
47
48     if let Err(code) = process(std::env::args().skip(2)) {
49         std::process::exit(code);
50     }
51 }
52
53 fn process<I>(mut old_args: I) -> Result<(), i32>
54 where
55     I: Iterator<Item = String>,
56 {
57     let mut args = vec!["check".to_owned()];
58
59     for arg in old_args.by_ref() {
60         if arg == "--" {
61             break;
62         }
63         args.push(arg);
64     }
65
66     let clippy_args: String = old_args.map(|arg| format!("{}__CLIPPY_HACKERY__", arg)).collect();
67
68     let mut path = std::env::current_exe()
69         .expect("current executable path invalid")
70         .with_file_name("clippy-driver");
71     if cfg!(windows) {
72         path.set_extension("exe");
73     }
74
75     let target_dir = std::env::var_os("CLIPPY_DOGFOOD")
76         .map(|_| {
77             std::env::var_os("CARGO_MANIFEST_DIR").map_or_else(
78                 || std::ffi::OsString::from("clippy_dogfood"),
79                 |d| {
80                     std::path::PathBuf::from(d)
81                         .join("target")
82                         .join("dogfood")
83                         .into_os_string()
84                 },
85             )
86         })
87         .map(|p| ("CARGO_TARGET_DIR", p));
88
89     // Run the dogfood tests directly on nightly cargo. This is required due
90     // to a bug in rustup.rs when running cargo on custom toolchains. See issue #3118.
91     if std::env::var_os("CLIPPY_DOGFOOD").is_some() && cfg!(windows) {
92         args.insert(0, "+nightly".to_string());
93     }
94
95     let exit_status = std::process::Command::new("cargo")
96         .args(&args)
97         .env("RUSTC_WRAPPER", path)
98         .env("CLIPPY_ARGS", clippy_args)
99         .envs(target_dir)
100         .spawn()
101         .expect("could not run cargo")
102         .wait()
103         .expect("failed to wait for cargo?");
104
105     if exit_status.success() {
106         Ok(())
107     } else {
108         Err(exit_status.code().unwrap_or(-1))
109     }
110 }