Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit b47a902

Browse files
committed
Add additional zero-length check to LeafToRootRefill
1 parent c9a1a97 commit b47a902

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet`1.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,16 @@ private ImmutableSortedSet<T> LeafToRootRefill(IEnumerable<T> addedItems)
11071107
List<T> list;
11081108
if (this.IsEmpty)
11091109
{
1110+
// If the additional items enumerable list is known to be empty, too,
1111+
// then just return this empty instance.
1112+
int count;
1113+
if (addedItems.TryGetCount(out count) && count == 0)
1114+
{
1115+
return this;
1116+
}
1117+
1118+
// Otherwise, construct a list from the items. The Count could still
1119+
// be zero, in which case, again, just return this empty instance.
11101120
list = new List<T>(addedItems);
11111121
if (list.Count == 0)
11121122
{
@@ -1115,6 +1125,9 @@ private ImmutableSortedSet<T> LeafToRootRefill(IEnumerable<T> addedItems)
11151125
}
11161126
else
11171127
{
1128+
// Build the list from this set and then add the additional items.
1129+
// Even if the additional items is empty, this set isn't, so we know
1130+
// the resulting list will not be empty.
11181131
list = new List<T>(this);
11191132
list.AddRange(addedItems);
11201133
}

0 commit comments

Comments
 (0)