Skip to content

Commit

Permalink
Move project_root function to clippy_dev/src/lib.rs
Browse files Browse the repository at this point in the history
This allows us to use the method in both `fmt.rs` and `lib.rs` in
multiple places. The downside is that we panic inside the method now,
instead of using the error handling in `fmt.rs`. We may want to
centralize the error handling for clippy_dev at some point, though.
  • Loading branch information
phansch committed Jan 30, 2020
1 parent 4d1a11d commit 3036a2c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 30 deletions.
28 changes: 3 additions & 25 deletions clippy_dev/src/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
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;

#[derive(Debug)]
pub enum CliError {
CommandFailed(String),
IoError(io::Error),
ProjectRootNotFound,
RustfmtNotInstalled,
WalkDirError(walkdir::Error),
}
Expand All @@ -35,7 +35,7 @@ pub fn run(check: bool, verbose: bool) {
fn try_run(context: &FmtContext) -> Result<bool, CliError> {
let mut success = true;

let project_root = project_root()?;
let project_root = clippy_project_root();

rustfmt_test(context)?;

Expand Down Expand Up @@ -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.");
},
Expand Down Expand Up @@ -176,22 +173,3 @@ fn rustfmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
}
Ok(success)
}

fn project_root() -> Result<PathBuf, CliError> {
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)
}
23 changes: 18 additions & 5 deletions clippy_dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ fn parse_contents(content: &str, filename: &str) -> impl Iterator<Item = Lint> {
fn lint_files() -> impl Iterator<Item = walkdir::DirEntry> {
// 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)
Expand Down Expand Up @@ -237,7 +237,7 @@ pub fn replace_region_in_file<F>(
where
F: Fn() -> Vec<String>,
{
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)
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 3036a2c

Please sign in to comment.