Repository: nushell/nushell
File: crates/nu-command/src/debug/metadata.rs (Rust)

Code before:

    fn requires_ast_for_arguments(&self) -> bool {
        true
    }

    fn run(
        &self,
        engine_state: &EngineState,
        stack: &mut Stack,
        call: &Call,
        input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        let arg = call.positional_nth(stack, 0);
        let head = call.head;

        let has_input = !matches!(input, PipelineData::Empty);
        let has_argument = arg.is_some();

        if has_input && has_argument {
            return Err(ShellError::IncompatibleParameters {
                left_message: "pipeline input was provided".into(),
                left_span: head,
                right_message: "but a positional metadata expression was also given".into(),
                right_span: arg.unwrap().span,
            });
        }

        if has_input {
            return Ok(Value::record(
                extend_record_with_metadata(Record::new(), input.metadata().as_ref(), call.head),
                call.head,
            )
            .into_pipeline_data());
        }

        match arg {
            Some(Expression {
                expr: Expr::FullCellPath(full_cell_path),
                span,
                ..
            }) => {
                if full_cell_path.tail.is_empty() {
                    match &full_cell_path.head {
                        Expression {
                            expr: Expr::Var(var_id),
                            ..
                        } => {
                            let origin = stack.get_var_with_origin(*var_id, *span)?;
                            Ok(build_metadata_record_value(
                                &origin,
                                input.metadata().as_ref(),

Review comment (suggestion):
You can turn the `has_` checks into an `if let` to avoid the unwrap.

Code after:

    fn requires_ast_for_arguments(&self) -> bool {
        true
    }

    fn run(
        &self,
        engine_state: &EngineState,
        stack: &mut Stack,
        call: &Call,
        input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        let arg = call.positional_nth(stack, 0);
        let head = call.head;

        if !matches!(input, PipelineData::Empty) {
            if let Some(arg_expr) = arg {
                return Err(ShellError::IncompatibleParameters {
                    left_message: "pipeline input was provided".into(),
                    left_span: head,
                    right_message: "but a positional metadata expression was also given".into(),
                    right_span: arg_expr.span,
                });
            }
        }

        match arg {
            Some(Expression {
                expr: Expr::FullCellPath(full_cell_path),
                span,
                ..
            }) => {
                if full_cell_path.tail.is_empty() {
                    match &full_cell_path.head {
                        Expression {
                            expr: Expr::Var(var_id),
                            ..
                        } => {
                            let origin = stack.get_var_with_origin(*var_id, *span)?;
                            Ok(build_metadata_record_value(
                                &origin,
                                input.metadata().as_ref(),
                                head,
                            )
                            .into_pipeline_data())
                        }
                        _ => {
                            let val: Value = call.req(engine_state, stack, 0)?;
                            Ok(
                                build_metadata_record_value(&val, input.metadata().as_ref(), head)
                                    .into_pipeline_data(),