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