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)]
|
|
|
|
|
2016-06-28 13:54:23 +00:00
|
|
|
extern crate clippy_lints;
|
2016-05-24 16:25:25 +00:00
|
|
|
extern crate getopts;
|
|
|
|
extern crate rustc;
|
2016-06-28 13:54:23 +00:00
|
|
|
extern crate rustc_driver;
|
|
|
|
extern crate rustc_errors;
|
2016-05-24 16:25:25 +00:00
|
|
|
extern crate rustc_plugin;
|
2016-06-28 13:54:23 +00:00
|
|
|
extern crate syntax;
|
2016-05-24 16:25:25 +00:00
|
|
|
|
2017-08-18 17:11:15 +00:00
|
|
|
use rustc_driver::{driver, Compilation, CompilerCalls, RustcDefaultCalls};
|
|
|
|
use rustc::session::{config, CompileIncomplete, Session};
|
|
|
|
use rustc::session::config::{ErrorOutputType, Input};
|
2017-07-27 07:04:17 +00:00
|
|
|
use std::collections::HashMap;
|
2016-05-24 16:25:25 +00:00
|
|
|
use std::path::PathBuf;
|
2016-11-08 13:28:46 +00:00
|
|
|
use std::process::{self, Command};
|
2016-08-17 03:48:15 +00:00
|
|
|
use syntax::ast;
|
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
|
|
|
|
2016-07-04 11:33:48 +00:00
|
|
|
struct ClippyCompilerCalls {
|
|
|
|
default: RustcDefaultCalls,
|
|
|
|
run_lints: bool,
|
2016-05-24 16:25:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ClippyCompilerCalls {
|
2016-07-04 11:33:48 +00:00
|
|
|
fn new(run_lints: bool) -> Self {
|
2017-08-21 10:57:33 +00:00
|
|
|
Self {
|
2016-07-04 11:33:48 +00:00
|
|
|
default: RustcDefaultCalls,
|
|
|
|
run_lints: run_lints,
|
|
|
|
}
|
2016-05-24 16:25:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> CompilerCalls<'a> for ClippyCompilerCalls {
|
2016-12-21 09:25:14 +00:00
|
|
|
fn early_callback(
|
2016-12-21 11:36:25 +00:00
|
|
|
&mut self,
|
|
|
|
matches: &getopts::Matches,
|
|
|
|
sopts: &config::Options,
|
|
|
|
cfg: &ast::CrateConfig,
|
|
|
|
descriptions: &rustc_errors::registry::Registry,
|
2017-04-12 09:06:32 +00:00
|
|
|
output: ErrorOutputType,
|
2016-12-21 09:25:14 +00:00
|
|
|
) -> Compilation {
|
2017-09-05 09:33:04 +00:00
|
|
|
self.default
|
|
|
|
.early_callback(matches, sopts, cfg, descriptions, output)
|
2016-05-24 16:25:25 +00:00
|
|
|
}
|
2016-12-21 09:25:14 +00:00
|
|
|
fn no_input(
|
2016-12-21 11:36:25 +00:00
|
|
|
&mut self,
|
|
|
|
matches: &getopts::Matches,
|
|
|
|
sopts: &config::Options,
|
|
|
|
cfg: &ast::CrateConfig,
|
|
|
|
odir: &Option<PathBuf>,
|
|
|
|
ofile: &Option<PathBuf>,
|
2017-04-12 09:06:32 +00:00
|
|
|
descriptions: &rustc_errors::registry::Registry,
|
2016-12-21 09:25:14 +00:00
|
|
|
) -> Option<(Input, Option<PathBuf>)> {
|
2017-09-05 09:33:04 +00:00
|
|
|
self.default
|
|
|
|
.no_input(matches, sopts, cfg, odir, ofile, descriptions)
|
2016-05-24 16:25:25 +00:00
|
|
|
}
|
2016-12-21 09:25:14 +00:00
|
|
|
fn late_callback(
|
2016-12-21 11:36:25 +00:00
|
|
|
&mut self,
|
|
|
|
matches: &getopts::Matches,
|
|
|
|
sess: &Session,
|
2017-09-14 04:18:08 +00:00
|
|
|
crate_stores: &rustc::middle::cstore::CrateStore,
|
2016-12-21 11:36:25 +00:00
|
|
|
input: &Input,
|
|
|
|
odir: &Option<PathBuf>,
|
2017-04-12 09:06:32 +00:00
|
|
|
ofile: &Option<PathBuf>,
|
2016-12-21 09:25:14 +00:00
|
|
|
) -> Compilation {
|
2017-09-05 09:33:04 +00:00
|
|
|
self.default
|
2017-09-14 04:18:08 +00:00
|
|
|
.late_callback(matches, sess, crate_stores, input, odir, ofile)
|
2016-05-24 16:25:25 +00:00
|
|
|
}
|
|
|
|
fn build_controller(&mut self, sess: &Session, matches: &getopts::Matches) -> driver::CompileController<'a> {
|
2016-07-04 11:33:48 +00:00
|
|
|
let mut control = self.default.build_controller(sess, matches);
|
|
|
|
|
|
|
|
if self.run_lints {
|
|
|
|
let old = std::mem::replace(&mut control.after_parse.callback, box |_| {});
|
|
|
|
control.after_parse.callback = Box::new(move |state| {
|
|
|
|
{
|
2017-08-09 07:30:56 +00:00
|
|
|
let mut registry = rustc_plugin::registry::Registry::new(
|
|
|
|
state.session,
|
|
|
|
state
|
|
|
|
.krate
|
|
|
|
.as_ref()
|
|
|
|
.expect(
|
|
|
|
"at this compilation stage \
|
2017-09-05 09:33:04 +00:00
|
|
|
the krate must be parsed",
|
2017-08-09 07:30:56 +00:00
|
|
|
)
|
|
|
|
.span,
|
|
|
|
);
|
2016-07-04 11:33:48 +00:00
|
|
|
registry.args_hidden = Some(Vec::new());
|
|
|
|
clippy_lints::register_plugins(&mut registry);
|
|
|
|
|
2017-04-12 09:06:32 +00:00
|
|
|
let rustc_plugin::registry::Registry {
|
|
|
|
early_lint_passes,
|
|
|
|
late_lint_passes,
|
|
|
|
lint_groups,
|
|
|
|
llvm_passes,
|
|
|
|
attributes,
|
|
|
|
..
|
|
|
|
} = registry;
|
2016-07-04 11:33:48 +00:00
|
|
|
let sess = &state.session;
|
|
|
|
let mut ls = sess.lint_store.borrow_mut();
|
|
|
|
for pass in early_lint_passes {
|
|
|
|
ls.register_early_pass(Some(sess), true, pass);
|
|
|
|
}
|
|
|
|
for pass in late_lint_passes {
|
|
|
|
ls.register_late_pass(Some(sess), true, pass);
|
|
|
|
}
|
2016-05-24 16:25:25 +00:00
|
|
|
|
2016-07-04 11:33:48 +00:00
|
|
|
for (name, to) in lint_groups {
|
|
|
|
ls.register_group(Some(sess), true, name, to);
|
|
|
|
}
|
2016-05-24 16:25:25 +00:00
|
|
|
|
2016-07-04 11:33:48 +00:00
|
|
|
sess.plugin_llvm_passes.borrow_mut().extend(llvm_passes);
|
|
|
|
sess.plugin_attributes.borrow_mut().extend(attributes);
|
|
|
|
}
|
|
|
|
old(state);
|
|
|
|
});
|
|
|
|
}
|
2016-05-24 16:25:25 +00:00
|
|
|
|
|
|
|
control
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use std::path::Path;
|
|
|
|
|
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() {
|
|
|
|
use std::env;
|
|
|
|
|
|
|
|
if env::var("CLIPPY_DOGFOOD").map(|_| true).unwrap_or(false) {
|
|
|
|
panic!("yummy");
|
|
|
|
}
|
2016-12-20 09:20:41 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
if let Some("clippy") = std::env::args().nth(1).as_ref().map(AsRef::as_ref) {
|
2016-07-04 11:33:48 +00:00
|
|
|
// this arm is executed on the initial call to `cargo clippy`
|
2016-10-24 13:31:11 +00:00
|
|
|
|
2017-08-18 17:11:15 +00:00
|
|
|
let manifest_path_arg = std::env::args()
|
|
|
|
.skip(2)
|
|
|
|
.find(|val| val.starts_with("--manifest-path="));
|
2016-10-23 01:15:42 +00:00
|
|
|
|
2017-09-05 09:33:04 +00:00
|
|
|
let mut metadata = if let Ok(metadata) = cargo_metadata::metadata(manifest_path_arg.as_ref().map(AsRef::as_ref))
|
|
|
|
{
|
|
|
|
metadata
|
|
|
|
} else {
|
|
|
|
let _ = io::stderr().write_fmt(format_args!("error: Could not obtain cargo metadata.\n"));
|
|
|
|
process::exit(101);
|
|
|
|
};
|
2016-10-24 13:31:11 +00:00
|
|
|
|
2017-08-09 07:30:56 +00:00
|
|
|
let manifest_path = manifest_path_arg.map(|arg| {
|
|
|
|
Path::new(&arg["--manifest-path=".len()..])
|
|
|
|
.canonicalize()
|
|
|
|
.expect("manifest path could not be canonicalized")
|
|
|
|
});
|
2016-10-23 01:15:42 +00:00
|
|
|
|
2017-08-18 17:11:15 +00:00
|
|
|
let packages = if std::env::args().any(|a| a == "--all") {
|
2017-08-18 16:57:33 +00:00
|
|
|
metadata.packages
|
|
|
|
} else {
|
|
|
|
let package_index = {
|
|
|
|
if let Some(manifest_path) = manifest_path {
|
|
|
|
metadata.packages.iter().position(|package| {
|
2017-08-18 17:11:15 +00:00
|
|
|
let package_manifest_path = Path::new(&package.manifest_path)
|
|
|
|
.canonicalize()
|
|
|
|
.expect("package manifest path could not be canonicalized");
|
2017-08-18 16:57:33 +00:00
|
|
|
package_manifest_path == manifest_path
|
2017-06-29 10:57:28 +00:00
|
|
|
})
|
2017-08-18 16:57:33 +00:00
|
|
|
} else {
|
|
|
|
let package_manifest_paths: HashMap<_, _> = metadata
|
|
|
|
.packages
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(i, package)| {
|
|
|
|
let package_manifest_path = Path::new(&package.manifest_path)
|
|
|
|
.parent()
|
|
|
|
.expect("could not find parent directory of package manifest")
|
|
|
|
.canonicalize()
|
|
|
|
.expect("package directory cannot be canonicalized");
|
|
|
|
(package_manifest_path, i)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let current_dir = std::env::current_dir()
|
|
|
|
.expect("could not read current directory")
|
|
|
|
.canonicalize()
|
|
|
|
.expect("current directory cannot be canonicalized");
|
|
|
|
|
|
|
|
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.
|
2017-08-18 17:11:15 +00:00
|
|
|
current_path = current_path
|
|
|
|
.parent()
|
|
|
|
.unwrap_or_else(|| panic!("could not find parent of path {}", current_path.display()));
|
2017-08-18 16:57:33 +00:00
|
|
|
}
|
2017-07-27 07:04:17 +00:00
|
|
|
}
|
2016-10-23 01:15:42 +00:00
|
|
|
}
|
2017-08-18 16:57:33 +00:00
|
|
|
}.expect("could not find matching package");
|
|
|
|
|
|
|
|
vec![metadata.packages.remove(package_index)]
|
|
|
|
};
|
|
|
|
|
|
|
|
for package in packages {
|
|
|
|
let manifest_path = package.manifest_path;
|
|
|
|
|
|
|
|
for target in package.targets {
|
2017-08-18 17:11:15 +00:00
|
|
|
let args = std::env::args()
|
|
|
|
.skip(2)
|
|
|
|
.filter(|a| a != "--all" && !a.starts_with("--manifest-path="));
|
2017-08-18 16:57: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") {
|
|
|
|
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) {
|
|
|
|
if let Err(code) = process(
|
|
|
|
vec![format!("--{}", first), target.name]
|
|
|
|
.into_iter()
|
|
|
|
.chain(args),
|
2017-08-18 17:11:15 +00:00
|
|
|
) {
|
2017-08-18 16:57:33 +00:00
|
|
|
std::process::exit(code);
|
|
|
|
}
|
2016-08-17 15:04:21 +00:00
|
|
|
}
|
2017-08-18 16:57:33 +00:00
|
|
|
} else {
|
|
|
|
panic!("badly formatted cargo metadata: target::kind is an empty array");
|
2016-06-02 15:39:28 +00:00
|
|
|
}
|
2016-05-27 13:31:19 +00:00
|
|
|
}
|
2016-05-24 16:25:25 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-04-12 09:06:32 +00:00
|
|
|
// this arm is executed when cargo-clippy runs `cargo rustc` with the `RUSTC`
|
|
|
|
// env var set to itself
|
2016-07-04 11:33:48 +00:00
|
|
|
|
2016-11-08 10:35:26 +00:00
|
|
|
let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
|
|
|
|
let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
|
|
|
|
let sys_root = if let (Some(home), Some(toolchain)) = (home, toolchain) {
|
|
|
|
format!("{}/toolchains/{}", home, toolchain)
|
|
|
|
} else {
|
|
|
|
option_env!("SYSROOT")
|
|
|
|
.map(|s| s.to_owned())
|
2017-01-15 00:03:46 +00:00
|
|
|
.or_else(|| {
|
|
|
|
Command::new("rustc")
|
|
|
|
.arg("--print")
|
|
|
|
.arg("sysroot")
|
|
|
|
.output()
|
|
|
|
.ok()
|
|
|
|
.and_then(|out| String::from_utf8(out.stdout).ok())
|
|
|
|
.map(|s| s.trim().to_owned())
|
|
|
|
})
|
2017-08-09 07:30:56 +00:00
|
|
|
.expect(
|
|
|
|
"need to specify SYSROOT env var during clippy compilation, or use rustup or multirust",
|
|
|
|
)
|
2016-11-08 10:35:26 +00:00
|
|
|
};
|
|
|
|
|
2017-02-28 11:47:48 +00:00
|
|
|
rustc_driver::in_rustc_thread(|| {
|
2017-04-12 09:06:32 +00:00
|
|
|
// this conditional check for the --sysroot flag is there so users can call
|
|
|
|
// `cargo-clippy` directly
|
|
|
|
// without having to pass --sysroot or anything
|
|
|
|
let mut args: Vec<String> = if env::args().any(|s| s == "--sysroot") {
|
|
|
|
env::args().collect()
|
|
|
|
} else {
|
|
|
|
env::args()
|
|
|
|
.chain(Some("--sysroot".to_owned()))
|
|
|
|
.chain(Some(sys_root))
|
|
|
|
.collect()
|
|
|
|
};
|
|
|
|
|
|
|
|
// this check ensures that dependencies are built but not linted and the final
|
|
|
|
// crate is
|
|
|
|
// linted but not built
|
2017-06-26 12:49:30 +00:00
|
|
|
let clippy_enabled = env::args().any(|s| s == "--emit=metadata");
|
2017-04-12 09:06:32 +00:00
|
|
|
|
|
|
|
if clippy_enabled {
|
|
|
|
args.extend_from_slice(&["--cfg".to_owned(), r#"feature="cargo-clippy""#.to_owned()]);
|
|
|
|
}
|
2016-11-08 20:50:35 +00:00
|
|
|
|
2017-04-12 09:06:32 +00:00
|
|
|
let mut ccc = ClippyCompilerCalls::new(clippy_enabled);
|
|
|
|
let (result, _) = rustc_driver::run_compiler(&args, &mut ccc, None, None);
|
2017-07-03 04:26:03 +00:00
|
|
|
if let Err(CompileIncomplete::Errored(_)) = result {
|
|
|
|
std::process::exit(1);
|
2017-04-12 09:06:32 +00:00
|
|
|
}
|
2017-08-09 07:30:56 +00:00
|
|
|
}).expect("rustc_thread failed");
|
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
|
|
|
|
|
|
|
let path = std::env::current_exe().expect("current executable path invalid");
|
|
|
|
let exit_status = std::process::Command::new("cargo")
|
|
|
|
.args(&args)
|
|
|
|
.env("RUSTC", 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
|
|
|
}
|