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