]> git.lizzy.rs Git - rust.git/blob - src/main.rs
Merge pull request #3465 from flip1995/rustfmt
[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 The feature `cargo-clippy` is automatically defined for convenience. You can use
37 it to allow or deny lints from the code, eg.:
38
39     #[cfg_attr(feature = "cargo-clippy", allow(needless_lifetimes))]
40 "#;
41
42 fn show_help() {
43     println!("{}", CARGO_CLIPPY_HELP);
44 }
45
46 fn show_version() {
47     let version_info = rustc_tools_util::get_version_info!();
48     println!("{}", version_info);
49 }
50
51 pub fn main() {
52     // Check for version and help flags even when invoked as 'cargo-clippy'
53     if std::env::args().any(|a| a == "--help" || a == "-h") {
54         show_help();
55         return;
56     }
57
58     if std::env::args().any(|a| a == "--version" || a == "-V") {
59         show_version();
60         return;
61     }
62
63     if let Err(code) = process(std::env::args().skip(2)) {
64         std::process::exit(code);
65     }
66 }
67
68 fn process<I>(mut old_args: I) -> Result<(), i32>
69 where
70     I: Iterator<Item = String>,
71 {
72     let mut args = vec!["check".to_owned()];
73
74     let mut found_dashes = false;
75     for arg in old_args.by_ref() {
76         found_dashes |= arg == "--";
77         if found_dashes {
78             break;
79         }
80         args.push(arg);
81     }
82
83     let clippy_args: String = old_args.map(|arg| format!("{}__CLIPPY_HACKERY__", arg)).collect();
84
85     let mut path = std::env::current_exe()
86         .expect("current executable path invalid")
87         .with_file_name("clippy-driver");
88     if cfg!(windows) {
89         path.set_extension("exe");
90     }
91
92     let target_dir = std::env::var_os("CLIPPY_DOGFOOD")
93         .map(|_| {
94             std::env::var_os("CARGO_MANIFEST_DIR").map_or_else(
95                 || {
96                     let mut fallback = std::ffi::OsString::new();
97                     fallback.push("clippy_dogfood");
98                     fallback
99                 },
100                 |d| {
101                     std::path::PathBuf::from(d)
102                         .join("target")
103                         .join("dogfood")
104                         .into_os_string()
105                 },
106             )
107         })
108         .map(|p| ("CARGO_TARGET_DIR", p));
109
110     let exit_status = std::process::Command::new("cargo")
111         .args(&args)
112         .env("RUSTC_WRAPPER", path)
113         .env("CLIPPY_ARGS", clippy_args)
114         .envs(target_dir)
115         .spawn()
116         .expect("could not run cargo")
117         .wait()
118         .expect("failed to wait for cargo?");
119
120     if exit_status.success() {
121         Ok(())
122     } else {
123         Err(exit_status.code().unwrap_or(-1))
124     }
125 }