Skip to content

Commit 762aff2

Browse files
committed
Merge pull request #960 from oli-obk/libbin
don't require `cargo clippy` to pass a `--lib` or `--bin x` argument
2 parents cbc430a + 41e71b4 commit 762aff2

File tree

8 files changed

+127
-15
lines changed

8 files changed

+127
-15
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ script:
2020
- cargo build --features debugging
2121
- cargo test --features debugging
2222
- SYSROOT=~/rust cargo install
23-
- cargo clippy --lib -- -D clippy
23+
- cargo clippy -- -D clippy
24+
- cd clippy_lints && cargo clippy -- -D clippy && cd ..
2425

2526
after_success:
2627
# only test regex_macros if it compiles

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ quine-mc_cluskey = "0.2.2"
3232
# begin automatic update
3333
clippy_lints = { version = "0.0.70", path = "clippy_lints" }
3434
# end automatic update
35+
rustc-serialize = "0.3"
3536

3637
[dev-dependencies]
3738
compiletest_rs = "0.1.0"
3839
lazy_static = "0.1.15"
3940
regex = "0.1.56"
40-
rustc-serialize = "0.3"
4141

4242
[features]
4343
debugging = []

clippy_lints/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ semver = "0.2.1"
2121
toml = "0.1"
2222
unicode-normalization = "0.1"
2323
quine-mc_cluskey = "0.2.2"
24+
rustc-serialize = "0.3"
2425

2526
[features]
2627
debugging = []

clippy_lints/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ extern crate regex_syntax;
3636
// for finding minimal boolean expressions
3737
extern crate quine_mc_cluskey;
3838

39+
extern crate rustc_serialize;
40+
3941
extern crate rustc_plugin;
4042
extern crate rustc_const_eval;
4143
extern crate rustc_const_math;

clippy_lints/src/utils/cargo.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
use std::collections::HashMap;
2+
use std::process::Command;
3+
use std::str::{from_utf8, Utf8Error};
4+
use std::io;
5+
use rustc_serialize::json;
6+
7+
#[derive(RustcDecodable, Debug)]
8+
pub struct Metadata {
9+
pub packages: Vec<Package>,
10+
resolve: Option<()>,
11+
pub version: usize,
12+
}
13+
14+
#[derive(RustcDecodable, Debug)]
15+
pub struct Package {
16+
pub name: String,
17+
pub version: String,
18+
id: String,
19+
source: Option<()>,
20+
pub dependencies: Vec<Dependency>,
21+
pub targets: Vec<Target>,
22+
features: HashMap<String, Vec<String>>,
23+
manifest_path: String,
24+
}
25+
26+
#[derive(RustcDecodable, Debug)]
27+
pub struct Dependency {
28+
pub name: String,
29+
source: Option<String>,
30+
pub req: String,
31+
kind: Option<String>,
32+
optional: bool,
33+
uses_default_features: bool,
34+
features: Vec<HashMap<String, String>>,
35+
target: Option<()>,
36+
}
37+
38+
#[allow(non_camel_case_types)]
39+
#[derive(RustcDecodable, Debug)]
40+
pub enum Kind {
41+
dylib,
42+
test,
43+
bin,
44+
lib,
45+
}
46+
47+
#[derive(RustcDecodable, Debug)]
48+
pub struct Target {
49+
pub name: String,
50+
pub kind: Vec<Kind>,
51+
src_path: String,
52+
}
53+
54+
#[derive(Debug)]
55+
pub enum Error {
56+
Io(io::Error),
57+
Utf8(Utf8Error),
58+
Json(json::DecoderError),
59+
}
60+
61+
impl From<io::Error> for Error {
62+
fn from(err: io::Error) -> Self { Error::Io(err) }
63+
}
64+
impl From<Utf8Error> for Error {
65+
fn from(err: Utf8Error) -> Self { Error::Utf8(err) }
66+
}
67+
impl From<json::DecoderError> for Error {
68+
fn from(err: json::DecoderError) -> Self { Error::Json(err) }
69+
}
70+
71+
pub fn metadata() -> Result<Metadata, Error> {
72+
let output = Command::new("cargo").args(&["metadata", "--no-deps"]).output()?;
73+
let stdout = from_utf8(&output.stdout)?;
74+
Ok(json::decode(stdout)?)
75+
}

clippy_lints/src/utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub mod conf;
2323
mod hir;
2424
pub mod paths;
2525
pub use self::hir::{SpanlessEq, SpanlessHash};
26+
pub mod cargo;
2627

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

src/main.rs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
// error-pattern:yummy
22
#![feature(box_syntax)]
33
#![feature(rustc_private)]
4+
#![feature(slice_patterns)]
45

56
extern crate rustc_driver;
67
extern crate getopts;
78
extern crate rustc;
89
extern crate syntax;
910
extern crate rustc_plugin;
1011
extern crate clippy_lints;
12+
extern crate rustc_serialize;
1113

1214
use rustc_driver::{driver, CompilerCalls, RustcDefaultCalls, Compilation};
1315
use rustc::session::{config, Session};
@@ -16,6 +18,8 @@ use syntax::diagnostics;
1618
use std::path::PathBuf;
1719
use std::process::Command;
1820

21+
use clippy_lints::utils::cargo;
22+
1923
struct ClippyCompilerCalls(RustcDefaultCalls);
2024

2125
impl std::default::Default for ClippyCompilerCalls {
@@ -118,16 +122,18 @@ pub fn main() {
118122
};
119123

120124
if let Some("clippy") = std::env::args().nth(1).as_ref().map(AsRef::as_ref) {
121-
let args = wrap_args(std::env::args().skip(2), dep_path, sys_root);
122-
let path = std::env::current_exe().expect("current executable path invalid");
123-
let exit_status = std::process::Command::new("cargo")
124-
.args(&args)
125-
.env("RUSTC", path)
126-
.spawn().expect("could not run cargo")
127-
.wait().expect("failed to wait for cargo?");
128-
129-
if let Some(code) = exit_status.code() {
130-
std::process::exit(code);
125+
let mut metadata = cargo::metadata().expect("could not obtain cargo metadata");
126+
assert_eq!(metadata.version, 1);
127+
for target in metadata.packages.remove(0).targets {
128+
let args = std::env::args().skip(2);
129+
assert_eq!(target.kind.len(), 1);
130+
match &target.kind[..] {
131+
[cargo::Kind::lib] |
132+
[cargo::Kind::dylib] => process(std::iter::once("--lib".to_owned()).chain(args), &dep_path, &sys_root),
133+
[cargo::Kind::bin] => process(vec!["--bin".to_owned(), target.name].into_iter().chain(args), &dep_path, &sys_root),
134+
// don't process tests and other stuff
135+
_ => {},
136+
}
131137
}
132138
} else {
133139
let args: Vec<String> = if env::args().any(|s| s == "--sysroot") {
@@ -145,7 +151,7 @@ pub fn main() {
145151
}
146152
}
147153

148-
fn wrap_args<P, I>(old_args: I, dep_path: P, sysroot: String) -> Vec<String>
154+
fn process<P, I>(old_args: I, dep_path: P, sysroot: &str)
149155
where P: AsRef<Path>, I: Iterator<Item=String> {
150156

151157
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>
161167
args.push("-L".to_owned());
162168
args.push(dep_path.as_ref().to_string_lossy().into_owned());
163169
args.push(String::from("--sysroot"));
164-
args.push(sysroot);
170+
args.push(sysroot.to_owned());
165171
args.push("-Zno-trans".to_owned());
166-
args
172+
173+
let path = std::env::current_exe().expect("current executable path invalid");
174+
let exit_status = std::process::Command::new("cargo")
175+
.args(&args)
176+
.env("RUSTC", path)
177+
.spawn().expect("could not run cargo")
178+
.wait().expect("failed to wait for cargo?");
179+
180+
if let Some(code) = exit_status.code() {
181+
std::process::exit(code);
182+
}
167183
}

tests/versioncheck.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
extern crate clippy_lints;
2+
use clippy_lints::utils::cargo;
3+
4+
#[test]
5+
fn check_that_clippy_lints_has_the_same_version_as_clippy() {
6+
let clippy_meta = cargo::metadata().expect("could not obtain cargo metadata");
7+
std::env::set_current_dir(std::env::current_dir().unwrap().join("clippy_lints")).unwrap();
8+
let clippy_lints_meta = cargo::metadata().expect("could not obtain cargo metadata");
9+
assert_eq!(clippy_lints_meta.packages[0].version, clippy_meta.packages[0].version);
10+
for package in &clippy_meta.packages[0].dependencies {
11+
if package.name == "clippy_lints" {
12+
assert_eq!(clippy_lints_meta.packages[0].version, package.req[1..]);
13+
return;
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)