Skip to content

Commit 106ac79

Browse files
committed
Add expect_action helper to clippy_dev
1 parent 544c300 commit 106ac79

File tree

4 files changed

+56
-63
lines changed

4 files changed

+56
-63
lines changed

clippy_dev/src/fmt.rs

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::utils::{
2-
ErrAction, FileUpdater, UpdateMode, UpdateStatus, panic_action, run_with_output, split_args_for_threads,
2+
ErrAction, FileUpdater, UpdateMode, UpdateStatus, expect_action, run_with_output, split_args_for_threads,
33
walk_dir_no_dot_or_target,
44
};
55
use itertools::Itertools;
@@ -270,7 +270,7 @@ fn run_rustfmt(update_mode: UpdateMode) {
270270

271271
let args: Vec<_> = walk_dir_no_dot_or_target()
272272
.filter_map(|e| {
273-
let e = e.expect("error reading `.`");
273+
let e = expect_action(e, ErrAction::Read, ".");
274274
e.path()
275275
.as_os_str()
276276
.as_encoded_bytes()
@@ -294,37 +294,32 @@ fn run_rustfmt(update_mode: UpdateMode) {
294294
},
295295
args.iter(),
296296
)
297-
.map(|mut cmd| match cmd.spawn() {
298-
Ok(x) => x,
299-
Err(ref e) => panic_action(&e, ErrAction::Run, "rustfmt".as_ref()),
300-
})
297+
.map(|mut cmd| expect_action(cmd.spawn(), ErrAction::Run, "rustfmt"))
301298
.collect();
302299

303300
for child in &mut children {
304-
match child.wait() {
305-
Ok(status) => match (update_mode, status.exit_ok()) {
306-
(UpdateMode::Check | UpdateMode::Change, Ok(())) => {},
307-
(UpdateMode::Check, Err(_)) => {
308-
let mut s = String::new();
309-
if let Some(mut stderr) = child.stderr.take()
310-
&& stderr.read_to_string(&mut s).is_ok()
311-
{
312-
eprintln!("{s}");
313-
}
314-
eprintln!("Formatting check failed!\nRun `cargo dev fmt` to update.");
315-
process::exit(1);
316-
},
317-
(UpdateMode::Change, Err(e)) => {
318-
let mut s = String::new();
319-
if let Some(mut stderr) = child.stderr.take()
320-
&& stderr.read_to_string(&mut s).is_ok()
321-
{
322-
eprintln!("{s}");
323-
}
324-
panic_action(&e, ErrAction::Run, "rustfmt".as_ref());
325-
},
301+
let status = expect_action(child.wait(), ErrAction::Run, "rustfmt");
302+
match (update_mode, status.exit_ok()) {
303+
(UpdateMode::Check | UpdateMode::Change, Ok(())) => {},
304+
(UpdateMode::Check, Err(_)) => {
305+
let mut s = String::new();
306+
if let Some(mut stderr) = child.stderr.take()
307+
&& stderr.read_to_string(&mut s).is_ok()
308+
{
309+
eprintln!("{s}");
310+
}
311+
eprintln!("Formatting check failed!\nRun `cargo dev fmt` to update.");
312+
process::exit(1);
313+
},
314+
(UpdateMode::Change, e) => {
315+
let mut s = String::new();
316+
if let Some(mut stderr) = child.stderr.take()
317+
&& stderr.read_to_string(&mut s).is_ok()
318+
{
319+
eprintln!("{s}");
320+
}
321+
expect_action(e, ErrAction::Run, "rustfmt");
326322
},
327-
Err(ref e) => panic_action(e, ErrAction::Run, "rustfmt".as_ref()),
328323
}
329324
}
330325
}

clippy_dev/src/rename_lint.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::update_lints::{RenamedLint, find_lint_decls, generate_lint_files, read_deprecated_lints};
22
use crate::utils::{
3-
FileUpdater, RustSearcher, Token, UpdateMode, UpdateStatus, Version, delete_dir_if_exists, delete_file_if_exists,
4-
try_rename_dir, try_rename_file, walk_dir_no_dot_or_target,
3+
ErrAction, FileUpdater, RustSearcher, Token, UpdateMode, UpdateStatus, Version, delete_dir_if_exists,
4+
delete_file_if_exists, expect_action, try_rename_dir, try_rename_file, walk_dir_no_dot_or_target,
55
};
66
use rustc_lexer::TokenKind;
77
use std::ffi::OsString;
@@ -132,10 +132,10 @@ pub fn rename(clippy_version: Version, old_name: &str, new_name: &str, uplift: b
132132
}
133133

134134
let mut update_fn = file_update_fn(old_name, new_name, mod_edit);
135-
for file in walk_dir_no_dot_or_target() {
136-
let file = file.expect("error reading `.`");
137-
if file.path().as_os_str().as_encoded_bytes().ends_with(b".rs") {
138-
updater.update_file(file.path(), &mut update_fn);
135+
for e in walk_dir_no_dot_or_target() {
136+
let e = expect_action(e, ErrAction::Read, ".");
137+
if e.path().as_os_str().as_encoded_bytes().ends_with(b".rs") {
138+
updater.update_file(e.path(), &mut update_fn);
139139
}
140140
}
141141
generate_lint_files(UpdateMode::Change, &lints, &deprecated_lints, &renamed_lints);

clippy_dev/src/update_lints.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::utils::{
2-
ErrAction, File, FileUpdater, RustSearcher, Token, UpdateMode, UpdateStatus, panic_action, update_text_region_fn,
2+
ErrAction, File, FileUpdater, RustSearcher, Token, UpdateMode, UpdateStatus, expect_action, update_text_region_fn,
33
};
44
use itertools::Itertools;
55
use std::collections::HashSet;
@@ -201,10 +201,7 @@ pub fn find_lint_decls() -> Vec<Lint> {
201201
/// Reads the source files from the given root directory
202202
fn read_src_with_module(src_root: &Path) -> impl use<'_> + Iterator<Item = (DirEntry, String)> {
203203
WalkDir::new(src_root).into_iter().filter_map(move |e| {
204-
let e = match e {
205-
Ok(e) => e,
206-
Err(ref e) => panic_action(e, ErrAction::Read, src_root),
207-
};
204+
let e = expect_action(e, ErrAction::Read, src_root);
208205
let path = e.path().as_os_str().as_encoded_bytes();
209206
if let Some(path) = path.strip_suffix(b".rs")
210207
&& let Some(path) = path.get("clippy_lints/src/".len()..)

clippy_dev/src/utils.rs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ pub fn panic_action(err: &impl Display, action: ErrAction, path: &Path) -> ! {
4747
panic!("error {} `{}`: {}", action.as_str(), path.display(), *err)
4848
}
4949

50+
#[track_caller]
51+
pub fn expect_action<T>(res: Result<T, impl Display>, action: ErrAction, path: impl AsRef<Path>) -> T {
52+
match res {
53+
Ok(x) => x,
54+
Err(ref e) => panic_action(e, action, path.as_ref()),
55+
}
56+
}
57+
5058
/// Wrapper around `std::fs::File` which panics with a path on failure.
5159
pub struct File<'a> {
5260
pub inner: fs::File,
@@ -57,9 +65,9 @@ impl<'a> File<'a> {
5765
#[track_caller]
5866
pub fn open(path: &'a (impl AsRef<Path> + ?Sized), options: &mut OpenOptions) -> Self {
5967
let path = path.as_ref();
60-
match options.open(path) {
61-
Ok(inner) => Self { inner, path },
62-
Err(e) => panic_action(&e, ErrAction::Open, path),
68+
Self {
69+
inner: expect_action(options.open(path), ErrAction::Open, path),
70+
path,
6371
}
6472
}
6573

@@ -86,10 +94,7 @@ impl<'a> File<'a> {
8694
/// Read the entire contents of a file to the given buffer.
8795
#[track_caller]
8896
pub fn read_append_to_string<'dst>(&mut self, dst: &'dst mut String) -> &'dst mut String {
89-
match self.inner.read_to_string(dst) {
90-
Ok(_) => {},
91-
Err(e) => panic_action(&e, ErrAction::Read, self.path),
92-
}
97+
expect_action(self.inner.read_to_string(dst), ErrAction::Read, self.path);
9398
dst
9499
}
95100

@@ -109,9 +114,7 @@ impl<'a> File<'a> {
109114
},
110115
Err(e) => Err(e),
111116
};
112-
if let Err(e) = res {
113-
panic_action(&e, ErrAction::Write, self.path);
114-
}
117+
expect_action(res, ErrAction::Write, self.path);
115118
}
116119
}
117120

@@ -662,24 +665,22 @@ pub fn try_rename_dir(old_name: &Path, new_name: &Path) -> bool {
662665
}
663666

664667
pub fn write_file(path: &Path, contents: &str) {
665-
fs::write(path, contents).unwrap_or_else(|e| panic_action(&e, ErrAction::Write, path));
668+
expect_action(fs::write(path, contents), ErrAction::Write, path);
666669
}
667670

668671
#[must_use]
669672
pub fn run_with_output(path: &(impl AsRef<Path> + ?Sized), cmd: &mut Command) -> Vec<u8> {
670673
fn f(path: &Path, cmd: &mut Command) -> Vec<u8> {
671-
match cmd
672-
.stdin(Stdio::null())
673-
.stdout(Stdio::piped())
674-
.stderr(Stdio::inherit())
675-
.output()
676-
{
677-
Ok(x) => match x.status.exit_ok() {
678-
Ok(()) => x.stdout,
679-
Err(ref e) => panic_action(e, ErrAction::Run, path),
680-
},
681-
Err(ref e) => panic_action(e, ErrAction::Run, path),
682-
}
674+
let output = expect_action(
675+
cmd.stdin(Stdio::null())
676+
.stdout(Stdio::piped())
677+
.stderr(Stdio::inherit())
678+
.output(),
679+
ErrAction::Run,
680+
path,
681+
);
682+
expect_action(output.status.exit_ok(), ErrAction::Run, path);
683+
output.stdout
683684
}
684685
f(path.as_ref(), cmd)
685686
}

0 commit comments

Comments
 (0)