Skip to content

diffguard-analytics: clone_from() instead of repeated field.clone() assignments #575

@EffortlessSteven

Description

@EffortlessSteven

Location: crates/diffguard-analytics/src/lib.rs:119-128

Problem: In the merge_baseline_with() function, when merging entries that already exist in the baseline, fields are assigned via repeated .clone() calls:

if existing.note.is_none() && entry.note.is_some() {
    existing.note = entry.note.clone();
}
if existing.rule_id.is_empty() {
    existing.rule_id = entry.rule_id.clone();
}
if existing.path.is_empty() {
    existing.path = entry.path.clone();
}

The assigning_clones clippy lint flags this as inefficient — clone_from() should be used instead when the destination already exists.

Why this matters: clone_from() can reuse existing heap allocations in the destination (especially for String), avoiding redundant allocations that .clone() would create.

Suggested fix: Use clone_from() on owned fields:

if existing.note.is_none() && entry.note.is_some() {
    existing.note.clone_from(&entry.note);
}
if existing.rule_id.is_empty() {
    existing.rule_id.clone_from(&entry.rule_id);
}
if existing.path.is_empty() {
    existing.path.clone_from(&entry.path);
}

Lint output:

warning: assigning the result of `Clone::clone()` may be inefficient
   --> crates/diffguard-analytics/src/lib.rs:121:17
121 |                 existing.note = entry.note.clone();
warning: assigning the result of `Clone::clone()` may be inefficient
   --> crates/diffguard-analytics/src/lib.rs:124:17
124 |                 existing.rule_id = entry.rule_id.clone();
warning: assigning the result of `Clone::clone()` may be inefficient
   --> crates/diffguard-analytics/src/lib.rs:127:17
127 |                 existing.path = entry.path.clone();

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