]> git.lizzy.rs Git - rust.git/blob - src/main.rs
Merge pull request #2625 from mikerite/clippy_warning
[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 target_dir = std::env::var_os("CLIPPY_DOGFOOD")
82         .map(|_| {
83             std::env::var_os("CARGO_MANIFEST_DIR").map_or_else(
84                 || {
85                     let mut fallback = std::ffi::OsString::new();
86                     fallback.push("clippy_dogfood");
87                     fallback
88                 },
89                 |d| {
90                     std::path::PathBuf::from(d)
91                         .join("target")
92                         .join("dogfood")
93                         .into_os_string()
94                 },
95             )
96         })
97         .map(|p| ("CARGO_TARGET_DIR", p));
98
99     let exit_status = std::process::Command::new("cargo")
100         .args(&args)
101         .env("RUSTC_WRAPPER", path)
102         .env("CLIPPY_ARGS", clippy_args)
103         .envs(target_dir)
104         .spawn()
105         .expect("could not run cargo")
106         .wait()
107         .expect("failed to wait for cargo?");
108
109     if exit_status.success() {
110         Ok(())
111     } else {
112         Err(exit_status.code().unwrap_or(-1))
113     }
114 }