2016-05-24 16:25:25 +00:00
|
|
|
// error-pattern:yummy
|
|
|
|
#![feature(box_syntax)]
|
|
|
|
#![feature(rustc_private)]
|
2016-08-23 16:09:37 +00:00
|
|
|
#![allow(unknown_lints, missing_docs_in_private_items)]
|
|
|
|
|
2017-07-27 07:04:17 +00:00
|
|
|
use std::collections::HashMap;
|
2017-09-18 10:47:33 +00:00
|
|
|
use std::process;
|
2016-11-08 13:28:46 +00:00
|
|
|
use std::io::{self, Write};
|
2016-05-24 16:25:25 +00:00
|
|
|
|
2017-01-24 10:31:42 +00:00
|
|
|
extern crate cargo_metadata;
|
2016-05-27 13:31:19 +00:00
|
|
|
|
2018-03-13 14:02:40 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2016-05-24 16:25:25 +00:00
|
|
|
|
2016-10-24 14:04:00 +00:00
|
|
|
const CARGO_CLIPPY_HELP: &str = r#"Checks a package to catch common mistakes and improve your Rust code.
|
2016-10-24 13:31:11 +00:00
|
|
|
|
|
|
|
Usage:
|
|
|
|
cargo clippy [options] [--] [<opts>...]
|
|
|
|
|
|
|
|
Common options:
|
|
|
|
-h, --help Print this message
|
|
|
|
--features Features to compile for the package
|
2016-11-08 12:54:08 +00:00
|
|
|
-V, --version Print version info and exit
|
2017-08-20 04:10:13 +00:00
|
|
|
--all Run over all packages in the current workspace
|
2016-10-24 13:31:11 +00:00
|
|
|
|
|
|
|
Other options are the same as `cargo rustc`.
|
|
|
|
|
2016-10-25 13:09:56 +00:00
|
|
|
To allow or deny a lint from the command line you can use `cargo clippy --`
|
|
|
|
with:
|
2016-10-24 13:31:11 +00:00
|
|
|
|
|
|
|
-W --warn OPT Set lint warnings
|
|
|
|
-A --allow OPT Set lint allowed
|
|
|
|
-D --deny OPT Set lint denied
|
2016-10-24 14:04:00 +00:00
|
|
|
-F --forbid OPT Set lint forbidden
|
|
|
|
|
2016-10-25 13:09:56 +00:00
|
|
|
The feature `cargo-clippy` is automatically defined for convenience. You can use
|
|
|
|
it to allow or deny lints from the code, eg.:
|
2016-10-24 14:04:00 +00:00
|
|
|
|
|
|
|
#[cfg_attr(feature = "cargo-clippy", allow(needless_lifetimes))]
|
|
|
|
"#;
|
2016-10-24 13:31:11 +00:00
|
|
|
|
2016-10-24 14:29:36 +00:00
|
|
|
#[allow(print_stdout)]
|
|
|
|
fn show_help() {
|
|
|
|
println!("{}", CARGO_CLIPPY_HELP);
|
|
|
|
}
|
|
|
|
|
2016-11-08 13:28:46 +00:00
|
|
|
#[allow(print_stdout)]
|
|
|
|
fn show_version() {
|
|
|
|
println!("{}", env!("CARGO_PKG_VERSION"));
|
|
|
|
}
|
|
|
|
|
2016-05-24 16:25:25 +00:00
|
|
|
pub fn main() {
|
2016-11-08 12:54:08 +00:00
|
|
|
// Check for version and help flags even when invoked as 'cargo-clippy'
|
|
|
|
if std::env::args().any(|a| a == "--help" || a == "-h") {
|
|
|
|
show_help();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if std::env::args().any(|a| a == "--version" || a == "-V") {
|
2016-11-08 13:28:46 +00:00
|
|
|
show_version();
|
2016-11-08 12:54:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-05-24 16:25:25 +00:00
|
|
|
|
2018-03-19 08:26:05 +00:00
|
|
|
let mut manifest_path_arg = std::env::args()
|
2017-09-18 10:47:33 +00:00
|
|
|
.skip(2)
|
2018-03-19 08:26:05 +00:00
|
|
|
.skip_while(|val| !val.starts_with("--manifest-path"));
|
|
|
|
let manifest_path_arg = manifest_path_arg.next().and_then(|val| {
|
|
|
|
if val == "--manifest-path" {
|
|
|
|
manifest_path_arg.next()
|
|
|
|
} else if val.starts_with("--manifest-path=") {
|
|
|
|
Some(val["--manifest-path=".len()..].to_owned())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
});
|
2016-10-24 13:31:11 +00:00
|
|
|
|
2017-11-04 19:55:56 +00:00
|
|
|
let mut metadata = if let Ok(metadata) = cargo_metadata::metadata(manifest_path_arg.as_ref().map(AsRef::as_ref)) {
|
2017-09-18 10:47:33 +00:00
|
|
|
metadata
|
|
|
|
} else {
|
2018-03-15 15:08:49 +00:00
|
|
|
println!(
|
|
|
|
"{:?}",
|
|
|
|
cargo_metadata::metadata(manifest_path_arg.as_ref().map(AsRef::as_ref))
|
|
|
|
);
|
2017-09-18 10:47:33 +00:00
|
|
|
let _ = io::stderr().write_fmt(format_args!("error: Could not obtain cargo metadata.\n"));
|
|
|
|
process::exit(101);
|
|
|
|
};
|
|
|
|
|
|
|
|
let manifest_path = manifest_path_arg.map(|arg| {
|
2018-03-13 14:02:40 +00:00
|
|
|
PathBuf::from(arg)
|
2017-09-18 10:47:33 +00:00
|
|
|
.canonicalize()
|
|
|
|
.expect("manifest path could not be canonicalized")
|
|
|
|
});
|
|
|
|
|
|
|
|
let packages = if std::env::args().any(|a| a == "--all") {
|
|
|
|
metadata.packages
|
|
|
|
} else {
|
|
|
|
let package_index = {
|
|
|
|
if let Some(manifest_path) = manifest_path {
|
|
|
|
metadata.packages.iter().position(|package| {
|
|
|
|
let package_manifest_path = Path::new(&package.manifest_path)
|
|
|
|
.canonicalize()
|
|
|
|
.expect("package manifest path could not be canonicalized");
|
|
|
|
package_manifest_path == manifest_path
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
let package_manifest_paths: HashMap<_, _> = metadata
|
|
|
|
.packages
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(i, package)| {
|
2017-08-18 17:11:15 +00:00
|
|
|
let package_manifest_path = Path::new(&package.manifest_path)
|
2017-09-18 10:47:33 +00:00
|
|
|
.parent()
|
|
|
|
.expect("could not find parent directory of package manifest")
|
2017-08-18 17:11:15 +00:00
|
|
|
.canonicalize()
|
2017-09-18 10:47:33 +00:00
|
|
|
.expect("package directory cannot be canonicalized");
|
|
|
|
(package_manifest_path, i)
|
2017-06-29 10:57:28 +00:00
|
|
|
})
|
2017-09-18 10:47:33 +00:00
|
|
|
.collect();
|
|
|
|
|
2018-03-27 10:14:46 +00:00
|
|
|
let current_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR")
|
|
|
|
.expect("CARGO_MANIFEST_DIR not set"))
|
2017-09-18 10:47:33 +00:00
|
|
|
.canonicalize()
|
2018-03-27 10:14:46 +00:00
|
|
|
.expect("manifest directory cannot be canonicalized");
|
2017-09-18 10:47:33 +00:00
|
|
|
|
|
|
|
let mut current_path: &Path = ¤t_dir;
|
|
|
|
|
|
|
|
// This gets the most-recent parent (the one that takes the fewest `cd ..`s to
|
|
|
|
// reach).
|
|
|
|
loop {
|
|
|
|
if let Some(&package_index) = package_manifest_paths.get(current_path) {
|
|
|
|
break Some(package_index);
|
|
|
|
} else {
|
|
|
|
// We'll never reach the filesystem root, because to get to this point in the
|
|
|
|
// code
|
|
|
|
// the call to `cargo_metadata::metadata` must have succeeded. So it's okay to
|
|
|
|
// unwrap the current path's parent.
|
|
|
|
current_path = current_path
|
|
|
|
.parent()
|
|
|
|
.unwrap_or_else(|| panic!("could not find parent of path {}", current_path.display()));
|
2017-07-27 07:04:17 +00:00
|
|
|
}
|
2016-10-23 01:15:42 +00:00
|
|
|
}
|
2017-09-18 10:47:33 +00:00
|
|
|
}
|
|
|
|
}.expect("could not find matching package");
|
2017-08-18 16:57:33 +00:00
|
|
|
|
2017-09-18 10:47:33 +00:00
|
|
|
vec![metadata.packages.remove(package_index)]
|
|
|
|
};
|
2017-08-18 16:57:33 +00:00
|
|
|
|
2017-09-18 10:47:33 +00:00
|
|
|
for package in packages {
|
|
|
|
let manifest_path = package.manifest_path;
|
2017-08-18 16:57:33 +00:00
|
|
|
|
2017-09-18 10:47:33 +00:00
|
|
|
for target in package.targets {
|
|
|
|
let args = std::env::args()
|
2018-03-25 06:52:00 +00:00
|
|
|
.skip(2)
|
2017-09-18 10:47:33 +00:00
|
|
|
.filter(|a| a != "--all" && !a.starts_with("--manifest-path="));
|
2017-08-18 16:57:33 +00:00
|
|
|
|
2017-09-18 10:47:33 +00:00
|
|
|
let args = std::iter::once(format!("--manifest-path={}", manifest_path)).chain(args);
|
|
|
|
if let Some(first) = target.kind.get(0) {
|
|
|
|
if target.kind.len() > 1 || first.ends_with("lib") {
|
2017-12-15 09:02:39 +00:00
|
|
|
println!("lib: {}", target.name);
|
2017-09-18 10:47:33 +00:00
|
|
|
if let Err(code) = process(std::iter::once("--lib".to_owned()).chain(args)) {
|
|
|
|
std::process::exit(code);
|
|
|
|
}
|
|
|
|
} else if ["bin", "example", "test", "bench"].contains(&&**first) {
|
2017-12-15 09:02:39 +00:00
|
|
|
println!("{}: {}", first, target.name);
|
2017-09-18 10:47:33 +00:00
|
|
|
if let Err(code) = process(
|
|
|
|
vec![format!("--{}", first), target.name]
|
|
|
|
.into_iter()
|
|
|
|
.chain(args),
|
|
|
|
) {
|
|
|
|
std::process::exit(code);
|
2016-08-17 15:04:21 +00:00
|
|
|
}
|
2016-06-02 15:39:28 +00:00
|
|
|
}
|
2017-04-12 09:06:32 +00:00
|
|
|
} else {
|
2017-09-18 10:47:33 +00:00
|
|
|
panic!("badly formatted cargo metadata: target::kind is an empty array");
|
2017-04-12 09:06:32 +00:00
|
|
|
}
|
2017-09-18 10:47:33 +00:00
|
|
|
}
|
2016-05-24 16:25:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-27 12:51:37 +00:00
|
|
|
fn process<I>(old_args: I) -> Result<(), i32>
|
2017-08-09 07:30:56 +00:00
|
|
|
where
|
|
|
|
I: Iterator<Item = String>,
|
2016-06-05 23:42:39 +00:00
|
|
|
{
|
2016-05-24 16:25:25 +00:00
|
|
|
let mut args = vec!["rustc".to_owned()];
|
|
|
|
|
|
|
|
let mut found_dashes = false;
|
|
|
|
for arg in old_args {
|
|
|
|
found_dashes |= arg == "--";
|
|
|
|
args.push(arg);
|
|
|
|
}
|
|
|
|
if !found_dashes {
|
|
|
|
args.push("--".to_owned());
|
|
|
|
}
|
2017-06-26 12:49:30 +00:00
|
|
|
args.push("--emit=metadata".to_owned());
|
2016-10-24 14:04:00 +00:00
|
|
|
args.push("--cfg".to_owned());
|
|
|
|
args.push(r#"feature="cargo-clippy""#.to_owned());
|
2016-05-27 13:31:19 +00:00
|
|
|
|
2018-01-17 07:52:41 +00:00
|
|
|
let mut path = std::env::current_exe()
|
2017-09-18 10:47:33 +00:00
|
|
|
.expect("current executable path invalid")
|
|
|
|
.with_file_name("clippy-driver");
|
2018-01-17 07:52:41 +00:00
|
|
|
if cfg!(windows) {
|
|
|
|
path.set_extension("exe");
|
|
|
|
}
|
2016-05-27 13:31:19 +00:00
|
|
|
let exit_status = std::process::Command::new("cargo")
|
|
|
|
.args(&args)
|
2017-09-27 18:17:08 +00:00
|
|
|
.env("RUSTC_WRAPPER", path)
|
2016-12-20 09:20:41 +00:00
|
|
|
.spawn()
|
|
|
|
.expect("could not run cargo")
|
|
|
|
.wait()
|
|
|
|
.expect("failed to wait for cargo?");
|
2016-05-27 13:31:19 +00:00
|
|
|
|
2016-06-06 09:28:09 +00:00
|
|
|
if exit_status.success() {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
2016-06-06 14:43:58 +00:00
|
|
|
Err(exit_status.code().unwrap_or(-1))
|
2016-05-27 13:31:19 +00:00
|
|
|
}
|
2016-05-24 16:25:25 +00:00
|
|
|
}
|