X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=clippy_dev%2Fsrc%2Ffmt.rs;h=a6043c4be0dc355969e8a144fe0abf1ab8f88cd5;hb=91a1cd562a1d19a4a5fba6c7905b5d8c3051da85;hp=3c90aa4b50759e57dceeb048d94d9c249c51db82;hpb=19c58d260bca9702a4fb4079e360b61248ac8738;p=rust.git diff --git a/clippy_dev/src/fmt.rs b/clippy_dev/src/fmt.rs index 3c90aa4b507..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 @@ 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 @@ fn output_err(err: CliError) { 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."); }, @@ -88,7 +85,7 @@ fn output_err(err: CliError) { Ok(false) => { eprintln!(); eprintln!("Formatting check failed."); - eprintln!("Run `./util/dev fmt` to update formatting."); + eprintln!("Run `cargo dev fmt` to update formatting."); 1 }, Err(err) => { @@ -137,13 +134,13 @@ fn cargo_fmt(context: &FmtContext, path: &Path) -> Result { args.push("--"); args.push("--check"); } - let success = exec(context, &bin_path("cargo"), path, &args)?; + let success = exec(context, "cargo", path, &args)?; Ok(success) } fn rustfmt_test(context: &FmtContext) -> Result<(), CliError> { - let program = bin_path("rustfmt"); + let program = "rustfmt"; let dir = std::env::current_dir()?; let args = &["+nightly", "--version"]; @@ -170,37 +167,9 @@ fn rustfmt(context: &FmtContext, path: &Path) -> Result { if context.check { args.push("--check".as_ref()); } - let success = exec(context, &bin_path("rustfmt"), std::env::current_dir()?, &args)?; + let success = exec(context, "rustfmt", std::env::current_dir()?, &args)?; if !success { eprintln!("rustfmt failed on {}", path.display()); } 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) -} - -// Workaround for https://github.com/rust-lang/cargo/issues/7475. -// FIXME: replace `&bin_path("command")` with `"command"` once the issue is fixed -fn bin_path(bin: &str) -> String { - let mut p = home::cargo_home().unwrap(); - p.push("bin"); - p.push(bin); - p.display().to_string() -}