Repository: nushell/nushell
File: crates/nu-command/src/filters/default.rs (Rust)

Code before:
                            if matches!(val, Value::Nothing { .. })
                                || (default_when_empty && val.is_empty())
                            {
                                *val = value.clone();
                            }
                        } else {
                            record.push(column.item.clone(), value.clone());
                        }

                        item
                    }
                    _ => item,
                },
                engine_state.signals(),
            )
            .map(|x| x.set_metadata(metadata))
    } else if input.is_nothing()
        || (default_when_empty
            && matches!(input, PipelineData::Value(ref value, _) if value.is_empty()))
    {
        Ok(value.into_pipeline_data())
    } else if let PipelineData::ListStream(ls, metadata) = input {
        let span = ls.span();
        let mut stream = ls.into_inner().peekable();
        if stream.peek().is_none() {
            return Ok(value.into_pipeline_data());
        }

        // stream's internal state already preserves the original signals config, so if this
        // Signals::empty list stream gets interrupted it will be catched by the underlying iterator
        let ls = ListStream::new(stream, span, Signals::empty());
        Ok(PipelineData::ListStream(ls, metadata))
    } else {
        Ok(input)
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_examples() {
        use crate::test_examples;

        test_examples(Default {})
    }
}


Review comment (suggestion):
We only need to do the empty ListStream replacement when `--empty` is passed (as represented by `default_when_empty`)

Code after:
                            if matches!(val, Value::Nothing { .. })
                                || (default_when_empty && val.is_empty())
                            {
                                *val = value.clone();
                            }
                        } else {
                            record.push(column.item.clone(), value.clone());
                        }

                        item
                    }
                    _ => item,
                },
                engine_state.signals(),
            )
            .map(|x| x.set_metadata(metadata))
    } else if input.is_nothing()
        || (default_when_empty
            && matches!(input, PipelineData::Value(ref value, _) if value.is_empty()))
    {
        Ok(value.into_pipeline_data())
    } else if default_when_empty && matches!(input, PipelineData::ListStream(..)) {
        let PipelineData::ListStream(ls, metadata) = input else {
            unreachable!()
        };
        let span = ls.span();
        let mut stream = ls.into_inner().peekable();
        if stream.peek().is_none() {
            return Ok(value.into_pipeline_data());
        }

        // stream's internal state already preserves the original signals config, so if this
        // Signals::empty list stream gets interrupted it will be caught by the underlying iterator
        let ls = ListStream::new(stream, span, Signals::empty());
        Ok(PipelineData::ListStream(ls, metadata))
    } else {
        Ok(input)
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_examples() {
        use crate::test_examples;

        test_examples(Default {})
    }
}