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