Skip to content

Commit

Permalink
Merge pull request #960 from oli-obk/libbin
Browse files Browse the repository at this point in the history
don't require `cargo clippy` to pass a `--lib` or `--bin x` argument
  • Loading branch information
llogiq committed May 31, 2016
2 parents cbc430a + 41e71b4 commit 762aff2
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ script:
- cargo build --features debugging
- cargo test --features debugging
- SYSROOT=~/rust cargo install
- cargo clippy --lib -- -D clippy
- cargo clippy -- -D clippy
- cd clippy_lints && cargo clippy -- -D clippy && cd ..

after_success:
# only test regex_macros if it compiles
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ semver = "0.2.1"
toml = "0.1"
unicode-normalization = "0.1"
quine-mc_cluskey = "0.2.2"
rustc-serialize = "0.3"

[features]
debugging = []
2 changes: 2 additions & 0 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ extern crate regex_syntax;
// for finding minimal boolean expressions
extern crate quine_mc_cluskey;

extern crate rustc_serialize;

extern crate rustc_plugin;
extern crate rustc_const_eval;
extern crate rustc_const_math;
Expand Down
75 changes: 75 additions & 0 deletions clippy_lints/src/utils/cargo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use std::collections::HashMap;
use std::process::Command;
use std::str::{from_utf8, Utf8Error};
use std::io;
use rustc_serialize::json;

#[derive(RustcDecodable, Debug)]
pub struct Metadata {
pub packages: Vec<Package>,
resolve: Option<()>,
pub version: usize,
}

#[derive(RustcDecodable, Debug)]
pub struct Package {
pub name: String,
pub version: String,
id: String,
source: Option<()>,
pub dependencies: Vec<Dependency>,
pub targets: Vec<Target>,
features: HashMap<String, Vec<String>>,
manifest_path: String,
}

#[derive(RustcDecodable, Debug)]
pub struct Dependency {
pub name: String,
source: Option<String>,
pub 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,
}

#[derive(Debug)]
pub enum Error {
Io(io::Error),
Utf8(Utf8Error),
Json(json::DecoderError),
}

impl From<io::Error> for Error {
fn from(err: io::Error) -> Self { Error::Io(err) }
}
impl From<Utf8Error> for Error {
fn from(err: Utf8Error) -> Self { Error::Utf8(err) }
}
impl From<json::DecoderError> for Error {
fn from(err: json::DecoderError) -> Self { Error::Json(err) }
}

pub fn metadata() -> Result<Metadata, Error> {
let output = Command::new("cargo").args(&["metadata", "--no-deps"]).output()?;
let stdout = from_utf8(&output.stdout)?;
Ok(json::decode(stdout)?)
}
1 change: 1 addition & 0 deletions clippy_lints/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub mod conf;
mod hir;
pub mod paths;
pub use self::hir::{SpanlessEq, SpanlessHash};
pub mod cargo;

pub type MethodArgs = HirVec<P<Expr>>;

Expand Down
42 changes: 29 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// error-pattern:yummy
#![feature(box_syntax)]
#![feature(rustc_private)]
#![feature(slice_patterns)]

extern crate rustc_driver;
extern crate getopts;
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};
Expand All @@ -16,6 +18,8 @@ use syntax::diagnostics;
use std::path::PathBuf;
use std::process::Command;

use clippy_lints::utils::cargo;

struct ClippyCompilerCalls(RustcDefaultCalls);

impl std::default::Default for ClippyCompilerCalls {
Expand Down Expand Up @@ -118,16 +122,18 @@ 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 mut metadata = cargo::metadata().expect("could not obtain cargo metadata");
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[..] {
[cargo::Kind::lib] |
[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 and other stuff
_ => {},
}
}
} else {
let args: Vec<String> = if env::args().any(|s| s == "--sysroot") {
Expand All @@ -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()];
Expand All @@ -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);
}
}
16 changes: 16 additions & 0 deletions tests/versioncheck.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
extern crate clippy_lints;
use clippy_lints::utils::cargo;

#[test]
fn check_that_clippy_lints_has_the_same_version_as_clippy() {
let clippy_meta = cargo::metadata().expect("could not obtain cargo metadata");
std::env::set_current_dir(std::env::current_dir().unwrap().join("clippy_lints")).unwrap();
let clippy_lints_meta = cargo::metadata().expect("could not obtain cargo metadata");
assert_eq!(clippy_lints_meta.packages[0].version, clippy_meta.packages[0].version);
for package in &clippy_meta.packages[0].dependencies {
if package.name == "clippy_lints" {
assert_eq!(clippy_lints_meta.packages[0].version, package.req[1..]);
return;
}
}
}

0 comments on commit 762aff2

Please sign in to comment.