Skip to content

Commit fc8bf97

Browse files
committed
Rename exit_if_err to run_exit_on_err
1 parent 6030997 commit fc8bf97

File tree

4 files changed

+55
-67
lines changed

4 files changed

+55
-67
lines changed

clippy_dev/src/dogfood.rs

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,29 @@
1-
use crate::utils::exit_if_err;
1+
use crate::utils::run_exit_on_err;
2+
use itertools::Itertools;
23
use std::process::Command;
34

45
/// # Panics
56
///
67
/// Panics if unable to run the dogfood test
78
#[allow(clippy::fn_params_excessive_bools)]
89
pub fn dogfood(fix: bool, allow_dirty: bool, allow_staged: bool, allow_no_vcs: bool) {
9-
let mut cmd = Command::new("cargo");
10-
11-
cmd.args(["test", "--test", "dogfood"])
12-
.args(["--features", "internal"])
13-
.args(["--", "dogfood_clippy", "--nocapture"]);
14-
15-
let mut dogfood_args = Vec::new();
16-
if fix {
17-
dogfood_args.push("--fix");
18-
}
19-
20-
if allow_dirty {
21-
dogfood_args.push("--allow-dirty");
22-
}
23-
24-
if allow_staged {
25-
dogfood_args.push("--allow-staged");
26-
}
27-
28-
if allow_no_vcs {
29-
dogfood_args.push("--allow-no-vcs");
30-
}
31-
32-
cmd.env("__CLIPPY_DOGFOOD_ARGS", dogfood_args.join(" "));
33-
34-
exit_if_err(cmd.status());
10+
run_exit_on_err(
11+
"cargo test",
12+
Command::new("cargo")
13+
.args(["test", "--test", "dogfood"])
14+
.args(["--features", "internal"])
15+
.args(["--", "dogfood_clippy", "--nocapture"])
16+
.env(
17+
"__CLIPPY_DOGFOOD_ARGS",
18+
[
19+
if fix { "--fix" } else { "" },
20+
if allow_dirty { "--allow-dirty" } else { "" },
21+
if allow_staged { "--allow-staged" } else { "" },
22+
if allow_no_vcs { "--allow-no-vcs" } else { "" },
23+
]
24+
.iter()
25+
.filter(|x| !x.is_empty())
26+
.join(" "),
27+
),
28+
);
3529
}

clippy_dev/src/lint.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{cargo_clippy_path, exit_if_err};
1+
use crate::utils::{cargo_clippy_path, run_exit_on_err};
22
use std::process::{self, Command};
33
use std::{env, fs};
44

@@ -12,33 +12,31 @@ pub fn run<'a>(path: &str, edition: &str, args: impl Iterator<Item = &'a String>
1212
};
1313

1414
if is_file {
15-
exit_if_err(
16-
Command::new(env::var("CARGO").unwrap_or_else(|_| "cargo".into()))
15+
run_exit_on_err(
16+
"cargo run",
17+
Command::new(env::var("CARGO").unwrap_or("cargo".into()))
1718
.args(["run", "--bin", "clippy-driver", "--"])
1819
.args(["-L", "./target/debug"])
1920
.args(["-Z", "no-codegen"])
2021
.args(["--edition", edition])
2122
.arg(path)
2223
.args(args)
2324
// Prevent rustc from creating `rustc-ice-*` files the console output is enough.
24-
.env("RUSTC_ICE", "0")
25-
.status(),
25+
.env("RUSTC_ICE", "0"),
2626
);
2727
} else {
28-
exit_if_err(
29-
Command::new(env::var("CARGO").unwrap_or_else(|_| "cargo".into()))
30-
.arg("build")
31-
.status(),
28+
run_exit_on_err(
29+
"cargo build",
30+
Command::new(env::var("CARGO").unwrap_or_else(|_| "cargo".into())).arg("build"),
31+
);
32+
run_exit_on_err(
33+
"cargo clippy",
34+
Command::new(cargo_clippy_path())
35+
.arg("clippy")
36+
.args(args)
37+
// Prevent rustc from creating `rustc-ice-*` files the console output is enough.
38+
.env("RUSTC_ICE", "0")
39+
.current_dir(path),
3240
);
33-
34-
let status = Command::new(cargo_clippy_path())
35-
.arg("clippy")
36-
.args(args)
37-
// Prevent rustc from creating `rustc-ice-*` files the console output is enough.
38-
.env("RUSTC_ICE", "0")
39-
.current_dir(path)
40-
.status();
41-
42-
exit_if_err(status);
4341
}
4442
}

clippy_dev/src/setup/toolchain.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::utils::run_exit_on_err;
12
use std::env::consts::EXE_SUFFIX;
23
use std::env::current_dir;
34
use std::ffi::OsStr;
@@ -6,8 +7,6 @@ use std::path::{Path, PathBuf};
67
use std::process::Command;
78
use walkdir::WalkDir;
89

9-
use crate::utils::exit_if_err;
10-
1110
pub fn create(standalone: bool, force: bool, release: bool, name: &str) {
1211
let rustup_home = std::env::var("RUSTUP_HOME").unwrap();
1312
let toolchain = std::env::var("RUSTUP_TOOLCHAIN").unwrap();
@@ -45,11 +44,10 @@ pub fn create(standalone: bool, force: bool, release: bool, name: &str) {
4544
}
4645
}
4746

48-
let status = Command::new("cargo")
49-
.arg("build")
50-
.args(release.then_some("--release"))
51-
.status();
52-
exit_if_err(status);
47+
run_exit_on_err(
48+
"cargo build",
49+
Command::new("cargo").arg("build").args(release.then_some("--release")),
50+
);
5351

5452
install_bin("cargo-clippy", &dest, standalone, release);
5553
install_bin("clippy-driver", &dest, standalone, release);

clippy_dev/src/utils.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::ffi::OsStr;
88
use std::fs::{self, OpenOptions};
99
use std::io::{self, Read as _, Seek as _, SeekFrom, Write};
1010
use std::path::{Path, PathBuf};
11-
use std::process::{self, Command, ExitStatus, Stdio};
11+
use std::process::{self, Command, Stdio};
1212
use std::{env, thread};
1313
use walkdir::WalkDir;
1414

@@ -288,19 +288,6 @@ impl ClippyInfo {
288288
}
289289
}
290290

291-
/// # Panics
292-
/// Panics if given command result was failed.
293-
pub fn exit_if_err(status: io::Result<ExitStatus>) {
294-
match status.expect("failed to run command").code() {
295-
Some(0) => {},
296-
Some(n) => process::exit(n),
297-
None => {
298-
eprintln!("Killed by signal");
299-
process::exit(1);
300-
},
301-
}
302-
}
303-
304291
#[derive(Clone, Copy)]
305292
pub enum UpdateStatus {
306293
Unchanged,
@@ -653,6 +640,17 @@ pub fn write_file(path: &Path, contents: &str) {
653640
expect_action(fs::write(path, contents), ErrAction::Write, path);
654641
}
655642

643+
pub fn run_exit_on_err(path: &(impl AsRef<Path> + ?Sized), cmd: &mut Command) {
644+
match expect_action(cmd.status(), ErrAction::Run, path.as_ref()).code() {
645+
Some(0) => {},
646+
Some(n) => process::exit(n),
647+
None => {
648+
eprintln!("{} killed by signal", path.as_ref().display());
649+
process::exit(1);
650+
},
651+
}
652+
}
653+
656654
#[must_use]
657655
pub fn run_with_output(path: &(impl AsRef<Path> + ?Sized), cmd: &mut Command) -> Vec<u8> {
658656
fn f(path: &Path, cmd: &mut Command) -> Vec<u8> {

0 commit comments

Comments
 (0)