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