Repository: nushell/nushell
File: crates/nu-cli/src/completions/arg_value_completion.rs (Rust)

Code before:
    pub arg_type: ArgType<'a>,
    pub need_fallback: bool,
    pub completer: &'a NuCompleter,
    pub arg_idx: usize,
    pub pos: usize,
    pub strip: bool,
}

impl<'a> Completer for ArgValueCompletion<'a> {
    fn fetch(
        &mut self,
        working_set: &StateWorkingSet,
        stack: &Stack,
        prefix: impl AsRef<str>,
        span: Span,
        offset: usize,
        options: &CompletionOptions,
    ) -> Vec<SemanticSuggestion> {
        // if user input `--foo abc`, then the `prefix` here is abc.
        let mut matcher = NuMatcher::new(prefix.as_ref(), options, true);

        let decl = working_set.get_decl(self.call.decl_id);
        let mut stack = stack.to_owned();

        let dynamic_completion_call =
            DynamicCompletionCall::from_ast_call(self.call.clone(), self.strip, self.pos);
        match decl.get_dynamic_completion(
            working_set.permanent_state,
            &mut stack,
            &dynamic_completion_call,
            &self.arg_type,
        ) {
            Ok(Some(items)) => {
                for i in items {
                    let result_span = i.span.unwrap_or(span);
                    let suggestion = SemanticSuggestion::from_dynamic_suggestion(
                        i,
                        reedline::Span {
                            start: result_span.start - offset,
                            end: result_span.end - offset,
                        },
                        None,
                    );
                    matcher.add_semantic_suggestion(suggestion);
                }
                return matcher.suggestion_results();
            }
            Err(e) => {
                log::error!(
                    "error on fetching dynamic suggestion on {} with {:?}: {e}",
                    decl.name(),

Review comment (suggestion):
Is it possible to avoid this clone? This always gets called, even for those without an implementation.

Code after:
    pub arg_type: ArgType<'a>,
    pub need_fallback: bool,
    pub completer: &'a NuCompleter,
    pub arg_idx: usize,
    pub pos: usize,
    pub strip: bool,
}

impl<'a> Completer for ArgValueCompletion<'a> {
    fn fetch(
        &mut self,
        working_set: &StateWorkingSet,
        stack: &Stack,
        prefix: impl AsRef<str>,
        span: Span,
        offset: usize,
        options: &CompletionOptions,
    ) -> Vec<SemanticSuggestion> {
        // if user input `--foo abc`, then the `prefix` here is abc.
        let mut matcher = NuMatcher::new(prefix.as_ref(), options, true);

        let decl = working_set.get_decl(self.call.decl_id);
        let mut stack = stack.to_owned();

        let dynamic_completion_call = DynamicCompletionCallRef {
            call: self.call,
            strip: self.strip,
            pos: self.pos,
        };
        match decl.get_dynamic_completion(
            working_set.permanent_state,
            &mut stack,
            dynamic_completion_call,
            &self.arg_type,
            #[expect(deprecated, reason = "internal usage")]
            nu_protocol::engine::ExperimentalMarker,
        ) {
            Ok(Some(items)) => {
                for i in items {
                    let result_span = i.span.unwrap_or(span);
                    let suggestion = SemanticSuggestion::from_dynamic_suggestion(
                        i,
                        reedline::Span {
                            start: result_span.start - offset,
                            end: result_span.end - offset,
                        },
                        None,
                    );
                    matcher.add_semantic_suggestion(suggestion);
                }
                return matcher.suggestion_results();