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::path::PathBuf;
8 use std::process::{self, Command};
10 const CARGO_CLIPPY_HELP: &str = r#"Checks a package to catch common mistakes and improve your Rust code.
13 cargo clippy [options] [--] [<opts>...]
16 --no-deps Run Clippy only on the given crate, without linting the dependencies
17 --fix Automatically apply lint suggestions. This flag implies `--no-deps`
18 -h, --help Print this message
19 -V, --version Print version info and exit
20 --explain LINT Print the documentation for a given lint
22 Other options are the same as `cargo check`.
24 To allow or deny a lint from the command line you can use `cargo clippy --`
27 -W --warn OPT Set lint warnings
28 -A --allow OPT Set lint allowed
29 -D --deny OPT Set lint denied
30 -F --forbid OPT Set lint forbidden
32 You can use tool lints to allow or deny lints from your code, eg.:
34 #[allow(clippy::needless_lifetimes)]
38 println!("{CARGO_CLIPPY_HELP}");
42 let version_info = rustc_tools_util::get_version_info!();
43 println!("{version_info}");
47 // Check for version and help flags even when invoked as 'cargo-clippy'
48 if env::args().any(|a| a == "--help" || a == "-h") {
53 if env::args().any(|a| a == "--version" || a == "-V") {
58 if let Some(pos) = env::args().position(|a| a == "--explain") {
59 if let Some(mut lint) = env::args().nth(pos + 1) {
60 lint.make_ascii_lowercase();
61 clippy_lints::explain(&lint.strip_prefix("clippy::").unwrap_or(&lint).replace('-', "_"));
68 if let Err(code) = process(env::args().skip(2)) {
74 cargo_subcommand: &'static str,
76 clippy_args: Vec<String>,
80 fn new<I>(mut old_args: I) -> Self
82 I: Iterator<Item = String>,
84 let mut cargo_subcommand = "check";
85 let mut args = vec![];
86 let mut clippy_args: Vec<String> = vec![];
88 for arg in old_args.by_ref() {
91 cargo_subcommand = "fix";
95 clippy_args.push("--no-deps".into());
105 clippy_args.append(&mut (old_args.collect()));
106 if cargo_subcommand == "fix" && !clippy_args.iter().any(|arg| arg == "--no-deps") {
107 clippy_args.push("--no-deps".into());
117 fn path() -> PathBuf {
118 let mut path = env::current_exe()
119 .expect("current executable path invalid")
120 .with_file_name("clippy-driver");
123 path.set_extension("exe");
129 fn into_std_cmd(self) -> Command {
130 let mut cmd = Command::new("cargo");
131 let clippy_args: String = self
134 .map(|arg| format!("{arg}__CLIPPY_HACKERY__"))
137 // Currently, `CLIPPY_TERMINAL_WIDTH` is used only to format "unknown field" error messages.
138 let terminal_width = termize::dimensions().map_or(0, |(w, _)| w);
140 cmd.env("RUSTC_WORKSPACE_WRAPPER", Self::path())
141 .env("CLIPPY_ARGS", clippy_args)
142 .env("CLIPPY_TERMINAL_WIDTH", terminal_width.to_string())
143 .arg(self.cargo_subcommand)
150 fn process<I>(old_args: I) -> Result<(), i32>
152 I: Iterator<Item = String>,
154 let cmd = ClippyCmd::new(old_args);
156 let mut cmd = cmd.into_std_cmd();
158 let exit_status = cmd
160 .expect("could not run cargo")
162 .expect("failed to wait for cargo?");
164 if exit_status.success() {
167 Err(exit_status.code().unwrap_or(-1))
173 use super::ClippyCmd;
177 let args = "cargo clippy --fix".split_whitespace().map(ToString::to_string);
178 let cmd = ClippyCmd::new(args);
179 assert_eq!("fix", cmd.cargo_subcommand);
180 assert!(!cmd.args.iter().any(|arg| arg.ends_with("unstable-options")));
184 fn fix_implies_no_deps() {
185 let args = "cargo clippy --fix".split_whitespace().map(ToString::to_string);
186 let cmd = ClippyCmd::new(args);
187 assert!(cmd.clippy_args.iter().any(|arg| arg == "--no-deps"));
191 fn no_deps_not_duplicated_with_fix() {
192 let args = "cargo clippy --fix -- --no-deps"
194 .map(ToString::to_string);
195 let cmd = ClippyCmd::new(args);
196 assert_eq!(cmd.clippy_args.iter().filter(|arg| *arg == "--no-deps").count(), 1);
201 let args = "cargo clippy".split_whitespace().map(ToString::to_string);
202 let cmd = ClippyCmd::new(args);
203 assert_eq!("check", cmd.cargo_subcommand);