Skip to content

Implement cargo clippy --all #1975

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ script:
- cargo test --features debugging
- mkdir -p ~/rust/cargo/bin
- cp target/debug/cargo-clippy ~/rust/cargo/bin/cargo-clippy
- PATH=$PATH:~/rust/cargo/bin cargo clippy -- -D clippy
- cd clippy_lints && PATH=$PATH:~/rust/cargo/bin cargo clippy -- -D clippy && cd ..
- PATH=$PATH:~/rust/cargo/bin cargo clippy --all -- -D clippy
- cd clippy_workspace_tests && PATH=$PATH:~/rust/cargo/bin cargo clippy -- -D clippy && cd ..
- cd clippy_workspace_tests/src && PATH=$PATH:~/rust/cargo/bin cargo clippy -- -D clippy && cd ../..
- cd clippy_workspace_tests/subcrate && PATH=$PATH:~/rust/cargo/bin cargo clippy -- -D clippy && cd ../..
Expand Down
148 changes: 80 additions & 68 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// error-pattern:yummy
#![feature(box_syntax)]
#![feature(rustc_private)]

#![allow(unknown_lints, missing_docs_in_private_items)]

extern crate clippy_lints;
Expand All @@ -12,9 +11,9 @@ extern crate rustc_errors;
extern crate rustc_plugin;
extern crate syntax;

use rustc_driver::{driver, CompilerCalls, RustcDefaultCalls, Compilation};
use rustc::session::{config, Session, CompileIncomplete};
use rustc::session::config::{Input, ErrorOutputType};
use rustc_driver::{driver, Compilation, CompilerCalls, RustcDefaultCalls};
use rustc::session::{config, CompileIncomplete, Session};
use rustc::session::config::{ErrorOutputType, Input};
use std::collections::HashMap;
use std::path::PathBuf;
use std::process::{self, Command};
Expand Down Expand Up @@ -152,6 +151,7 @@ Common options:
-h, --help Print this message
--features Features to compile for the package
-V, --version Print version info and exit
--all Run over all packages in the current workspace

Other options are the same as `cargo rustc`.

Expand Down Expand Up @@ -199,9 +199,9 @@ pub fn main() {
if let Some("clippy") = std::env::args().nth(1).as_ref().map(AsRef::as_ref) {
// this arm is executed on the initial call to `cargo clippy`

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

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

let package_index = {
if let Some(manifest_path) = manifest_path {
metadata.packages.iter().position(|package| {
let package_manifest_path = Path::new(&package.manifest_path).canonicalize().expect(
"package manifest path could not be canonicalized",
);
package_manifest_path == manifest_path
})
} else {
let package_manifest_paths: HashMap<_, _> = metadata
.packages
.iter()
.enumerate()
.map(|(i, package)| {
let packages = if std::env::args().any(|a| a == "--all") {
metadata.packages
} else {
let package_index = {
if let Some(manifest_path) = manifest_path {
metadata.packages.iter().position(|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)
.expect("package manifest path could not be canonicalized");
package_manifest_path == manifest_path
})
.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 = &current_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.
current_path = current_path.parent().unwrap_or_else(|| {
panic!("could not find parent of path {}", current_path.display())
});
} 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 = &current_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.
current_path = current_path
.parent()
.unwrap_or_else(|| panic!("could not find parent of path {}", current_path.display()));
}
}
}
}
}.expect("could not find matching package");

let package = metadata.packages.remove(package_index);
for target in package.targets {
let args = std::env::args().skip(2);
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),
)
{
std::process::exit(code);
}.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 {
let args = std::env::args()
.skip(2)
.filter(|a| a != "--all" && !a.starts_with("--manifest-path="));

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),
) {
std::process::exit(code);
}
}
} else {
panic!("badly formatted cargo metadata: target::kind is an empty array");
}
} else {
panic!("badly formatted cargo metadata: target::kind is an empty array");
}
}
} else {
Expand Down