Skip to content

Prevent DeepCloneFrom self-clone from clearing collections - #65

Merged
matt-edmondson merged 2 commits into
mainfrom
copilot/fix-deepclone-empty-destination
Sep 13, 2026
Merged

matt-edmondson merged 2 commits into
mainfrom
copilot/fix-deepclone-empty-destination

Conversation

Copilot AI commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

DeepCloneFrom previously cleared the destination before enumeration, which silently emptied data when dest and source were the same instance. This broke the intended in-place clone pattern (collection.DeepCloneFrom(collection)).

  • Core fix (collection + dictionary overloads)

    • Snapshot source items before Clear() in:
      • DeepCloneFrom<T>(ICollection<T> dest, IEnumerable<T> source)
      • DeepCloneFrom<TKey, TValue>(IDictionary<TKey, TValue> dest, IDictionary<TKey, TValue> source)
    • This preserves original elements for cloning even when source and destination reference the same object.
  • Regression coverage

    • Added focused tests for same-instance in-place cloning:
      • DeepCloneFrom_SameCollectionInstance_ShouldPreserveAndCloneItems
      • DeepCloneFrom_SameDictionaryInstance_ShouldPreserveAndCloneItems
    • Tests assert both data preservation and replacement with deep-cloned element instances.
T[] items = [.. source];
dest.Clear();
foreach (T? item in items)
{
    dest.Add(DeepClone(item));
}

Co-authored-by: matt-edmondson <19528727+matt-edmondson@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix DeepCloneFrom to preserve destination collection Prevent DeepCloneFrom self-clone from clearing collections Sep 13, 2026
@matt-edmondson
matt-edmondson marked this pull request as ready for review September 13, 2026 12:04
@sonarqubecloud

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit ed04421 into main Sep 13, 2026
12 checks passed
@matt-edmondson
matt-edmondson deleted the copilot/fix-deepclone-empty-destination branch September 13, 2026 12:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DeepCloneFrom empties the destination when dest and source are the same collection instance

2 participants