mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-10 15:14:29 +00:00
don't require cargo clippy
to pass a --lib
or --bin x
argument
This commit is contained in:
parent
cbc430a699
commit
0818e70497
3 changed files with 78 additions and 14 deletions
|
@ -32,12 +32,12 @@ quine-mc_cluskey = "0.2.2"
|
|||
# begin automatic update
|
||||
clippy_lints = { version = "0.0.70", path = "clippy_lints" }
|
||||
# end automatic update
|
||||
rustc-serialize = "0.3"
|
||||
|
||||
[dev-dependencies]
|
||||
compiletest_rs = "0.1.0"
|
||||
lazy_static = "0.1.15"
|
||||
regex = "0.1.56"
|
||||
rustc-serialize = "0.3"
|
||||
|
||||
[features]
|
||||
debugging = []
|
||||
|
|
48
src/cargo.rs
Normal file
48
src/cargo.rs
Normal file
|
@ -0,0 +1,48 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
#[derive(RustcDecodable, Debug)]
|
||||
pub struct Metadata {
|
||||
pub packages: Vec<Package>,
|
||||
resolve: Option<()>,
|
||||
pub version: usize,
|
||||
}
|
||||
|
||||
#[derive(RustcDecodable, Debug)]
|
||||
pub struct Package {
|
||||
name: String,
|
||||
version: String,
|
||||
id: String,
|
||||
source: Option<()>,
|
||||
dependencies: Vec<Dependency>,
|
||||
pub targets: Vec<Target>,
|
||||
features: HashMap<String, Vec<String>>,
|
||||
manifest_path: String,
|
||||
}
|
||||
|
||||
#[derive(RustcDecodable, Debug)]
|
||||
pub struct Dependency {
|
||||
name: String,
|
||||
source: Option<String>,
|
||||
req: String,
|
||||
kind: Option<String>,
|
||||
optional: bool,
|
||||
uses_default_features: bool,
|
||||
features: Vec<HashMap<String, String>>,
|
||||
target: Option<()>,
|
||||
}
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
#[derive(RustcDecodable, Debug)]
|
||||
pub enum Kind {
|
||||
dylib,
|
||||
test,
|
||||
bin,
|
||||
lib,
|
||||
}
|
||||
|
||||
#[derive(RustcDecodable, Debug)]
|
||||
pub struct Target {
|
||||
pub name: String,
|
||||
pub kind: Vec<Kind>,
|
||||
src_path: String,
|
||||
}
|
42
src/main.rs
42
src/main.rs
|
@ -8,6 +8,7 @@ extern crate rustc;
|
|||
extern crate syntax;
|
||||
extern crate rustc_plugin;
|
||||
extern crate clippy_lints;
|
||||
extern crate rustc_serialize;
|
||||
|
||||
use rustc_driver::{driver, CompilerCalls, RustcDefaultCalls, Compilation};
|
||||
use rustc::session::{config, Session};
|
||||
|
@ -16,6 +17,8 @@ use syntax::diagnostics;
|
|||
use std::path::PathBuf;
|
||||
use std::process::Command;
|
||||
|
||||
mod cargo;
|
||||
|
||||
struct ClippyCompilerCalls(RustcDefaultCalls);
|
||||
|
||||
impl std::default::Default for ClippyCompilerCalls {
|
||||
|
@ -118,16 +121,19 @@ pub fn main() {
|
|||
};
|
||||
|
||||
if let Some("clippy") = std::env::args().nth(1).as_ref().map(AsRef::as_ref) {
|
||||
let args = wrap_args(std::env::args().skip(2), dep_path, sys_root);
|
||||
let path = std::env::current_exe().expect("current executable path invalid");
|
||||
let exit_status = std::process::Command::new("cargo")
|
||||
.args(&args)
|
||||
.env("RUSTC", path)
|
||||
.spawn().expect("could not run cargo")
|
||||
.wait().expect("failed to wait for cargo?");
|
||||
|
||||
if let Some(code) = exit_status.code() {
|
||||
std::process::exit(code);
|
||||
let output = std::process::Command::new("cargo").args(&["metadata", "--no-deps"]).output().expect("could not run `cargo metadata`");
|
||||
let stdout = std::str::from_utf8(&output.stdout).expect("`cargo metadata` output is not utf8");
|
||||
let mut metadata: cargo::Metadata = rustc_serialize::json::decode(stdout).expect("`cargo metadata` output is not valid json");
|
||||
assert_eq!(metadata.version, 1);
|
||||
for target in metadata.packages.remove(0).targets {
|
||||
let args = std::env::args().skip(2);
|
||||
assert_eq!(target.kind.len(), 1);
|
||||
match target.kind[0] {
|
||||
cargo::Kind::dylib => process(std::iter::once("--lib".to_owned()).chain(args), &dep_path, &sys_root),
|
||||
cargo::Kind::bin => process(vec!["--bin".to_owned(), target.name].into_iter().chain(args), &dep_path, &sys_root),
|
||||
// don't process tests
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let args: Vec<String> = if env::args().any(|s| s == "--sysroot") {
|
||||
|
@ -145,7 +151,7 @@ pub fn main() {
|
|||
}
|
||||
}
|
||||
|
||||
fn wrap_args<P, I>(old_args: I, dep_path: P, sysroot: String) -> Vec<String>
|
||||
fn process<P, I>(old_args: I, dep_path: P, sysroot: &str)
|
||||
where P: AsRef<Path>, I: Iterator<Item=String> {
|
||||
|
||||
let mut args = vec!["rustc".to_owned()];
|
||||
|
@ -161,7 +167,17 @@ fn wrap_args<P, I>(old_args: I, dep_path: P, sysroot: String) -> Vec<String>
|
|||
args.push("-L".to_owned());
|
||||
args.push(dep_path.as_ref().to_string_lossy().into_owned());
|
||||
args.push(String::from("--sysroot"));
|
||||
args.push(sysroot);
|
||||
args.push(sysroot.to_owned());
|
||||
args.push("-Zno-trans".to_owned());
|
||||
args
|
||||
|
||||
let path = std::env::current_exe().expect("current executable path invalid");
|
||||
let exit_status = std::process::Command::new("cargo")
|
||||
.args(&args)
|
||||
.env("RUSTC", path)
|
||||
.spawn().expect("could not run cargo")
|
||||
.wait().expect("failed to wait for cargo?");
|
||||
|
||||
if let Some(code) = exit_status.code() {
|
||||
std::process::exit(code);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue