Skip to content

Commit 3036a2c

Browse files
committed
Move project_root function to clippy_dev/src/lib.rs
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.
1 parent 4d1a11d commit 3036a2c

File tree

2 files changed

+21
-30
lines changed

2 files changed

+21
-30
lines changed

clippy_dev/src/fmt.rs

+3-25
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1+
use clippy_dev::clippy_project_root;
12
use shell_escape::escape;
23
use std::ffi::OsStr;
34
use std::io;
4-
use std::path::{Path, PathBuf};
5+
use std::path::Path;
56
use std::process::{self, Command};
67
use walkdir::WalkDir;
78

89
#[derive(Debug)]
910
pub enum CliError {
1011
CommandFailed(String),
1112
IoError(io::Error),
12-
ProjectRootNotFound,
1313
RustfmtNotInstalled,
1414
WalkDirError(walkdir::Error),
1515
}
@@ -35,7 +35,7 @@ pub fn run(check: bool, verbose: bool) {
3535
fn try_run(context: &FmtContext) -> Result<bool, CliError> {
3636
let mut success = true;
3737

38-
let project_root = project_root()?;
38+
let project_root = clippy_project_root();
3939

4040
rustfmt_test(context)?;
4141

@@ -69,9 +69,6 @@ pub fn run(check: bool, verbose: bool) {
6969
CliError::IoError(err) => {
7070
eprintln!("error: {}", err);
7171
},
72-
CliError::ProjectRootNotFound => {
73-
eprintln!("error: Can't determine root of project. Please run inside a Clippy working dir.");
74-
},
7572
CliError::RustfmtNotInstalled => {
7673
eprintln!("error: rustfmt nightly is not installed.");
7774
},
@@ -176,22 +173,3 @@ fn rustfmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
176173
}
177174
Ok(success)
178175
}
179-
180-
fn project_root() -> Result<PathBuf, CliError> {
181-
let current_dir = std::env::current_dir()?;
182-
for path in current_dir.ancestors() {
183-
let result = std::fs::read_to_string(path.join("Cargo.toml"));
184-
if let Err(err) = &result {
185-
if err.kind() == io::ErrorKind::NotFound {
186-
continue;
187-
}
188-
}
189-
190-
let content = result?;
191-
if content.contains("[package]\nname = \"clippy\"") {
192-
return Ok(path.to_path_buf());
193-
}
194-
}
195-
196-
Err(CliError::ProjectRootNotFound)
197-
}

clippy_dev/src/lib.rs

+18-5
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ fn parse_contents(content: &str, filename: &str) -> impl Iterator<Item = Lint> {
206206
fn lint_files() -> impl Iterator<Item = walkdir::DirEntry> {
207207
// We use `WalkDir` instead of `fs::read_dir` here in order to recurse into subdirectories.
208208
// Otherwise we would not collect all the lints, for example in `clippy_lints/src/methods/`.
209-
let path = clippy_project_dir().join("clippy_lints/src");
209+
let path = clippy_project_root().join("clippy_lints/src");
210210
WalkDir::new(path)
211211
.into_iter()
212212
.filter_map(std::result::Result::ok)
@@ -237,7 +237,7 @@ pub fn replace_region_in_file<F>(
237237
where
238238
F: Fn() -> Vec<String>,
239239
{
240-
let path = clippy_project_dir().join(path);
240+
let path = clippy_project_root().join(path);
241241
let mut f = fs::File::open(&path).expect(&format!("File not found: {}", path.to_string_lossy()));
242242
let mut contents = String::new();
243243
f.read_to_string(&mut contents)
@@ -322,9 +322,22 @@ where
322322
}
323323

324324
/// Returns the path to the Clippy project directory
325-
fn clippy_project_dir() -> PathBuf {
326-
let clippy_dev_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
327-
clippy_dev_dir.parent().unwrap().to_path_buf()
325+
pub fn clippy_project_root() -> PathBuf {
326+
let current_dir = std::env::current_dir().unwrap();
327+
for path in current_dir.ancestors() {
328+
let result = std::fs::read_to_string(path.join("Cargo.toml"));
329+
if let Err(err) = &result {
330+
if err.kind() == std::io::ErrorKind::NotFound {
331+
continue;
332+
}
333+
}
334+
335+
let content = result.unwrap();
336+
if content.contains("[package]\nname = \"clippy\"") {
337+
return path.to_path_buf();
338+
}
339+
}
340+
panic!("error: Can't determine root of project. Please run inside a Clippy working dir.");
328341
}
329342

330343
#[test]

0 commit comments

Comments
 (0)