]> git.lizzy.rs Git - rust.git/blob - src/main.rs
Merge branch 'master' into dogfood_target_dir
[rust.git] / src / main.rs
1 // error-pattern:yummy
2 #![feature(box_syntax)]
3 #![feature(rustc_private)]
4 #![allow(unknown_lints, missing_docs_in_private_items)]
5
6 const CARGO_CLIPPY_HELP: &str = r#"Checks a package to catch common mistakes and improve your Rust code.
7
8 Usage:
9     cargo clippy [options] [--] [<opts>...]
10
11 Common options:
12     -h, --help               Print this message
13     -V, --version            Print version info and exit
14
15 Other options are the same as `cargo check`.
16
17 To allow or deny a lint from the command line you can use `cargo clippy --`
18 with:
19
20     -W --warn OPT       Set lint warnings
21     -A --allow OPT      Set lint allowed
22     -D --deny OPT       Set lint denied
23     -F --forbid OPT     Set lint forbidden
24
25 The feature `cargo-clippy` is automatically defined for convenience. You can use
26 it to allow or deny lints from the code, eg.:
27
28     #[cfg_attr(feature = "cargo-clippy", allow(needless_lifetimes))]
29 "#;
30
31 #[allow(print_stdout)]
32 fn show_help() {
33     println!("{}", CARGO_CLIPPY_HELP);
34 }
35
36 #[allow(print_stdout)]
37 fn show_version() {
38     println!("{}", env!("CARGO_PKG_VERSION"));
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     if std::env::args().any(|a| a == "--version" || a == "-V") {
48         show_version();
49         return;
50     }
51
52     if let Err(code) = process(std::env::args().skip(2)) {
53         std::process::exit(code);
54     }
55 }
56
57 fn process<I>(mut old_args: I) -> Result<(), i32>
58 where
59     I: Iterator<Item = String>,
60 {
61     let mut args = vec!["check".to_owned()];
62
63     let mut found_dashes = false;
64     for arg in old_args.by_ref() {
65         found_dashes |= arg == "--";
66         if found_dashes {
67             break;
68         }
69         args.push(arg);
70     }
71
72     let clippy_args: String = old_args.map(|arg| format!("{}__CLIPPY_HACKERY__", arg)).collect();
73
74     let mut path = std::env::current_exe()
75         .expect("current executable path invalid")
76         .with_file_name("clippy-driver");
77     if cfg!(windows) {
78         path.set_extension("exe");
79     }
80
81     let mut extra_envs = vec![];
82     if std::env::var("CLIPPY_DOGFOOD").is_ok() {
83         let target_dir = std::env::var("CARGO_MANIFEST_DIR")
84             .map(|m| {
85                 std::path::PathBuf::from(m)
86                     .join("target")
87                     .join("dogfood")
88                     .to_string_lossy()
89                     .into_owned()
90             })
91             .unwrap_or_else(|_| "clippy_dogfood".to_string());
92
93         extra_envs.push(("CARGO_TARGET_DIR", target_dir));
94     };
95
96     let exit_status = std::process::Command::new("cargo")
97         .args(&args)
98         .env("RUSTC_WRAPPER", path)
99         .env("CLIPPY_ARGS", clippy_args)
100         .envs(extra_envs)
101         .spawn()
102         .expect("could not run cargo")
103         .wait()
104         .expect("failed to wait for cargo?");
105
106     if exit_status.success() {
107         Ok(())
108     } else {
109         Err(exit_status.code().unwrap_or(-1))
110     }
111 }