Skip to content

Commit e624b77

Browse files
committed
Inline cargo_clippy_path and add a note about removing it when possible
1 parent ed1088e commit e624b77

File tree

2 files changed

+17
-29
lines changed

2 files changed

+17
-29
lines changed

clippy_dev/src/lint.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
use crate::utils::{cargo_clippy_path, cargo_cmd, run_exit_on_err};
2-
use std::fs;
3-
use std::process::{self, Command};
1+
use crate::utils::{ErrAction, cargo_cmd, expect_action, run_exit_on_err};
2+
use std::process::Command;
3+
use std::{env, fs};
44

5-
pub fn run<'a>(path: &str, edition: &str, args: impl Iterator<Item = &'a String>) {
6-
let is_file = match fs::metadata(path) {
7-
Ok(metadata) => metadata.is_file(),
8-
Err(e) => {
9-
eprintln!("Failed to read {path}: {e:?}");
10-
process::exit(1);
11-
},
12-
};
5+
#[cfg(not(windows))]
6+
static CARGO_CLIPPY_EXE: &str = "cargo-clippy";
7+
#[cfg(windows)]
8+
static CARGO_CLIPPY_EXE: &str = "cargo-clippy.exe";
139

10+
pub fn run<'a>(path: &str, edition: &str, args: impl Iterator<Item = &'a String>) {
11+
let is_file = expect_action(fs::metadata(path), ErrAction::Read, path).is_file();
1412
if is_file {
1513
run_exit_on_err(
1614
"cargo run",
@@ -25,10 +23,17 @@ pub fn run<'a>(path: &str, edition: &str, args: impl Iterator<Item = &'a String>
2523
.env("RUSTC_ICE", "0"),
2624
);
2725
} else {
26+
// Ideally this would just be `cargo run`, but the working directory needs to be
27+
// set to clippy's directory when building, and the target project's directory
28+
// when running clippy. `cargo` can only set a single working directory for both
29+
// when using `run`.
2830
run_exit_on_err("cargo build", cargo_cmd().arg("build"));
31+
32+
let mut exe = env::current_exe().expect("failed to get current executable name");
33+
exe.set_file_name(CARGO_CLIPPY_EXE);
2934
run_exit_on_err(
3035
"cargo clippy",
31-
Command::new(cargo_clippy_path())
36+
Command::new(exe)
3237
.arg("clippy")
3338
.args(args)
3439
// Prevent rustc from creating `rustc-ice-*` files the console output is enough.

clippy_dev/src/utils.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ use std::process::{self, Command, Stdio};
1212
use std::{env, thread};
1313
use walkdir::WalkDir;
1414

15-
#[cfg(not(windows))]
16-
static CARGO_CLIPPY_EXE: &str = "cargo-clippy";
17-
#[cfg(windows)]
18-
static CARGO_CLIPPY_EXE: &str = "cargo-clippy.exe";
19-
2015
#[derive(Clone, Copy)]
2116
pub enum ErrAction {
2217
Open,
@@ -118,18 +113,6 @@ impl<'a> File<'a> {
118113
}
119114
}
120115

121-
/// Returns the path to the `cargo-clippy` binary
122-
///
123-
/// # Panics
124-
///
125-
/// Panics if the path of current executable could not be retrieved.
126-
#[must_use]
127-
pub fn cargo_clippy_path() -> PathBuf {
128-
let mut path = env::current_exe().expect("failed to get current executable name");
129-
path.set_file_name(CARGO_CLIPPY_EXE);
130-
path
131-
}
132-
133116
/// Creates a `Command` for running cargo.
134117
#[must_use]
135118
pub fn cargo_cmd() -> Command {

0 commit comments

Comments
 (0)