about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--clippy_dev/src/lint.rs29
-rw-r--r--clippy_dev/src/utils.rs17
2 files changed, 17 insertions, 29 deletions
diff --git a/clippy_dev/src/lint.rs b/clippy_dev/src/lint.rs
index d8d3e39116b..2d9f563cdae 100644
--- a/clippy_dev/src/lint.rs
+++ b/clippy_dev/src/lint.rs
@@ -1,16 +1,14 @@
-use crate::utils::{cargo_clippy_path, cargo_cmd, run_exit_on_err};
-use std::fs;
-use std::process::{self, Command};
+use crate::utils::{ErrAction, cargo_cmd, expect_action, run_exit_on_err};
+use std::process::Command;
+use std::{env, fs};
 
-pub fn run<'a>(path: &str, edition: &str, args: impl Iterator<Item = &'a String>) {
-    let is_file = match fs::metadata(path) {
-        Ok(metadata) => metadata.is_file(),
-        Err(e) => {
-            eprintln!("Failed to read {path}: {e:?}");
-            process::exit(1);
-        },
-    };
+#[cfg(not(windows))]
+static CARGO_CLIPPY_EXE: &str = "cargo-clippy";
+#[cfg(windows)]
+static CARGO_CLIPPY_EXE: &str = "cargo-clippy.exe";
 
+pub fn run<'a>(path: &str, edition: &str, args: impl Iterator<Item = &'a String>) {
+    let is_file = expect_action(fs::metadata(path), ErrAction::Read, path).is_file();
     if is_file {
         run_exit_on_err(
             "cargo run",
@@ -25,10 +23,17 @@ pub fn run<'a>(path: &str, edition: &str, args: impl Iterator<Item = &'a String>
                 .env("RUSTC_ICE", "0"),
         );
     } else {
+        // Ideally this would just be `cargo run`, but the working directory needs to be
+        // set to clippy's directory when building, and the target project's directory
+        // when running clippy. `cargo` can only set a single working directory for both
+        // when using `run`.
         run_exit_on_err("cargo build", cargo_cmd().arg("build"));
+
+        let mut exe = env::current_exe().expect("failed to get current executable name");
+        exe.set_file_name(CARGO_CLIPPY_EXE);
         run_exit_on_err(
             "cargo clippy",
-            Command::new(cargo_clippy_path())
+            Command::new(exe)
                 .arg("clippy")
                 .args(args)
                 // Prevent rustc from creating `rustc-ice-*` files the console output is enough.
diff --git a/clippy_dev/src/utils.rs b/clippy_dev/src/utils.rs
index 0c5020c6982..dfa2d186097 100644
--- a/clippy_dev/src/utils.rs
+++ b/clippy_dev/src/utils.rs
@@ -12,11 +12,6 @@ use std::process::{self, Command, Stdio};
 use std::{env, thread};
 use walkdir::WalkDir;
 
-#[cfg(not(windows))]
-static CARGO_CLIPPY_EXE: &str = "cargo-clippy";
-#[cfg(windows)]
-static CARGO_CLIPPY_EXE: &str = "cargo-clippy.exe";
-
 #[derive(Clone, Copy)]
 pub enum ErrAction {
     Open,
@@ -118,18 +113,6 @@ impl<'a> File<'a> {
     }
 }
 
-/// Returns the path to the `cargo-clippy` binary
-///
-/// # Panics
-///
-/// Panics if the path of current executable could not be retrieved.
-#[must_use]
-pub fn cargo_clippy_path() -> PathBuf {
-    let mut path = env::current_exe().expect("failed to get current executable name");
-    path.set_file_name(CARGO_CLIPPY_EXE);
-    path
-}
-
 /// Creates a `Command` for running cargo.
 #[must_use]
 pub fn cargo_cmd() -> Command {