Skip to content

diffguard-lsp: apply_incremental_change() does full-document clone on every change #574

@EffortlessSteven

Description

@EffortlessSteven

Location: crates/diffguard-lsp/src/text.rs:63

Problem: When change.range is None (full document replacement), the code does *text = change.text.clone(). This clones the entire new text content, but text is being replaced entirely — ownership could be taken directly instead of cloning.

let Some(range) = change.range else {
    *text = change.text.clone();  // clones entire document
    return Ok(());
};

Why this matters: On large documents, this causes unnecessary heap allocation and copying on every full-document sync (common during file saves, initial opens).

Suggested fix: Use std::mem::take() or std::mem::replace() to take ownership of the incoming text without cloning:

let Some(range) = change.range else {
    text.clear();
    text.push_str(&change.text);
    return Ok(());
};

Or if the API allows: pass change.text by value and let the caller handle ownership transfer.

Context: assigning_clones clippy lint fires here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions