]> git.lizzy.rs Git - rust.git/blob - src/main.rs
split it up for testing but the merge broke tests
[rust.git] / src / main.rs
1 #![cfg_attr(feature = "deny-warnings", deny(warnings))]
2
3 use rustc_tools_util::VersionInfo;
4 use std::env;
5 use std::path::PathBuf;
6 use std::process::{self, Command};
7 use std::ffi::OsString;
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 You can use tool lints to allow or deny lints from your code, eg.:
29
30     #[allow(clippy::needless_lifetimes)]
31 "#;
32
33 fn show_help() {
34     println!("{}", CARGO_CLIPPY_HELP);
35 }
36
37 fn show_version() {
38     let version_info = rustc_tools_util::get_version_info!();
39     println!("{}", version_info);
40 }
41
42 pub fn main() {
43     // Check for version and help flags even when invoked as 'cargo-clippy'
44     if env::args().any(|a| a == "--help" || a == "-h") {
45         show_help();
46         return;
47     }
48
49     if env::args().any(|a| a == "--version" || a == "-V") {
50         show_version();
51         return;
52     }
53
54     if let Err(code) = process(env::args().skip(2)) {
55         process::exit(code);
56     }
57 }
58
59 struct ClippyCmd {
60     unstable_options: bool,
61     cmd: &'static str,
62     args: Vec<String>,
63     clippy_args: String
64 }
65
66 impl ClippyCmd
67 {
68     fn new<I>(mut old_args: I) -> Self
69     where
70         I: Iterator<Item = String>,
71     {
72         let mut cmd = "check";
73         let mut unstable_options = false;
74         let mut args = vec![];
75
76         for arg in old_args.by_ref() {
77             match arg.as_str() {
78                 "--fix" => {
79                     cmd = "fix";
80                     continue;
81                 }
82                 "--" => break,
83                 // Cover -Zunstable-options and -Z unstable-options
84                 s if s.ends_with("unstable-options") => unstable_options = true,
85                 _ => {}
86             }
87
88             args.push(arg);
89         }
90
91         if cmd == "fix" && !unstable_options {
92             panic!("Usage of `--fix` requires `-Z unstable-options`");
93         }
94
95         // Run the dogfood tests directly on nightly cargo. This is required due
96         // to a bug in rustup.rs when running cargo on custom toolchains. See issue #3118.
97         if env::var_os("CLIPPY_DOGFOOD").is_some() && cfg!(windows) {
98             args.insert(0, "+nightly".to_string());
99         }
100
101         let clippy_args: String =
102             old_args
103             .map(|arg| format!("{}__CLIPPY_HACKERY__", arg))
104             .collect();
105
106         ClippyCmd {
107             unstable_options,
108             cmd,
109             args,
110             clippy_args,
111         }
112     }
113
114     fn path_env(&self) -> &'static str {
115         if self.unstable_options {
116             "RUSTC_WORKSPACE_WRAPPER"
117         } else {
118             "RUSTC_WRAPPER"
119         }
120     }
121
122     fn path(&self) -> PathBuf {
123         let mut path = env::current_exe()
124             .expect("current executable path invalid")
125             .with_file_name("clippy-driver");
126
127         if cfg!(windows) {
128             path.set_extension("exe");
129         }
130
131         path
132     }
133
134     fn target_dir() -> Option<(&'static str, OsString)> {
135         env::var_os("CLIPPY_DOGFOOD")
136             .map(|_| {
137                 env::var_os("CARGO_MANIFEST_DIR").map_or_else(
138                     || std::ffi::OsString::from("clippy_dogfood"),
139                     |d| {
140                         std::path::PathBuf::from(d)
141                             .join("target")
142                             .join("dogfood")
143                             .into_os_string()
144                     },
145                 )
146             })
147             .map(|p| ("CARGO_TARGET_DIR", p))
148     }
149
150     fn to_std_cmd(self) -> Command {
151         let mut cmd = Command::new("cargo");
152
153         cmd.env(self.path_env(), self.path())
154             .envs(ClippyCmd::target_dir())
155             .env("CLIPPY_ARGS", self.clippy_args)
156             .arg(self.cmd)
157             .args(&self.args);
158
159         cmd
160     }
161 }
162
163
164 fn process<I>(old_args: I) -> Result<(), i32>
165 where
166     I: Iterator<Item = String>,
167 {
168     let cmd = ClippyCmd::new(old_args);
169
170     let mut cmd = cmd.to_std_cmd();
171
172     let exit_status = cmd
173         .spawn()
174         .expect("could not run cargo")
175         .wait()
176         .expect("failed to wait for cargo?");
177
178     if exit_status.success() {
179         Ok(())
180     } else {
181         Err(exit_status.code().unwrap_or(-1))
182     }
183 }