From 956a98c0c7efbc7bb7dda33a2179dccc336a1bdd Mon Sep 17 00:00:00 2001 From: Vurich Date: Thu, 29 Jun 2017 12:57:28 +0200 Subject: [PATCH 1/2] Allow cargo-clippy to work in subdirectories --- src/main.rs | 56 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1b3dd4f05e0..6e4aa05480c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -158,6 +158,10 @@ fn show_version() { println!("{}", env!("CARGO_PKG_VERSION")); } +fn has_prefix<'a, T: PartialEq, I: Iterator>(v: &'a [T], itr: I) -> bool { + v.iter().zip(itr).all(|(a, b)| a == b) +} + pub fn main() { use std::env; @@ -192,26 +196,48 @@ pub fn main() { let manifest_path = manifest_path_arg.map(|arg| PathBuf::from(Path::new(&arg["--manifest-path=".len()..]))); - let current_dir = std::env::current_dir(); + let package_index = { + let mut iterator = metadata.packages.iter(); - let package_index = metadata - .packages - .iter() - .position(|package| { - let package_manifest_path = Path::new(&package.manifest_path); if let Some(ref manifest_path) = manifest_path { - package_manifest_path == manifest_path + iterator.position(|package| { + let package_manifest_path = Path::new(&package.manifest_path); + package_manifest_path == manifest_path + }) } else { - let current_dir = current_dir - .as_ref() - .expect("could not read current directory"); - let package_manifest_directory = package_manifest_path - .parent() - .expect("could not find parent directory of package manifest"); - package_manifest_directory == current_dir + let current_dir = std::env::current_dir() + .expect("could not read current directory") + .canonicalize() + .expect("current directory cannot be canonicalized"); + let current_dir_components = current_dir.components().collect::>(); + + // This gets the most-recent parent (the one that takes the fewest `cd ..`s to + // reach). + iterator + .enumerate() + .filter_map(|(i, package)| { + let package_manifest_path = Path::new(&package.manifest_path); + let canonical_path = package_manifest_path + .parent() + .expect("could not find parent directory of package manifest") + .canonicalize() + .expect("package directory cannot be canonicalized"); + + // TODO: We can do this in `O(1)` by combining the `len` and the + // iteration. + let components = canonical_path.components().collect::>(); + if has_prefix(¤t_dir_components, components.iter()) { + Some((i, components.len())) + } else { + None + } + }) + .max_by_key(|&(_, length)| length) + .map(|(i, _)| i) } - }) + } .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); From 01bb0f9e51711248914ce78ed9f353c93c80c6b8 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Tue, 4 Jul 2017 16:07:33 +0200 Subject: [PATCH 2/2] ignore needless_lifetimes false positive --- src/main.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main.rs b/src/main.rs index 6e4aa05480c..df39b5b9c1d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -158,6 +158,8 @@ fn show_version() { println!("{}", env!("CARGO_PKG_VERSION")); } +// FIXME: false positive for needless_lifetimes +#[allow(needless_lifetimes)] fn has_prefix<'a, T: PartialEq, I: Iterator>(v: &'a [T], itr: I) -> bool { v.iter().zip(itr).all(|(a, b)| a == b) }