diff --git a/clippy_dev/src/fmt.rs b/clippy_dev/src/fmt.rs index 0d950d5b9fe..a6043c4be0d 100644 --- a/clippy_dev/src/fmt.rs +++ b/clippy_dev/src/fmt.rs @@ -1,7 +1,8 @@ +use clippy_dev::clippy_project_root; use shell_escape::escape; use std::ffi::OsStr; use std::io; -use std::path::{Path, PathBuf}; +use std::path::Path; use std::process::{self, Command}; use walkdir::WalkDir; @@ -9,7 +10,6 @@ use walkdir::WalkDir; pub enum CliError { CommandFailed(String), IoError(io::Error), - ProjectRootNotFound, RustfmtNotInstalled, WalkDirError(walkdir::Error), } @@ -35,7 +35,7 @@ pub fn run(check: bool, verbose: bool) { fn try_run(context: &FmtContext) -> Result { let mut success = true; - let project_root = project_root()?; + let project_root = clippy_project_root(); rustfmt_test(context)?; @@ -69,9 +69,6 @@ pub fn run(check: bool, verbose: bool) { CliError::IoError(err) => { eprintln!("error: {}", err); }, - CliError::ProjectRootNotFound => { - eprintln!("error: Can't determine root of project. Please run inside a Clippy working dir."); - }, CliError::RustfmtNotInstalled => { eprintln!("error: rustfmt nightly is not installed."); }, @@ -176,22 +173,3 @@ fn rustfmt(context: &FmtContext, path: &Path) -> Result { } Ok(success) } - -fn project_root() -> Result { - let current_dir = std::env::current_dir()?; - for path in current_dir.ancestors() { - let result = std::fs::read_to_string(path.join("Cargo.toml")); - if let Err(err) = &result { - if err.kind() == io::ErrorKind::NotFound { - continue; - } - } - - let content = result?; - if content.contains("[package]\nname = \"clippy\"") { - return Ok(path.to_path_buf()); - } - } - - Err(CliError::ProjectRootNotFound) -} diff --git a/clippy_dev/src/lib.rs b/clippy_dev/src/lib.rs index 1aa34ce651f..efc1a9675da 100644 --- a/clippy_dev/src/lib.rs +++ b/clippy_dev/src/lib.rs @@ -206,7 +206,7 @@ fn parse_contents(content: &str, filename: &str) -> impl Iterator { fn lint_files() -> impl Iterator { // We use `WalkDir` instead of `fs::read_dir` here in order to recurse into subdirectories. // Otherwise we would not collect all the lints, for example in `clippy_lints/src/methods/`. - let path = clippy_project_dir().join("clippy_lints/src"); + let path = clippy_project_root().join("clippy_lints/src"); WalkDir::new(path) .into_iter() .filter_map(std::result::Result::ok) @@ -237,7 +237,7 @@ pub fn replace_region_in_file( where F: Fn() -> Vec, { - let path = clippy_project_dir().join(path); + let path = clippy_project_root().join(path); let mut f = fs::File::open(&path).expect(&format!("File not found: {}", path.to_string_lossy())); let mut contents = String::new(); f.read_to_string(&mut contents) @@ -322,9 +322,22 @@ where } /// Returns the path to the Clippy project directory -fn clippy_project_dir() -> PathBuf { - let clippy_dev_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap()); - clippy_dev_dir.parent().unwrap().to_path_buf() +pub fn clippy_project_root() -> PathBuf { + let current_dir = std::env::current_dir().unwrap(); + for path in current_dir.ancestors() { + let result = std::fs::read_to_string(path.join("Cargo.toml")); + if let Err(err) = &result { + if err.kind() == std::io::ErrorKind::NotFound { + continue; + } + } + + let content = result.unwrap(); + if content.contains("[package]\nname = \"clippy\"") { + return path.to_path_buf(); + } + } + panic!("error: Can't determine root of project. Please run inside a Clippy working dir."); } #[test]