]> git.lizzy.rs Git - rust.git/blobdiff - clippy_dev/src/fmt.rs
Merge branch 'master' of github.com:rust-lang/rust-clippy
[rust.git] / clippy_dev / src / fmt.rs
index 1f4925db1befb00e84302eef22f5a8b04cce5c9e..a6043c4be0dc355969e8a144fe0abf1ab8f88cd5 100644 (file)
@@ -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<bool, CliError> {
         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) => {
@@ -100,10 +97,7 @@ fn output_err(err: CliError) {
 }
 
 fn format_command(program: impl AsRef<OsStr>, dir: impl AsRef<Path>, args: &[impl AsRef<OsStr>]) -> String {
-    let arg_display: Vec<_> = args
-        .iter()
-        .map(|a| escape(a.as_ref().to_string_lossy()).to_owned())
-        .collect();
+    let arg_display: Vec<_> = args.iter().map(|a| escape(a.as_ref().to_string_lossy())).collect();
 
     format!(
         "cd {} && {} {}",
@@ -140,13 +134,13 @@ fn cargo_fmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
         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"];
 
@@ -173,37 +167,9 @@ fn rustfmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
     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<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)
-}
-
-// 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()
-}