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;
12 use std::path::{Path, PathBuf};
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"));
52 // Check for version and help flags even when invoked as 'cargo-clippy'
53 if std::env::args().any(|a| a == "--help" || a == "-h") {
57 if std::env::args().any(|a| a == "--version" || a == "-V") {
62 let manifest_path_arg = std::env::args()
64 .find(|val| val.starts_with("--manifest-path="))
65 .map(|val| val["--manifest-path=".len()..].to_owned());
67 let mut metadata = if let Ok(metadata) = cargo_metadata::metadata(manifest_path_arg.as_ref().map(AsRef::as_ref)) {
70 println!("{:?}", cargo_metadata::metadata(manifest_path_arg.as_ref().map(AsRef::as_ref)));
71 let _ = io::stderr().write_fmt(format_args!("error: Could not obtain cargo metadata.\n"));
75 let manifest_path = manifest_path_arg.map(|arg| {
78 .expect("manifest path could not be canonicalized")
81 let packages = if std::env::args().any(|a| a == "--all") {
85 if let Some(manifest_path) = manifest_path {
86 metadata.packages.iter().position(|package| {
87 let package_manifest_path = Path::new(&package.manifest_path)
89 .expect("package manifest path could not be canonicalized");
90 package_manifest_path == manifest_path
93 let package_manifest_paths: HashMap<_, _> = metadata
98 let package_manifest_path = Path::new(&package.manifest_path)
100 .expect("could not find parent directory of package manifest")
102 .expect("package directory cannot be canonicalized");
103 (package_manifest_path, i)
107 let current_dir = std::env::current_dir()
108 .expect("could not read current directory")
110 .expect("current directory cannot be canonicalized");
112 let mut current_path: &Path = ¤t_dir;
114 // This gets the most-recent parent (the one that takes the fewest `cd ..`s to
117 if let Some(&package_index) = package_manifest_paths.get(current_path) {
118 break Some(package_index);
120 // We'll never reach the filesystem root, because to get to this point in the
122 // the call to `cargo_metadata::metadata` must have succeeded. So it's okay to
123 // unwrap the current path's parent.
124 current_path = current_path
126 .unwrap_or_else(|| panic!("could not find parent of path {}", current_path.display()));
130 }.expect("could not find matching package");
132 vec![metadata.packages.remove(package_index)]
135 for package in packages {
136 let manifest_path = package.manifest_path;
138 for target in package.targets {
139 let args = std::env::args()
141 .filter(|a| a != "--all" && !a.starts_with("--manifest-path="));
143 let args = std::iter::once(format!("--manifest-path={}", manifest_path)).chain(args);
144 if let Some(first) = target.kind.get(0) {
145 if target.kind.len() > 1 || first.ends_with("lib") {
146 println!("lib: {}", target.name);
147 if let Err(code) = process(std::iter::once("--lib".to_owned()).chain(args)) {
148 std::process::exit(code);
150 } else if ["bin", "example", "test", "bench"].contains(&&**first) {
151 println!("{}: {}", first, target.name);
152 if let Err(code) = process(
153 vec![format!("--{}", first), target.name]
157 std::process::exit(code);
161 panic!("badly formatted cargo metadata: target::kind is an empty array");
167 fn process<I>(old_args: I) -> Result<(), i32>
169 I: Iterator<Item = String>,
171 let mut args = vec!["rustc".to_owned()];
173 let mut found_dashes = false;
174 for arg in old_args {
175 found_dashes |= arg == "--";
179 args.push("--".to_owned());
181 args.push("--emit=metadata".to_owned());
182 args.push("--cfg".to_owned());
183 args.push(r#"feature="cargo-clippy""#.to_owned());
185 let mut path = std::env::current_exe()
186 .expect("current executable path invalid")
187 .with_file_name("clippy-driver");
189 path.set_extension("exe");
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))