Skip to content

Commit 46ff0a8

Browse files
committed
Add helper function for creating cargo commands.
1 parent ed50c8a commit 46ff0a8

File tree

5 files changed

+21
-18
lines changed

5 files changed

+21
-18
lines changed

clippy_dev/src/dogfood.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use crate::utils::run_exit_on_err;
1+
use crate::utils::{cargo_cmd, run_exit_on_err};
22
use itertools::Itertools;
3-
use std::process::Command;
43

54
/// # Panics
65
///
@@ -9,7 +8,7 @@ use std::process::Command;
98
pub fn dogfood(fix: bool, allow_dirty: bool, allow_staged: bool, allow_no_vcs: bool) {
109
run_exit_on_err(
1110
"cargo test",
12-
Command::new("cargo")
11+
cargo_cmd()
1312
.args(["test", "--test", "dogfood"])
1413
.args(["--features", "internal"])
1514
.args(["--", "dogfood_clippy", "--nocapture"])

clippy_dev/src/lint.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use crate::utils::{cargo_clippy_path, run_exit_on_err};
1+
use crate::utils::{cargo_clippy_path, cargo_cmd, run_exit_on_err};
2+
use std::fs;
23
use std::process::{self, Command};
3-
use std::{env, fs};
44

55
pub fn run<'a>(path: &str, edition: &str, args: impl Iterator<Item = &'a String>) {
66
let is_file = match fs::metadata(path) {
@@ -14,7 +14,7 @@ pub fn run<'a>(path: &str, edition: &str, args: impl Iterator<Item = &'a String>
1414
if is_file {
1515
run_exit_on_err(
1616
"cargo run",
17-
Command::new(env::var("CARGO").unwrap_or("cargo".into()))
17+
cargo_cmd()
1818
.args(["run", "--bin", "clippy-driver", "--"])
1919
.args(["-L", "./target/debug"])
2020
.args(["-Z", "no-codegen"])
@@ -25,10 +25,7 @@ pub fn run<'a>(path: &str, edition: &str, args: impl Iterator<Item = &'a String>
2525
.env("RUSTC_ICE", "0"),
2626
);
2727
} else {
28-
run_exit_on_err(
29-
"cargo build",
30-
Command::new(env::var("CARGO").unwrap_or_else(|_| "cargo".into())).arg("build"),
31-
);
28+
run_exit_on_err("cargo build", cargo_cmd().arg("build"));
3229
run_exit_on_err(
3330
"cargo clippy",
3431
Command::new(cargo_clippy_path())

clippy_dev/src/serve.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use crate::utils::{ErrAction, expect_action};
1+
use crate::utils::{ErrAction, cargo_cmd, expect_action};
22
use core::fmt::Display;
33
use core::mem;
44
use std::path::Path;
55
use std::process::Command;
66
use std::time::{Duration, SystemTime};
7-
use std::{env, fs, thread};
7+
use std::{fs, thread};
88
use walkdir::WalkDir;
99

1010
#[cfg(windows)]
@@ -27,9 +27,7 @@ pub fn run(port: u16, lint: Option<String>) -> ! {
2727
if is_metadata_outdated(mem::replace(&mut last_update, SystemTime::now())) {
2828
// Ignore the command result; we'll fall back to displaying the old metadata.
2929
let _ = expect_action(
30-
Command::new(env::var("CARGO").unwrap_or_else(|_| "cargo".into()))
31-
.arg("collect-metadata")
32-
.status(),
30+
cargo_cmd().arg("collect-metadata").status(),
3331
ErrAction::Run,
3432
"cargo collect-metadata",
3533
);

clippy_dev/src/setup/toolchain.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
use crate::utils::run_exit_on_err;
1+
use crate::utils::{cargo_cmd, run_exit_on_err};
22
use std::env::consts::EXE_SUFFIX;
33
use std::env::current_dir;
44
use std::ffi::OsStr;
55
use std::fs;
66
use std::path::{Path, PathBuf};
7-
use std::process::Command;
87
use walkdir::WalkDir;
98

109
pub fn create(standalone: bool, force: bool, release: bool, name: &str) {
@@ -46,7 +45,7 @@ pub fn create(standalone: bool, force: bool, release: bool, name: &str) {
4645

4746
run_exit_on_err(
4847
"cargo build",
49-
Command::new("cargo").arg("build").args(release.then_some("--release")),
48+
cargo_cmd().arg("build").args(release.then_some("--release")),
5049
);
5150

5251
install_bin("cargo-clippy", &dest, standalone, release);

clippy_dev/src/utils.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@ pub fn cargo_clippy_path() -> PathBuf {
130130
path
131131
}
132132

133+
/// Creates a `Command` for running cargo.
134+
#[must_use]
135+
pub fn cargo_cmd() -> Command {
136+
if let Some(path) = env::var_os("CARGO") {
137+
Command::new(path)
138+
} else {
139+
Command::new("cargo")
140+
}
141+
}
142+
133143
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
134144
pub struct Version {
135145
pub major: u16,

0 commit comments

Comments
 (0)