diff options
| author | Jason Newcomb <jsnewcomb@pm.me> | 2025-05-23 13:19:51 -0400 |
|---|---|---|
| committer | Jason Newcomb <jsnewcomb@pm.me> | 2025-07-11 00:57:50 -0400 |
| commit | fc8bf97095d6578be5464c4f10e1f11971738d80 (patch) | |
| tree | 7c21a000a69b0dc98cb13024dc7e18b71281828e | |
| parent | 6030997800b681cee91acf6ef8aba199d03041a6 (diff) | |
| download | rust-fc8bf97095d6578be5464c4f10e1f11971738d80.tar.gz rust-fc8bf97095d6578be5464c4f10e1f11971738d80.zip | |
Rename `exit_if_err` to `run_exit_on_err`
| -rw-r--r-- | clippy_dev/src/dogfood.rs | 48 | ||||
| -rw-r--r-- | clippy_dev/src/lint.rs | 36 | ||||
| -rw-r--r-- | clippy_dev/src/setup/toolchain.rs | 12 | ||||
| -rw-r--r-- | clippy_dev/src/utils.rs | 26 |
4 files changed, 55 insertions, 67 deletions
diff --git a/clippy_dev/src/dogfood.rs b/clippy_dev/src/dogfood.rs index 7e9d92458d0..c1ee3633d3e 100644 --- a/clippy_dev/src/dogfood.rs +++ b/clippy_dev/src/dogfood.rs @@ -1,4 +1,5 @@ -use crate::utils::exit_if_err; +use crate::utils::run_exit_on_err; +use itertools::Itertools; use std::process::Command; /// # Panics @@ -6,30 +7,23 @@ use std::process::Command; /// Panics if unable to run the dogfood test #[allow(clippy::fn_params_excessive_bools)] pub fn dogfood(fix: bool, allow_dirty: bool, allow_staged: bool, allow_no_vcs: bool) { - let mut cmd = Command::new("cargo"); - - cmd.args(["test", "--test", "dogfood"]) - .args(["--features", "internal"]) - .args(["--", "dogfood_clippy", "--nocapture"]); - - let mut dogfood_args = Vec::new(); - if fix { - dogfood_args.push("--fix"); - } - - if allow_dirty { - dogfood_args.push("--allow-dirty"); - } - - if allow_staged { - dogfood_args.push("--allow-staged"); - } - - if allow_no_vcs { - dogfood_args.push("--allow-no-vcs"); - } - - cmd.env("__CLIPPY_DOGFOOD_ARGS", dogfood_args.join(" ")); - - exit_if_err(cmd.status()); + run_exit_on_err( + "cargo test", + Command::new("cargo") + .args(["test", "--test", "dogfood"]) + .args(["--features", "internal"]) + .args(["--", "dogfood_clippy", "--nocapture"]) + .env( + "__CLIPPY_DOGFOOD_ARGS", + [ + if fix { "--fix" } else { "" }, + if allow_dirty { "--allow-dirty" } else { "" }, + if allow_staged { "--allow-staged" } else { "" }, + if allow_no_vcs { "--allow-no-vcs" } else { "" }, + ] + .iter() + .filter(|x| !x.is_empty()) + .join(" "), + ), + ); } diff --git a/clippy_dev/src/lint.rs b/clippy_dev/src/lint.rs index 0d66f167a38..f450bf5d8a2 100644 --- a/clippy_dev/src/lint.rs +++ b/clippy_dev/src/lint.rs @@ -1,4 +1,4 @@ -use crate::utils::{cargo_clippy_path, exit_if_err}; +use crate::utils::{cargo_clippy_path, run_exit_on_err}; use std::process::{self, Command}; use std::{env, fs}; @@ -12,8 +12,9 @@ pub fn run<'a>(path: &str, edition: &str, args: impl Iterator<Item = &'a String> }; if is_file { - exit_if_err( - Command::new(env::var("CARGO").unwrap_or_else(|_| "cargo".into())) + run_exit_on_err( + "cargo run", + Command::new(env::var("CARGO").unwrap_or("cargo".into())) .args(["run", "--bin", "clippy-driver", "--"]) .args(["-L", "./target/debug"]) .args(["-Z", "no-codegen"]) @@ -21,24 +22,21 @@ pub fn run<'a>(path: &str, edition: &str, args: impl Iterator<Item = &'a String> .arg(path) .args(args) // Prevent rustc from creating `rustc-ice-*` files the console output is enough. - .env("RUSTC_ICE", "0") - .status(), + .env("RUSTC_ICE", "0"), ); } else { - exit_if_err( - Command::new(env::var("CARGO").unwrap_or_else(|_| "cargo".into())) - .arg("build") - .status(), + run_exit_on_err( + "cargo build", + Command::new(env::var("CARGO").unwrap_or_else(|_| "cargo".into())).arg("build"), + ); + run_exit_on_err( + "cargo clippy", + Command::new(cargo_clippy_path()) + .arg("clippy") + .args(args) + // Prevent rustc from creating `rustc-ice-*` files the console output is enough. + .env("RUSTC_ICE", "0") + .current_dir(path), ); - - let status = Command::new(cargo_clippy_path()) - .arg("clippy") - .args(args) - // Prevent rustc from creating `rustc-ice-*` files the console output is enough. - .env("RUSTC_ICE", "0") - .current_dir(path) - .status(); - - exit_if_err(status); } } diff --git a/clippy_dev/src/setup/toolchain.rs b/clippy_dev/src/setup/toolchain.rs index c70b56461a7..4c1db4e8a67 100644 --- a/clippy_dev/src/setup/toolchain.rs +++ b/clippy_dev/src/setup/toolchain.rs @@ -1,3 +1,4 @@ +use crate::utils::run_exit_on_err; use std::env::consts::EXE_SUFFIX; use std::env::current_dir; use std::ffi::OsStr; @@ -6,8 +7,6 @@ use std::path::{Path, PathBuf}; use std::process::Command; use walkdir::WalkDir; -use crate::utils::exit_if_err; - pub fn create(standalone: bool, force: bool, release: bool, name: &str) { let rustup_home = std::env::var("RUSTUP_HOME").unwrap(); let toolchain = std::env::var("RUSTUP_TOOLCHAIN").unwrap(); @@ -45,11 +44,10 @@ pub fn create(standalone: bool, force: bool, release: bool, name: &str) { } } - let status = Command::new("cargo") - .arg("build") - .args(release.then_some("--release")) - .status(); - exit_if_err(status); + run_exit_on_err( + "cargo build", + Command::new("cargo").arg("build").args(release.then_some("--release")), + ); install_bin("cargo-clippy", &dest, standalone, release); install_bin("clippy-driver", &dest, standalone, release); diff --git a/clippy_dev/src/utils.rs b/clippy_dev/src/utils.rs index 89962a11034..f518460f582 100644 --- a/clippy_dev/src/utils.rs +++ b/clippy_dev/src/utils.rs @@ -8,7 +8,7 @@ use std::ffi::OsStr; use std::fs::{self, OpenOptions}; use std::io::{self, Read as _, Seek as _, SeekFrom, Write}; use std::path::{Path, PathBuf}; -use std::process::{self, Command, ExitStatus, Stdio}; +use std::process::{self, Command, Stdio}; use std::{env, thread}; use walkdir::WalkDir; @@ -288,19 +288,6 @@ impl ClippyInfo { } } -/// # Panics -/// Panics if given command result was failed. -pub fn exit_if_err(status: io::Result<ExitStatus>) { - match status.expect("failed to run command").code() { - Some(0) => {}, - Some(n) => process::exit(n), - None => { - eprintln!("Killed by signal"); - process::exit(1); - }, - } -} - #[derive(Clone, Copy)] pub enum UpdateStatus { Unchanged, @@ -653,6 +640,17 @@ pub fn write_file(path: &Path, contents: &str) { expect_action(fs::write(path, contents), ErrAction::Write, path); } +pub fn run_exit_on_err(path: &(impl AsRef<Path> + ?Sized), cmd: &mut Command) { + match expect_action(cmd.status(), ErrAction::Run, path.as_ref()).code() { + Some(0) => {}, + Some(n) => process::exit(n), + None => { + eprintln!("{} killed by signal", path.as_ref().display()); + process::exit(1); + }, + } +} + #[must_use] pub fn run_with_output(path: &(impl AsRef<Path> + ?Sized), cmd: &mut Command) -> Vec<u8> { fn f(path: &Path, cmd: &mut Command) -> Vec<u8> { |
