]> git.lizzy.rs Git - rust.git/blob - src/main.rs
Auto merge of #3577 - daxpedda:master, r=flip1995
[rust.git] / src / main.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 // error-pattern:yummy
11 #![feature(box_syntax)]
12 #![feature(rustc_private)]
13 #![allow(clippy::missing_docs_in_private_items)]
14
15 use rustc_tools_util::*;
16
17 const CARGO_CLIPPY_HELP: &str = r#"Checks a package to catch common mistakes and improve your Rust code.
18
19 Usage:
20     cargo clippy [options] [--] [<opts>...]
21
22 Common options:
23     -h, --help               Print this message
24     -V, --version            Print version info and exit
25
26 Other options are the same as `cargo check`.
27
28 To allow or deny a lint from the command line you can use `cargo clippy --`
29 with:
30
31     -W --warn OPT       Set lint warnings
32     -A --allow OPT      Set lint allowed
33     -D --deny OPT       Set lint denied
34     -F --forbid OPT     Set lint forbidden
35
36 You can use tool lints to allow or deny lints from your code, eg.:
37
38     #[allow(clippy::needless_lifetimes)]
39 "#;
40
41 fn show_help() {
42     println!("{}", CARGO_CLIPPY_HELP);
43 }
44
45 fn show_version() {
46     let version_info = rustc_tools_util::get_version_info!();
47     println!("{}", version_info);
48 }
49
50 pub fn main() {
51     // Check for version and help flags even when invoked as 'cargo-clippy'
52     if std::env::args().any(|a| a == "--help" || a == "-h") {
53         show_help();
54         return;
55     }
56
57     if std::env::args().any(|a| a == "--version" || a == "-V") {
58         show_version();
59         return;
60     }
61
62     if let Err(code) = process(std::env::args().skip(2)) {
63         std::process::exit(code);
64     }
65 }
66
67 fn process<I>(mut old_args: I) -> Result<(), i32>
68 where
69     I: Iterator<Item = String>,
70 {
71     let mut args = vec!["check".to_owned()];
72
73     let mut found_dashes = false;
74     for arg in old_args.by_ref() {
75         found_dashes |= arg == "--";
76         if found_dashes {
77             break;
78         }
79         args.push(arg);
80     }
81
82     let clippy_args: String = old_args.map(|arg| format!("{}__CLIPPY_HACKERY__", arg)).collect();
83
84     let mut path = std::env::current_exe()
85         .expect("current executable path invalid")
86         .with_file_name("clippy-driver");
87     if cfg!(windows) {
88         path.set_extension("exe");
89     }
90
91     let target_dir = std::env::var_os("CLIPPY_DOGFOOD")
92         .map(|_| {
93             std::env::var_os("CARGO_MANIFEST_DIR").map_or_else(
94                 || {
95                     let mut fallback = std::ffi::OsString::new();
96                     fallback.push("clippy_dogfood");
97                     fallback
98                 },
99                 |d| {
100                     std::path::PathBuf::from(d)
101                         .join("target")
102                         .join("dogfood")
103                         .into_os_string()
104                 },
105             )
106         })
107         .map(|p| ("CARGO_TARGET_DIR", p));
108
109     let exit_status = std::process::Command::new("cargo")
110         .args(&args)
111         .env("RUSTC_WRAPPER", path)
112         .env("CLIPPY_ARGS", clippy_args)
113         .envs(target_dir)
114         .spawn()
115         .expect("could not run cargo")
116         .wait()
117         .expect("failed to wait for cargo?");
118
119     if exit_status.success() {
120         Ok(())
121     } else {
122         Err(exit_status.code().unwrap_or(-1))
123     }
124 }