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};
12 const CARGO_CLIPPY_HELP: &str = r#"Checks a package to catch common mistakes and improve your Rust code.
15 cargo clippy [options] [--] [<opts>...]
18 --no-deps Run Clippy only on the given crate, without linting the dependencies
19 --fix Automatically apply lint suggestions. This flag implies `--no-deps`
20 -h, --help Print this message
21 -V, --version Print version info and exit
22 --explain LINT Print the documentation for a given lint
24 Other options are the same as `cargo check`.
26 To allow or deny a lint from the command line you can use `cargo clippy --`
29 -W --warn OPT Set lint warnings
30 -A --allow OPT Set lint allowed
31 -D --deny OPT Set lint denied
32 -F --forbid OPT Set lint forbidden
34 You can use tool lints to allow or deny lints from your code, eg.:
36 #[allow(clippy::needless_lifetimes)]
40 println!("{CARGO_CLIPPY_HELP}");
44 let version_info = rustc_tools_util::get_version_info!();
45 println!("{version_info}");
49 // Check for version and help flags even when invoked as 'cargo-clippy'
50 if env::args().any(|a| a == "--help" || a == "-h") {
55 if env::args().any(|a| a == "--version" || a == "-V") {
60 if let Some(pos) = env::args().position(|a| a == "--explain") {
61 if let Some(mut lint) = env::args().nth(pos + 1) {
62 lint.make_ascii_lowercase();
63 docs::explain(&lint.strip_prefix("clippy::").unwrap_or(&lint).replace('-', "_"));
70 if let Err(code) = process(env::args().skip(2)) {
76 cargo_subcommand: &'static str,
78 clippy_args: Vec<String>,
82 fn new<I>(mut old_args: I) -> Self
84 I: Iterator<Item = String>,
86 let mut cargo_subcommand = "check";
87 let mut args = vec![];
88 let mut clippy_args: Vec<String> = vec![];
90 for arg in old_args.by_ref() {
93 cargo_subcommand = "fix";
97 clippy_args.push("--no-deps".into());
107 clippy_args.append(&mut (old_args.collect()));
108 if cargo_subcommand == "fix" && !clippy_args.iter().any(|arg| arg == "--no-deps") {
109 clippy_args.push("--no-deps".into());
119 fn path() -> PathBuf {
120 let mut path = env::current_exe()
121 .expect("current executable path invalid")
122 .with_file_name("clippy-driver");
125 path.set_extension("exe");
131 fn into_std_cmd(self) -> Command {
132 let mut cmd = Command::new("cargo");
133 let clippy_args: String = self
136 .map(|arg| format!("{arg}__CLIPPY_HACKERY__"))
139 // Currently, `CLIPPY_TERMINAL_WIDTH` is used only to format "unknown field" error messages.
140 let terminal_width = termize::dimensions().map_or(0, |(w, _)| w);
142 cmd.env("RUSTC_WORKSPACE_WRAPPER", Self::path())
143 .env("CLIPPY_ARGS", clippy_args)
144 .env("CLIPPY_TERMINAL_WIDTH", terminal_width.to_string())
145 .arg(self.cargo_subcommand)
152 fn process<I>(old_args: I) -> Result<(), i32>
154 I: Iterator<Item = String>,
156 let cmd = ClippyCmd::new(old_args);
158 let mut cmd = cmd.into_std_cmd();
160 let exit_status = cmd
162 .expect("could not run cargo")
164 .expect("failed to wait for cargo?");
166 if exit_status.success() {
169 Err(exit_status.code().unwrap_or(-1))
175 use super::ClippyCmd;
179 let args = "cargo clippy --fix".split_whitespace().map(ToString::to_string);
180 let cmd = ClippyCmd::new(args);
181 assert_eq!("fix", cmd.cargo_subcommand);
182 assert!(!cmd.args.iter().any(|arg| arg.ends_with("unstable-options")));
186 fn fix_implies_no_deps() {
187 let args = "cargo clippy --fix".split_whitespace().map(ToString::to_string);
188 let cmd = ClippyCmd::new(args);
189 assert!(cmd.clippy_args.iter().any(|arg| arg == "--no-deps"));
193 fn no_deps_not_duplicated_with_fix() {
194 let args = "cargo clippy --fix -- --no-deps"
196 .map(ToString::to_string);
197 let cmd = ClippyCmd::new(args);
198 assert_eq!(cmd.clippy_args.iter().filter(|arg| *arg == "--no-deps").count(), 1);
203 let args = "cargo clippy".split_whitespace().map(ToString::to_string);
204 let cmd = ClippyCmd::new(args);
205 assert_eq!("check", cmd.cargo_subcommand);