Skip to content

Commit cf1fc5d

Browse files
authored
Merge pull request #1975 from bjgill/clippy--all
Implement `cargo clippy --all`
2 parents 6649cb3 + ddc733a commit cf1fc5d

File tree

2 files changed

+81
-70
lines changed

2 files changed

+81
-70
lines changed

.travis.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ script:
2828
- cargo test --features debugging
2929
- mkdir -p ~/rust/cargo/bin
3030
- cp target/debug/cargo-clippy ~/rust/cargo/bin/cargo-clippy
31-
- PATH=$PATH:~/rust/cargo/bin cargo clippy -- -D clippy
32-
- cd clippy_lints && PATH=$PATH:~/rust/cargo/bin cargo clippy -- -D clippy && cd ..
31+
- PATH=$PATH:~/rust/cargo/bin cargo clippy --all -- -D clippy
3332
- cd clippy_workspace_tests && PATH=$PATH:~/rust/cargo/bin cargo clippy -- -D clippy && cd ..
3433
- cd clippy_workspace_tests/src && PATH=$PATH:~/rust/cargo/bin cargo clippy -- -D clippy && cd ../..
3534
- cd clippy_workspace_tests/subcrate && PATH=$PATH:~/rust/cargo/bin cargo clippy -- -D clippy && cd ../..

src/main.rs

+80-68
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// error-pattern:yummy
22
#![feature(box_syntax)]
33
#![feature(rustc_private)]
4-
54
#![allow(unknown_lints, missing_docs_in_private_items)]
65

76
extern crate clippy_lints;
@@ -12,9 +11,9 @@ extern crate rustc_errors;
1211
extern crate rustc_plugin;
1312
extern crate syntax;
1413

15-
use rustc_driver::{driver, CompilerCalls, RustcDefaultCalls, Compilation};
16-
use rustc::session::{config, Session, CompileIncomplete};
17-
use rustc::session::config::{Input, ErrorOutputType};
14+
use rustc_driver::{driver, Compilation, CompilerCalls, RustcDefaultCalls};
15+
use rustc::session::{config, CompileIncomplete, Session};
16+
use rustc::session::config::{ErrorOutputType, Input};
1817
use std::collections::HashMap;
1918
use std::path::PathBuf;
2019
use std::process::{self, Command};
@@ -152,6 +151,7 @@ Common options:
152151
-h, --help Print this message
153152
--features Features to compile for the package
154153
-V, --version Print version info and exit
154+
--all Run over all packages in the current workspace
155155
156156
Other options are the same as `cargo rustc`.
157157
@@ -199,9 +199,9 @@ pub fn main() {
199199
if let Some("clippy") = std::env::args().nth(1).as_ref().map(AsRef::as_ref) {
200200
// this arm is executed on the initial call to `cargo clippy`
201201

202-
let manifest_path_arg = std::env::args().skip(2).find(|val| {
203-
val.starts_with("--manifest-path=")
204-
});
202+
let manifest_path_arg = std::env::args()
203+
.skip(2)
204+
.find(|val| val.starts_with("--manifest-path="));
205205

206206
let mut metadata =
207207
if let Ok(metadata) = cargo_metadata::metadata(manifest_path_arg.as_ref().map(AsRef::as_ref)) {
@@ -217,74 +217,86 @@ pub fn main() {
217217
.expect("manifest path could not be canonicalized")
218218
});
219219

220-
let package_index = {
221-
if let Some(manifest_path) = manifest_path {
222-
metadata.packages.iter().position(|package| {
223-
let package_manifest_path = Path::new(&package.manifest_path).canonicalize().expect(
224-
"package manifest path could not be canonicalized",
225-
);
226-
package_manifest_path == manifest_path
227-
})
228-
} else {
229-
let package_manifest_paths: HashMap<_, _> = metadata
230-
.packages
231-
.iter()
232-
.enumerate()
233-
.map(|(i, package)| {
220+
let packages = if std::env::args().any(|a| a == "--all") {
221+
metadata.packages
222+
} else {
223+
let package_index = {
224+
if let Some(manifest_path) = manifest_path {
225+
metadata.packages.iter().position(|package| {
234226
let package_manifest_path = Path::new(&package.manifest_path)
235-
.parent()
236-
.expect("could not find parent directory of package manifest")
237227
.canonicalize()
238-
.expect("package directory cannot be canonicalized");
239-
(package_manifest_path, i)
228+
.expect("package manifest path could not be canonicalized");
229+
package_manifest_path == manifest_path
240230
})
241-
.collect();
242-
243-
let current_dir = std::env::current_dir()
244-
.expect("could not read current directory")
245-
.canonicalize()
246-
.expect("current directory cannot be canonicalized");
247-
248-
let mut current_path: &Path = &current_dir;
249-
250-
// This gets the most-recent parent (the one that takes the fewest `cd ..`s to
251-
// reach).
252-
loop {
253-
if let Some(&package_index) = package_manifest_paths.get(current_path) {
254-
break Some(package_index);
255-
} else {
256-
// We'll never reach the filesystem root, because to get to this point in the
257-
// code
258-
// the call to `cargo_metadata::metadata` must have succeeded. So it's okay to
259-
// unwrap the current path's parent.
260-
current_path = current_path.parent().unwrap_or_else(|| {
261-
panic!("could not find parent of path {}", current_path.display())
262-
});
231+
} else {
232+
let package_manifest_paths: HashMap<_, _> = metadata
233+
.packages
234+
.iter()
235+
.enumerate()
236+
.map(|(i, package)| {
237+
let package_manifest_path = Path::new(&package.manifest_path)
238+
.parent()
239+
.expect("could not find parent directory of package manifest")
240+
.canonicalize()
241+
.expect("package directory cannot be canonicalized");
242+
(package_manifest_path, i)
243+
})
244+
.collect();
245+
246+
let current_dir = std::env::current_dir()
247+
.expect("could not read current directory")
248+
.canonicalize()
249+
.expect("current directory cannot be canonicalized");
250+
251+
let mut current_path: &Path = &current_dir;
252+
253+
// This gets the most-recent parent (the one that takes the fewest `cd ..`s to
254+
// reach).
255+
loop {
256+
if let Some(&package_index) = package_manifest_paths.get(current_path) {
257+
break Some(package_index);
258+
} else {
259+
// We'll never reach the filesystem root, because to get to this point in the
260+
// code
261+
// the call to `cargo_metadata::metadata` must have succeeded. So it's okay to
262+
// unwrap the current path's parent.
263+
current_path = current_path
264+
.parent()
265+
.unwrap_or_else(|| panic!("could not find parent of path {}", current_path.display()));
266+
}
263267
}
264268
}
265-
}
266-
}.expect("could not find matching package");
267-
268-
let package = metadata.packages.remove(package_index);
269-
for target in package.targets {
270-
let args = std::env::args().skip(2);
271-
if let Some(first) = target.kind.get(0) {
272-
if target.kind.len() > 1 || first.ends_with("lib") {
273-
if let Err(code) = process(std::iter::once("--lib".to_owned()).chain(args)) {
274-
std::process::exit(code);
275-
}
276-
} else if ["bin", "example", "test", "bench"].contains(&&**first) {
277-
if let Err(code) = process(
278-
vec![format!("--{}", first), target.name]
279-
.into_iter()
280-
.chain(args),
281-
)
282-
{
283-
std::process::exit(code);
269+
}.expect("could not find matching package");
270+
271+
vec![metadata.packages.remove(package_index)]
272+
};
273+
274+
for package in packages {
275+
let manifest_path = package.manifest_path;
276+
277+
for target in package.targets {
278+
let args = std::env::args()
279+
.skip(2)
280+
.filter(|a| a != "--all" && !a.starts_with("--manifest-path="));
281+
282+
let args = std::iter::once(format!("--manifest-path={}", manifest_path)).chain(args);
283+
if let Some(first) = target.kind.get(0) {
284+
if target.kind.len() > 1 || first.ends_with("lib") {
285+
if let Err(code) = process(std::iter::once("--lib".to_owned()).chain(args)) {
286+
std::process::exit(code);
287+
}
288+
} else if ["bin", "example", "test", "bench"].contains(&&**first) {
289+
if let Err(code) = process(
290+
vec![format!("--{}", first), target.name]
291+
.into_iter()
292+
.chain(args),
293+
) {
294+
std::process::exit(code);
295+
}
284296
}
297+
} else {
298+
panic!("badly formatted cargo metadata: target::kind is an empty array");
285299
}
286-
} else {
287-
panic!("badly formatted cargo metadata: target::kind is an empty array");
288300
}
289301
}
290302
} else {

0 commit comments

Comments
 (0)