2 #![feature(box_syntax)]
3 #![feature(rustc_private)]
4 #![allow(unknown_lints, missing_docs_in_private_items)]
6 use std::collections::HashMap;
8 use std::io::{self, Write};
10 extern crate cargo_metadata;
14 const CARGO_CLIPPY_HELP: &str = r#"Checks a package to catch common mistakes and improve your Rust code.
17 cargo clippy [options] [--] [<opts>...]
20 -h, --help Print this message
21 --features Features to compile for the package
22 -V, --version Print version info and exit
23 --all Run over all packages in the current workspace
25 Other options are the same as `cargo rustc`.
27 To allow or deny a lint from the command line you can use `cargo clippy --`
30 -W --warn OPT Set lint warnings
31 -A --allow OPT Set lint allowed
32 -D --deny OPT Set lint denied
33 -F --forbid OPT Set lint forbidden
35 The feature `cargo-clippy` is automatically defined for convenience. You can use
36 it to allow or deny lints from the code, eg.:
38 #[cfg_attr(feature = "cargo-clippy", allow(needless_lifetimes))]
41 #[allow(print_stdout)]
43 println!("{}", CARGO_CLIPPY_HELP);
46 #[allow(print_stdout)]
48 println!("{}", env!("CARGO_PKG_VERSION"));
54 if env::var("CLIPPY_DOGFOOD").is_ok() {
58 // Check for version and help flags even when invoked as 'cargo-clippy'
59 if std::env::args().any(|a| a == "--help" || a == "-h") {
63 if std::env::args().any(|a| a == "--version" || a == "-V") {
68 let manifest_path_arg = std::env::args()
70 .find(|val| val.starts_with("--manifest-path="));
72 let mut metadata = if let Ok(metadata) = cargo_metadata::metadata(manifest_path_arg.as_ref().map(AsRef::as_ref))
76 let _ = io::stderr().write_fmt(format_args!("error: Could not obtain cargo metadata.\n"));
80 let manifest_path = manifest_path_arg.map(|arg| {
81 Path::new(&arg["--manifest-path=".len()..])
83 .expect("manifest path could not be canonicalized")
86 let packages = if std::env::args().any(|a| a == "--all") {
90 if let Some(manifest_path) = manifest_path {
91 metadata.packages.iter().position(|package| {
92 let package_manifest_path = Path::new(&package.manifest_path)
94 .expect("package manifest path could not be canonicalized");
95 package_manifest_path == manifest_path
98 let package_manifest_paths: HashMap<_, _> = metadata
102 .map(|(i, package)| {
103 let package_manifest_path = Path::new(&package.manifest_path)
105 .expect("could not find parent directory of package manifest")
107 .expect("package directory cannot be canonicalized");
108 (package_manifest_path, i)
112 let current_dir = std::env::current_dir()
113 .expect("could not read current directory")
115 .expect("current directory cannot be canonicalized");
117 let mut current_path: &Path = ¤t_dir;
119 // This gets the most-recent parent (the one that takes the fewest `cd ..`s to
122 if let Some(&package_index) = package_manifest_paths.get(current_path) {
123 break Some(package_index);
125 // We'll never reach the filesystem root, because to get to this point in the
127 // the call to `cargo_metadata::metadata` must have succeeded. So it's okay to
128 // unwrap the current path's parent.
129 current_path = current_path
131 .unwrap_or_else(|| panic!("could not find parent of path {}", current_path.display()));
135 }.expect("could not find matching package");
137 vec![metadata.packages.remove(package_index)]
140 for package in packages {
141 let manifest_path = package.manifest_path;
143 for target in package.targets {
144 let args = std::env::args()
146 .filter(|a| a != "--all" && !a.starts_with("--manifest-path="));
148 let args = std::iter::once(format!("--manifest-path={}", manifest_path)).chain(args);
149 if let Some(first) = target.kind.get(0) {
150 if target.kind.len() > 1 || first.ends_with("lib") {
151 if let Err(code) = process(std::iter::once("--lib".to_owned()).chain(args)) {
152 std::process::exit(code);
154 } else if ["bin", "example", "test", "bench"].contains(&&**first) {
155 if let Err(code) = process(
156 vec![format!("--{}", first), target.name]
160 std::process::exit(code);
164 panic!("badly formatted cargo metadata: target::kind is an empty array");
170 fn process<I>(old_args: I) -> Result<(), i32>
172 I: Iterator<Item = String>,
174 let mut args = vec!["rustc".to_owned()];
176 let mut found_dashes = false;
177 for arg in old_args {
178 found_dashes |= arg == "--";
182 args.push("--".to_owned());
184 args.push("--emit=metadata".to_owned());
185 args.push("--cfg".to_owned());
186 args.push(r#"feature="cargo-clippy""#.to_owned());
188 let path = std::env::current_exe()
189 .expect("current executable path invalid")
190 .with_file_name("clippy-driver");
191 let exit_status = std::process::Command::new("cargo")
193 .env("RUSTC_WRAPPER", path)
195 .expect("could not run cargo")
197 .expect("failed to wait for cargo?");
199 if exit_status.success() {
202 Err(exit_status.code().unwrap_or(-1))