]> 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 5ccdbec14288c78bd79a6853d02bfb89512e6ae5..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;
 
 pub enum CliError {
     CommandFailed(String),
     IoError(io::Error),
-    ProjectRootNotFound,
+    RustfmtNotInstalled,
     WalkDirError(walkdir::Error),
 }
 
 impl From<io::Error> for CliError {
     fn from(error: io::Error) -> Self {
-        CliError::IoError(error)
+        Self::IoError(error)
     }
 }
 
 impl From<walkdir::Error> for CliError {
     fn from(error: walkdir::Error) -> Self {
-        CliError::WalkDirError(error)
+        Self::WalkDirError(error)
     }
 }
 
@@ -34,7 +35,9 @@ 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)?;
 
         success &= cargo_fmt(context, project_root.as_path())?;
         success &= cargo_fmt(context, &project_root.join("clippy_dev"))?;
@@ -66,8 +69,8 @@ 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.");
             },
             CliError::WalkDirError(err) => {
                 eprintln!("error: {}", err);
@@ -82,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) => {
@@ -94,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 {} && {} {}",
@@ -139,6 +139,29 @@ fn cargo_fmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
     Ok(success)
 }
 
+fn rustfmt_test(context: &FmtContext) -> Result<(), CliError> {
+    let program = "rustfmt";
+    let dir = std::env::current_dir()?;
+    let args = &["+nightly", "--version"];
+
+    if context.verbose {
+        println!("{}", format_command(&program, &dir, args));
+    }
+
+    let output = Command::new(&program).current_dir(&dir).args(args.iter()).output()?;
+
+    if output.status.success() {
+        Ok(())
+    } else if std::str::from_utf8(&output.stderr)
+        .unwrap_or("")
+        .starts_with("error: 'rustfmt' is not installed")
+    {
+        Err(CliError::RustfmtNotInstalled)
+    } else {
+        Err(CliError::CommandFailed(format_command(&program, &dir, args)))
+    }
+}
+
 fn rustfmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
     let mut args = vec!["+nightly".as_ref(), path.as_os_str()];
     if context.check {
@@ -150,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)
-}