Skip to content

Commit 175c229

Browse files
committed
refactor(lib): simplify stdout flush error handling in bin! macro
Reorder imports for consistency and refactor the ignore_closed_stdout condition to a more concise form, improving code readability without altering functionality.
1 parent d264f0c commit 175c229

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

src/uucore/src/lib/lib.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ macro_rules! bin {
217217
let stdout_was_closed = {
218218
#[cfg(unix)]
219219
{
220-
use nix::fcntl::{fcntl, FcntlArg};
220+
use nix::fcntl::{FcntlArg, fcntl};
221221
match fcntl(std::io::stdout(), FcntlArg::F_GETFL) {
222222
Ok(_) => false,
223223
Err(nix::errno::Errno::EBADF) => true,
@@ -236,17 +236,16 @@ macro_rules! bin {
236236
if let Err(e) = std::io::stdout().flush() {
237237
// Treat write errors as a failure, but ignore BrokenPipe to avoid
238238
// breaking utilities that intentionally silence it (e.g., seq).
239-
let ignore_closed_stdout = stdout_was_closed
240-
&& {
241-
#[cfg(unix)]
242-
{
243-
e.raw_os_error() == Some(nix::errno::Errno::EBADF as i32)
244-
}
245-
#[cfg(not(unix))]
246-
{
247-
false
248-
}
249-
};
239+
let ignore_closed_stdout = stdout_was_closed && {
240+
#[cfg(unix)]
241+
{
242+
e.raw_os_error() == Some(nix::errno::Errno::EBADF as i32)
243+
}
244+
#[cfg(not(unix))]
245+
{
246+
false
247+
}
248+
};
250249
if e.kind() != std::io::ErrorKind::BrokenPipe && !ignore_closed_stdout {
251250
eprintln!("Error flushing stdout: {e}");
252251
if code == 0 {

0 commit comments

Comments
 (0)