]>
git.proxmox.com Git - rustc.git/blob - src/tools/clippy/src/main.rs
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
21 Other options are the same as `cargo check`.
23 To allow or deny a lint from the command line you can use `cargo clippy --`
26 -W --warn OPT Set lint warnings
27 -A --allow OPT Set lint allowed
28 -D --deny OPT Set lint denied
29 -F --forbid OPT Set lint forbidden
31 You can use tool lints to allow or deny lints from your code, eg.:
33 #[allow(clippy::needless_lifetimes)]
37 println
!("{}", CARGO_CLIPPY_HELP
);
41 let version_info
= rustc_tools_util
::get_version_info
!();
42 println
!("{}", version_info
);
46 // Check for version and help flags even when invoked as 'cargo-clippy'
47 if env
::args().any(|a
| a
== "--help" || a
== "-h") {
52 if env
::args().any(|a
| a
== "--version" || a
== "-V") {
57 if let Err(code
) = process(env
::args().skip(2)) {
63 cargo_subcommand
: &'
static str,
65 clippy_args
: Vec
<String
>,
69 fn new
<I
>(mut old_args
: I
) -> Self
71 I
: Iterator
<Item
= String
>,
73 let mut cargo_subcommand
= "check";
74 let mut args
= vec
![];
75 let mut clippy_args
: Vec
<String
> = vec
![];
77 for arg
in old_args
.by_ref() {
80 cargo_subcommand
= "fix";
84 clippy_args
.push("--no-deps".into());
94 clippy_args
.append(&mut (old_args
.collect()));
95 if cargo_subcommand
== "fix" && !clippy_args
.iter().any(|arg
| arg
== "--no-deps") {
96 clippy_args
.push("--no-deps".into());
106 fn path() -> PathBuf
{
107 let mut path
= env
::current_exe()
108 .expect("current executable path invalid")
109 .with_file_name("clippy-driver");
112 path
.set_extension("exe");
118 fn into_std_cmd(self) -> Command
{
119 let mut cmd
= Command
::new("cargo");
120 let clippy_args
: String
= self
123 .map(|arg
| format
!("{}__CLIPPY_HACKERY__", arg
))
126 cmd
.env("RUSTC_WORKSPACE_WRAPPER", Self::path())
127 .env("CLIPPY_ARGS", clippy_args
)
128 .arg(self.cargo_subcommand
)
135 fn process
<I
>(old_args
: I
) -> Result
<(), i32>
137 I
: Iterator
<Item
= String
>,
139 let cmd
= ClippyCmd
::new(old_args
);
141 let mut cmd
= cmd
.into_std_cmd();
143 let exit_status
= cmd
145 .expect("could not run cargo")
147 .expect("failed to wait for cargo?");
149 if exit_status
.success() {
152 Err(exit_status
.code().unwrap_or(-1))
158 use super::ClippyCmd
;
162 let args
= "cargo clippy --fix".split_whitespace().map(ToString
::to_string
);
163 let cmd
= ClippyCmd
::new(args
);
164 assert_eq
!("fix", cmd
.cargo_subcommand
);
165 assert
!(!cmd
.args
.iter().any(|arg
| arg
.ends_with("unstable-options")));
169 fn fix_implies_no_deps() {
170 let args
= "cargo clippy --fix".split_whitespace().map(ToString
::to_string
);
171 let cmd
= ClippyCmd
::new(args
);
172 assert
!(cmd
.clippy_args
.iter().any(|arg
| arg
== "--no-deps"));
176 fn no_deps_not_duplicated_with_fix() {
177 let args
= "cargo clippy --fix -- --no-deps"
179 .map(ToString
::to_string
);
180 let cmd
= ClippyCmd
::new(args
);
181 assert_eq
!(cmd
.clippy_args
.iter().filter(|arg
| *arg
== "--no-deps").count(), 1);
186 let args
= "cargo clippy".split_whitespace().map(ToString
::to_string
);
187 let cmd
= ClippyCmd
::new(args
);
188 assert_eq
!("check", cmd
.cargo_subcommand
);