Skip to content

Commit 149bbd6

Browse files
committed
Inline cargo_clippy_path and add a note about removing it when possible
1 parent 2396533 commit 149bbd6

File tree

2 files changed

+27
-31
lines changed

2 files changed

+27
-31
lines changed

clippy_dev/src/lint.rs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
use crate::utils::{cargo_clippy_path, cargo_cmd, run_success};
2-
use std::fs;
3-
use std::process::{self, Command};
1+
use crate::utils::{ErrAction, cargo_cmd, expect_action, run_success};
2+
use std::path::PathBuf;
3+
use std::process::Command;
4+
use std::{env, fs};
45

56
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-
};
13-
7+
let is_file = expect_action(fs::metadata(path), ErrAction::Read, path).is_file();
148
if is_file {
159
run_success(
1610
"cargo run",
@@ -25,10 +19,29 @@ pub fn run<'a>(path: &str, edition: &str, args: impl Iterator<Item = &'a String>
2519
.env("RUSTC_ICE", "0"),
2620
);
2721
} else {
28-
run_success("cargo build", cargo_cmd().arg("build"));
22+
// FIXME: This should be using `cargo run`, but can't since cargo loads the config from the
23+
// current directory. Either we need a way to change the current directory between building
24+
// running `cargo-clippy` as this currently does by running in two steps, or we need a way
25+
// to change where cargo loads the config.
26+
// See https://github.com/rust-lang/cargo/issues/9769
27+
28+
run_success("cargo build", cargo_cmd().args(["build", "--bin", "cargo-clippy"]));
29+
30+
let mut exe = match env::current_exe() {
31+
Ok(mut exe) => {
32+
exe.pop();
33+
exe
34+
},
35+
Err(_) => PathBuf::from("target/debug"),
36+
};
37+
#[cfg(windows)]
38+
exe.push("cargo-clippy.exe");
39+
#[cfg(not(windows))]
40+
exe.push("cargo-clippy");
41+
2942
run_success(
30-
"cargo clippy",
31-
Command::new(cargo_clippy_path())
43+
"cargo run",
44+
Command::new(exe)
3245
.arg("clippy")
3346
.args(args)
3447
// 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)