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

Code before:
    engine_state: &EngineState,
    stack: &mut Stack,
    call: &Call,
    input: PipelineData,
    default_value: Value,
    default_when_empty: bool,
    columns: &[String],
) -> Result<PipelineData, ShellError> {
    let input_span = input.span().unwrap_or_else(Span::unknown);
    let mut default_value = DefaultValue::new(engine_state, stack, default_value);
    let metadata = input.metadata();

    // If user supplies columns, check if input is a record or list of records
    // and set the default value for the specified record columns
    if !columns.is_empty() {
        if matches!(input, PipelineData::Value(Value::Record { .. }, _)) {
            let Value::Record {
                val: ref mut record,
                internal_span,
            } = input.into_value(input_span)?
            else {
                unreachable!()
            };
            let record = record.to_mut().into_spanned(internal_span);
            fill_record(record, &mut default_value, columns, default_when_empty)
                .map(|x| x.into_pipeline_data().set_metadata(metadata))
        } else if matches!(
            input,
            PipelineData::ListStream(..) | PipelineData::Value(Value::List { .. }, _)
        ) {
            let mut output_list: Vec<Value> = vec![];
            for mut item in input {
                if let Value::Record {
                    val: ref mut record,
                    internal_span,
                } = item
                {
                    let record = record.to_mut().into_spanned(internal_span);
                    item = fill_record(record, &mut default_value, columns, default_when_empty)?;
                }
                output_list.push(item);
            }
            let ls = ListStream::new(
                output_list.into_iter(),
                call.head,
                engine_state.signals().clone(),
            );
            Ok(PipelineData::ListStream(ls, metadata))
        // If columns are given, but input does not use columns, return an error
        } else {
            Err(ShellError::PipelineMismatch {

Review comment (refactor):
```suggestion
            let record = input.into_value(input_span)?.into_record()?;
            fill_record(
                record,
                input_span,
                &mut default_value,
                columns.as_slice(),
                default_when_empty,
            )
            .map(|x| x.into_pipeline_data_with_metadata(metadata))
```

Since we're already coercing this into a concrete `Value`, we can just extract the `Record` value directly rather than mutating it by reference, which is a bit nicer to work with

Code after:
    stack: &mut Stack,
    call: &Call,
    input: PipelineData,
    default_value: Value,
    default_when_empty: bool,
    columns: Vec<String>,
) -> Result<PipelineData, ShellError> {
    let input_span = input.span().unwrap_or(call.head);
    let mut default_value = DefaultValue::new(engine_state, stack, default_value);
    let metadata = input.metadata();

    // If user supplies columns, check if input is a record or list of records
    // and set the default value for the specified record columns
    if !columns.is_empty() {
        if matches!(input, PipelineData::Value(Value::Record { .. }, _)) {
            let record = input.into_value(input_span)?.into_record()?;
            fill_record(
                record,
                input_span,
                &mut default_value,
                columns.as_slice(),
                default_when_empty,
            )
            .map(|x| x.into_pipeline_data_with_metadata(metadata))
        } else if matches!(
            input,
            PipelineData::ListStream(..) | PipelineData::Value(Value::List { .. }, _)
        ) {
            // Potential enhancement: add another branch for Value::List,
            // and collect the iterator into a Result<Value::List, ShellError>
            // so we can preemptively return an error for collected lists
            let head = call.head;
            Ok(input
                .into_iter()
                .map(move |item| {
                    let span = item.span();
                    if let Value::Record { val, .. } = item {
                        fill_record(
                            val.into_owned(),
                            span,
                            &mut default_value,
                            columns.as_slice(),
                            default_when_empty,
                        )
                        .unwrap_or_else(|err| Value::error(err, head))
                    } else {
                        item
                    }
                })
                .into_pipeline_data_with_metadata(head, engine_state.signals().clone(), metadata))
        // If columns are given, but input does not use columns, return an error