1 #![cfg_attr(feature = "deny-warnings", deny(warnings))]
2 // warn on lints, that are included in `rust-lang/rust`s bootstrap
3 #![warn(rust_2018_idioms, unused_lifetimes)]
5 use rustc_tools_util::VersionInfo;
7 use std::ffi::OsString;
8 use std::path::PathBuf;
9 use std::process::{self, Command};
11 const CARGO_CLIPPY_HELP: &str = r#"Checks a package to catch common mistakes and improve your Rust code.
14 cargo clippy [options] [--] [<opts>...]
17 -h, --help Print this message
18 -V, --version Print version info and exit
20 Other options are the same as `cargo check`.
22 To allow or deny a lint from the command line you can use `cargo clippy --`
25 -W --warn OPT Set lint warnings
26 -A --allow OPT Set lint allowed
27 -D --deny OPT Set lint denied
28 -F --forbid OPT Set lint forbidden
30 You can use tool lints to allow or deny lints from your code, eg.:
32 #[allow(clippy::needless_lifetimes)]
36 println!("{}", CARGO_CLIPPY_HELP);
40 let version_info = rustc_tools_util::get_version_info!();
41 println!("{}", version_info);
45 // Check for version and help flags even when invoked as 'cargo-clippy'
46 if env::args().any(|a| a == "--help" || a == "-h") {
51 if env::args().any(|a| a == "--version" || a == "-V") {
56 if let Err(code) = process(env::args().skip(2)) {
62 cargo_subcommand: &'static str,
64 clippy_args: Vec<String>,
68 fn new<I>(mut old_args: I) -> Self
70 I: Iterator<Item = String>,
72 let mut cargo_subcommand = "check";
73 let mut unstable_options = false;
74 let mut args = vec![];
76 for arg in old_args.by_ref() {
79 cargo_subcommand = "fix";
83 // Cover -Zunstable-options and -Z unstable-options
84 s if s.ends_with("unstable-options") => unstable_options = true,
91 if cargo_subcommand == "fix" && !unstable_options {
92 panic!("Usage of `--fix` requires `-Z unstable-options`");
95 let mut clippy_args: Vec<String> = old_args.collect();
96 if cargo_subcommand == "fix" && !clippy_args.iter().any(|arg| arg == "--no-deps") {
97 clippy_args.push("--no-deps".into());
107 fn path() -> PathBuf {
108 let mut path = env::current_exe()
109 .expect("current executable path invalid")
110 .with_file_name("clippy-driver");
113 path.set_extension("exe");
119 fn target_dir() -> Option<(&'static str, OsString)> {
120 env::var_os("CLIPPY_DOGFOOD")
122 env::var_os("CARGO_MANIFEST_DIR").map_or_else(
123 || std::ffi::OsString::from("clippy_dogfood"),
125 std::path::PathBuf::from(d)
132 .map(|p| ("CARGO_TARGET_DIR", p))
135 fn into_std_cmd(self) -> Command {
136 let mut cmd = Command::new("cargo");
137 let clippy_args: String = self
140 .map(|arg| format!("{}__CLIPPY_HACKERY__", arg))
143 cmd.env("RUSTC_WORKSPACE_WRAPPER", Self::path())
144 .envs(ClippyCmd::target_dir())
145 .env("CLIPPY_ARGS", clippy_args)
146 .arg(self.cargo_subcommand)
153 fn process<I>(old_args: I) -> Result<(), i32>
155 I: Iterator<Item = String>,
157 let cmd = ClippyCmd::new(old_args);
159 let mut cmd = cmd.into_std_cmd();
161 let exit_status = cmd
163 .expect("could not run cargo")
165 .expect("failed to wait for cargo?");
167 if exit_status.success() {
170 Err(exit_status.code().unwrap_or(-1))
176 use super::ClippyCmd;
180 fn fix_without_unstable() {
181 let args = "cargo clippy --fix".split_whitespace().map(ToString::to_string);
182 ClippyCmd::new(args);
187 let args = "cargo clippy --fix -Zunstable-options"
189 .map(ToString::to_string);
190 let cmd = ClippyCmd::new(args);
191 assert_eq!("fix", cmd.cargo_subcommand);
192 assert!(cmd.args.iter().any(|arg| arg.ends_with("unstable-options")));
196 fn fix_implies_no_deps() {
197 let args = "cargo clippy --fix -Zunstable-options"
199 .map(ToString::to_string);
200 let cmd = ClippyCmd::new(args);
201 assert!(cmd.clippy_args.iter().any(|arg| arg == "--no-deps"));
205 fn no_deps_not_duplicated_with_fix() {
206 let args = "cargo clippy --fix -Zunstable-options -- --no-deps"
208 .map(ToString::to_string);
209 let cmd = ClippyCmd::new(args);
210 assert_eq!(cmd.clippy_args.iter().filter(|arg| *arg == "--no-deps").count(), 1);
215 let args = "cargo clippy".split_whitespace().map(ToString::to_string);
216 let cmd = ClippyCmd::new(args);
217 assert_eq!("check", cmd.cargo_subcommand);