Repository: nushell/nushell
File: crates/nu-command/src/filesystem/save.rs (Rust)

Code before:
                        stream_to_file(source, size, signals, file, span, progress)?;
                    }
                    ByteStreamSource::Child(mut child) => {
                        fn write_or_consume_stderr(
                            stderr: ChildPipe,
                            file: Option<File>,
                            span: Span,
                            signals: &Signals,
                            progress: bool,
                        ) -> Result<(), ShellError> {
                            if let Some(file) = file {
                                match stderr {
                                    ChildPipe::Pipe(pipe) => {
                                        stream_to_file(pipe, None, signals, file, span, progress)
                                    }
                                    ChildPipe::Tee(tee) => {
                                        stream_to_file(tee, None, signals, file, span, progress)
                                    }
                                }?
                            } else {
                                match stderr {
                                    ChildPipe::Pipe(mut pipe) => {
                                        io::copy(&mut pipe, &mut io::sink())
                                    }
                                    ChildPipe::Tee(mut tee) => io::copy(&mut tee, &mut io::sink()),
                                }
                                .err_span(span)?;
                            }
                            Ok(())
                        }

                        match (child.stdout.take(), child.stderr.take()) {
                            (Some(stdout), stderr) => {
                                // delegate a thread to redirect stderr to result.
                                let handler = stderr
                                    .map(|stderr| {
                                        let signals = signals.clone();
                                        thread::Builder::new().name("stderr saver".into()).spawn(
                                            move || {
                                                write_or_consume_stderr(
                                                    stderr,
                                                    stderr_file,
                                                    span,
                                                    &signals,
                                                    progress,
                                                )
                                            },
                                        )
                                    })
                                    .transpose()
                                    .err_span(span)?;

Review comment (suggestion):
Both of these should use the `stderr` `OutDest` in the `Stack`.
```suggestion
                                        match stderr {
                                            OutDest::Pipe | OutDest::Capture => {
                                                // What to do here?
                                                Ok(0)
                                            }
                                            OutDest::Null => io::copy(&mut tee, &mut io::sink()),
                                            OutDest::Inherit => {
                                                io::copy(&mut tee, &mut io::stderr())
                                            }
                                            OutDest::File(file) => {
                                                io::copy(&mut tee, &mut file.as_ref())
                                            }
                                        }
```
```rust
fn write_or_consume_stderr(
    child_stderr: ChildPipe,
    stderr: &OutDest,
    file: Option<File>,
// ...
```
```rust
write_or_consume_stderr(
    stderr,
    stack.stderr(),
    stderr_file,
    span,
    &signals,
    progress,
)
```

Code after:
                        stream_to_file(source, size, signals, file, span, progress)?;
                    }
                    ByteStreamSource::Child(mut child) => {
                        fn write_or_consume_stderr(
                            stderr: ChildPipe,
                            file: Option<File>,
                            span: Span,
                            signals: &Signals,
                            progress: bool,
                        ) -> Result<(), ShellError> {
                            if let Some(file) = file {
                                match stderr {
                                    ChildPipe::Pipe(pipe) => {
                                        stream_to_file(pipe, None, signals, file, span, progress)
                                    }
                                    ChildPipe::Tee(tee) => {
                                        stream_to_file(tee, None, signals, file, span, progress)
                                    }
                                }?
                            } else {
                                match stderr {
                                    ChildPipe::Pipe(mut pipe) => {
                                        io::copy(&mut pipe, &mut io::stderr())
                                    }
                                    ChildPipe::Tee(mut tee) => {
                                        io::copy(&mut tee, &mut io::stderr())
                                    }
                                }
                                .err_span(span)?;
                            }
                            Ok(())
                        }

                        match (child.stdout.take(), child.stderr.take()) {
                            (Some(stdout), stderr) => {
                                // delegate a thread to redirect stderr to result.
                                let handler = stderr
                                    .map(|stderr| {
                                        let signals = signals.clone();
                                        thread::Builder::new().name("stderr saver".into()).spawn(
                                            move || {
                                                write_or_consume_stderr(
                                                    stderr,
                                                    stderr_file,
                                                    span,
                                                    &signals,
                                                    progress,
                                                )
                                            },
                                        )
                                    })