]> git.lizzy.rs Git - rust.git/blob - src/main.rs
resolved conflicts
[rust.git] / src / main.rs
1 // error-pattern:yummy
2 #![feature(box_syntax)]
3 #![feature(rustc_private)]
4 #![feature(tool_lints)]
5 #![allow(unknown_lints, clippy::missing_docs_in_private_items)]
6
7 const CARGO_CLIPPY_HELP: &str = r#"Checks a package to catch common mistakes and improve your Rust code.
8
9 Usage:
10     cargo clippy [options] [--] [<opts>...]
11
12 Common options:
13     -h, --help               Print this message
14     -V, --version            Print version info and exit
15
16 Other options are the same as `cargo check`.
17
18 To allow or deny a lint from the command line you can use `cargo clippy --`
19 with:
20
21     -W --warn OPT       Set lint warnings
22     -A --allow OPT      Set lint allowed
23     -D --deny OPT       Set lint denied
24     -F --forbid OPT     Set lint forbidden
25
26 The feature `cargo-clippy` is automatically defined for convenience. You can use
27 it to allow or deny lints from the code, eg.:
28
29     #[cfg_attr(feature = "cargo-clippy", allow(needless_lifetimes))]
30 "#;
31
32 #[allow(clippy::print_stdout)]
33 fn show_help() {
34     println!("{}", CARGO_CLIPPY_HELP);
35 }
36
37 #[allow(clippy::print_stdout)]
38 fn show_version() {
39     println!(env!("CARGO_PKG_VERSION"));
40 }
41
42 pub fn main() {
43     // Check for version and help flags even when invoked as 'cargo-clippy'
44     if std::env::args().any(|a| a == "--help" || a == "-h") {
45         show_help();
46         return;
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     let mut found_dashes = false;
65     for arg in old_args.by_ref() {
66         found_dashes |= arg == "--";
67         if found_dashes {
68             break;
69         }
70         args.push(arg);
71     }
72
73     let clippy_args: String = old_args.map(|arg| format!("{}__CLIPPY_HACKERY__", arg)).collect();
74
75     let mut path = std::env::current_exe()
76         .expect("current executable path invalid")
77         .with_file_name("clippy-driver");
78     if cfg!(windows) {
79         path.set_extension("exe");
80     }
81
82     let target_dir = std::env::var_os("CLIPPY_DOGFOOD")
83         .map(|_| {
84             std::env::var_os("CARGO_MANIFEST_DIR").map_or_else(
85                 || {
86                     let mut fallback = std::ffi::OsString::new();
87                     fallback.push("clippy_dogfood");
88                     fallback
89                 },
90                 |d| {
91                     std::path::PathBuf::from(d)
92                         .join("target")
93                         .join("dogfood")
94                         .into_os_string()
95                 },
96             )
97         })
98         .map(|p| ("CARGO_TARGET_DIR", p));
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 }