Skip to content

Commit 843b197

Browse files
authored
Use MemberNotNullAttribute in ImmutableArray (dotnet#119898)
1 parent 4725e36 commit 843b197

3 files changed

Lines changed: 37 additions & 35 deletions

File tree

src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Minimal.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public void CopyTo(T[] destination)
227227
{
228228
ImmutableArray<T> self = this;
229229
self.ThrowNullRefIfNotInitialized();
230-
Array.Copy(self.array!, destination, self.Length);
230+
Array.Copy(self.array, destination, self.Length);
231231
}
232232

233233
/// <summary>
@@ -239,7 +239,7 @@ public void CopyTo(T[] destination, int destinationIndex)
239239
{
240240
ImmutableArray<T> self = this;
241241
self.ThrowNullRefIfNotInitialized();
242-
Array.Copy(self.array!, 0, destination, destinationIndex, self.Length);
242+
Array.Copy(self.array, 0, destination, destinationIndex, self.Length);
243243
}
244244

245245
/// <summary>
@@ -253,7 +253,7 @@ public void CopyTo(int sourceIndex, T[] destination, int destinationIndex, int l
253253
{
254254
ImmutableArray<T> self = this;
255255
self.ThrowNullRefIfNotInitialized();
256-
Array.Copy(self.array!, sourceIndex, destination, destinationIndex, length);
256+
Array.Copy(self.array, sourceIndex, destination, destinationIndex, length);
257257
}
258258

259259
/// <summary>
@@ -282,7 +282,7 @@ public Enumerator GetEnumerator()
282282
{
283283
ImmutableArray<T> self = this;
284284
self.ThrowNullRefIfNotInitialized();
285-
return new Enumerator(self.array!);
285+
return new Enumerator(self.array);
286286
}
287287

288288
/// <summary>
@@ -388,7 +388,7 @@ IEnumerator<T> IEnumerable<T>.GetEnumerator()
388388
{
389389
ImmutableArray<T> self = this;
390390
self.ThrowInvalidOperationIfNotInitialized();
391-
return EnumeratorObject.Create(self.array!);
391+
return EnumeratorObject.Create(self.array);
392392
}
393393

394394
/// <summary>
@@ -400,12 +400,13 @@ IEnumerator IEnumerable.GetEnumerator()
400400
{
401401
ImmutableArray<T> self = this;
402402
self.ThrowInvalidOperationIfNotInitialized();
403-
return EnumeratorObject.Create(self.array!);
403+
return EnumeratorObject.Create(self.array);
404404
}
405405

406406
/// <summary>
407407
/// Throws a null reference exception if the array field is null.
408408
/// </summary>
409+
[MemberNotNull(nameof(array))]
409410
internal void ThrowNullRefIfNotInitialized()
410411
{
411412
// Force NullReferenceException if array is null by touching its Length.
@@ -427,9 +428,10 @@ internal void ThrowNullRefIfNotInitialized()
427428
///
428429
/// This is intended for explicitly implemented interface method and property implementations.
429430
/// </summary>
431+
[MemberNotNull(nameof(array))]
430432
private void ThrowInvalidOperationIfNotInitialized()
431433
{
432-
if (this.IsDefault)
434+
if (this.array == null)
433435
{
434436
throw new InvalidOperationException(SR.InvalidOperationOnDefaultArray);
435437
}

src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,13 @@ public int IndexOf(T item, int startIndex, int count, IEqualityComparer<T>? equa
179179
equalityComparer ??= EqualityComparer<T>.Default;
180180
if (equalityComparer == EqualityComparer<T>.Default)
181181
{
182-
return Array.IndexOf(self.array!, item, startIndex, count);
182+
return Array.IndexOf(self.array, item, startIndex, count);
183183
}
184184
else
185185
{
186186
for (int i = startIndex; i < startIndex + count; i++)
187187
{
188-
if (equalityComparer.Equals(self.array![i], item))
188+
if (equalityComparer.Equals(self.array[i], item))
189189
{
190190
return i;
191191
}
@@ -264,13 +264,13 @@ public int LastIndexOf(T item, int startIndex, int count, IEqualityComparer<T>?
264264
equalityComparer ??= EqualityComparer<T>.Default;
265265
if (equalityComparer == EqualityComparer<T>.Default)
266266
{
267-
return Array.LastIndexOf(self.array!, item, startIndex, count);
267+
return Array.LastIndexOf(self.array, item, startIndex, count);
268268
}
269269
else
270270
{
271271
for (int i = startIndex; i >= startIndex - count + 1; i--)
272272
{
273-
if (equalityComparer.Equals(item, self.array![i]))
273+
if (equalityComparer.Equals(item, self.array[i]))
274274
{
275275
return i;
276276
}
@@ -326,11 +326,11 @@ public ImmutableArray<T> Insert(int index, T item)
326326

327327
if (index != 0)
328328
{
329-
Array.Copy(self.array!, tmp, index);
329+
Array.Copy(self.array, tmp, index);
330330
}
331331
if (index != self.Length)
332332
{
333-
Array.Copy(self.array!, index, tmp, index + 1, self.Length - index);
333+
Array.Copy(self.array, index, tmp, index + 1, self.Length - index);
334334
}
335335

336336
return new ImmutableArray<T>(tmp);
@@ -364,11 +364,11 @@ public ImmutableArray<T> InsertRange(int index, IEnumerable<T> items)
364364

365365
if (index != 0)
366366
{
367-
Array.Copy(self.array!, tmp, index);
367+
Array.Copy(self.array, tmp, index);
368368
}
369369
if (index != self.Length)
370370
{
371-
Array.Copy(self.array!, index, tmp, index + count, self.Length - index);
371+
Array.Copy(self.array, index, tmp, index + count, self.Length - index);
372372
}
373373

374374
// We want to copy over the items we need to insert.
@@ -464,7 +464,7 @@ public ImmutableArray<T> AddRange(T[] items, int length)
464464
}
465465

466466
T[] tmp = new T[self.Length + length];
467-
Array.Copy(self.array!, tmp, self.Length);
467+
Array.Copy(self.array, tmp, self.Length);
468468
Array.Copy(items, 0, tmp, self.Length, length);
469469

470470
return new ImmutableArray<T>(tmp);
@@ -488,7 +488,7 @@ public ImmutableArray<T> AddRange<TDerived>(TDerived[] items) where TDerived : T
488488
}
489489

490490
T[] tmp = new T[self.Length + items.Length];
491-
Array.Copy(self.array!, tmp, self.Length);
491+
Array.Copy(self.array, tmp, self.Length);
492492
Array.Copy(items, 0, tmp, self.Length, items.Length);
493493

494494
return new ImmutableArray<T>(tmp);
@@ -558,7 +558,7 @@ public ImmutableArray<T> SetItem(int index, T item)
558558
Requires.Range(index >= 0 && index < self.Length, nameof(index));
559559

560560
T[] tmp = new T[self.Length];
561-
Array.Copy(self.array!, tmp, self.Length);
561+
Array.Copy(self.array, tmp, self.Length);
562562
tmp[index] = item;
563563
return new ImmutableArray<T>(tmp);
564564
}
@@ -658,8 +658,8 @@ public ImmutableArray<T> RemoveRange(int index, int length)
658658
}
659659

660660
T[] tmp = new T[self.Length - length];
661-
Array.Copy(self.array!, tmp, index);
662-
Array.Copy(self.array!, index + length, tmp, index, self.Length - index - length);
661+
Array.Copy(self.array, tmp, index);
662+
Array.Copy(self.array, index + length, tmp, index, self.Length - index - length);
663663
return new ImmutableArray<T>(tmp);
664664
}
665665

@@ -757,7 +757,7 @@ public ImmutableArray<T> RemoveAll(Predicate<T> match)
757757
}
758758

759759
List<int>? removeIndices = null;
760-
for (int i = 0; i < self.array!.Length; i++)
760+
for (int i = 0; i < self.array.Length; i++)
761761
{
762762
if (match(self.array[i]))
763763
{
@@ -838,7 +838,7 @@ public ImmutableArray<T> Sort(int index, int count, IComparer<T>? comparer)
838838
bool outOfOrder = false;
839839
for (int i = index + 1; i < index + count; i++)
840840
{
841-
if (comparer.Compare(self.array![i - 1], self.array[i]) > 0)
841+
if (comparer.Compare(self.array[i - 1], self.array[i]) > 0)
842842
{
843843
outOfOrder = true;
844844
break;
@@ -848,7 +848,7 @@ public ImmutableArray<T> Sort(int index, int count, IComparer<T>? comparer)
848848
if (outOfOrder)
849849
{
850850
var tmp = new T[self.Length];
851-
Array.Copy(self.array!, tmp, self.Length);
851+
Array.Copy(self.array, tmp, self.Length);
852852
Array.Sort(tmp, index, count, comparer);
853853
return new ImmutableArray<T>(tmp);
854854
}
@@ -1372,7 +1372,7 @@ void ICollection.CopyTo(Array array, int index)
13721372
{
13731373
ImmutableArray<T> self = this;
13741374
self.ThrowInvalidOperationIfNotInitialized();
1375-
Array.Copy(self.array!, 0, array, index, self.Length);
1375+
Array.Copy(self.array, 0, array, index, self.Length);
13761376
}
13771377

13781378
/// <summary>
@@ -1494,13 +1494,13 @@ private ImmutableArray<T> RemoveAtRange(ICollection<int> indicesToRemove)
14941494
{
14951495
int copyLength = lastIndexRemoved == -1 ? indexToRemove : (indexToRemove - lastIndexRemoved - 1);
14961496
Debug.Assert(indexToRemove > lastIndexRemoved); // We require that the input be a sorted set.
1497-
Array.Copy(self.array!, copied + removed, newArray, copied, copyLength);
1497+
Array.Copy(self.array, copied + removed, newArray, copied, copyLength);
14981498
removed++;
14991499
copied += copyLength;
15001500
lastIndexRemoved = indexToRemove;
15011501
}
15021502

1503-
Array.Copy(self.array!, copied + removed, newArray, copied, self.Length - (copied + removed));
1503+
Array.Copy(self.array, copied + removed, newArray, copied, self.Length - (copied + removed));
15041504

15051505
return new ImmutableArray<T>(newArray);
15061506
}

src/libraries/System.Collections.Immutable/src/System/Linq/ImmutableArrayExtensions.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static IEnumerable<TResult> Select<T, TResult>(this ImmutableArray<T> imm
2828
// LINQ Select/Where have optimized treatment for arrays.
2929
// They also do not modify the source arrays or expose them to modifications.
3030
// Therefore we will just apply Select/Where to the underlying this.array array.
31-
return immutableArray.array!.Select(selector);
31+
return immutableArray.array.Select(selector);
3232
}
3333

3434
/// <summary>
@@ -86,7 +86,7 @@ public static IEnumerable<T> Where<T>(this ImmutableArray<T> immutableArray, Fun
8686
// LINQ Select/Where have optimized treatment for arrays.
8787
// They also do not modify the source arrays or expose them to modifications.
8888
// Therefore we will just apply Select/Where to the underlying this.array array.
89-
return immutableArray.array!.Where(predicate);
89+
return immutableArray.array.Where(predicate);
9090
}
9191

9292
/// <summary>
@@ -111,7 +111,7 @@ public static bool Any<T>(this ImmutableArray<T> immutableArray, Func<T, bool> p
111111
immutableArray.ThrowNullRefIfNotInitialized();
112112
Requires.NotNull(predicate, nameof(predicate));
113113

114-
foreach (T v in immutableArray.array!)
114+
foreach (T v in immutableArray.array)
115115
{
116116
if (predicate(v))
117117
{
@@ -137,7 +137,7 @@ public static bool All<T>(this ImmutableArray<T> immutableArray, Func<T, bool> p
137137
immutableArray.ThrowNullRefIfNotInitialized();
138138
Requires.NotNull(predicate, nameof(predicate));
139139

140-
foreach (T v in immutableArray.array!)
140+
foreach (T v in immutableArray.array)
141141
{
142142
if (!predicate(v))
143143
{
@@ -171,7 +171,7 @@ public static bool SequenceEqual<TDerived, TBase>(this ImmutableArray<TBase> imm
171171

172172
for (int i = 0; i < immutableArray.Length; i++)
173173
{
174-
if (!comparer.Equals(immutableArray.array![i], items.array![i]))
174+
if (!comparer.Equals(immutableArray.array[i], items.array[i]))
175175
{
176176
return false;
177177
}
@@ -429,7 +429,7 @@ public static T Last<T>(this ImmutableArray<T> immutableArray, Func<T, bool> pre
429429
public static T? LastOrDefault<T>(this ImmutableArray<T> immutableArray)
430430
{
431431
immutableArray.ThrowNullRefIfNotInitialized();
432-
return immutableArray.array!.LastOrDefault()!;
432+
return immutableArray.array.LastOrDefault()!;
433433
}
434434

435435
/// <summary>
@@ -459,7 +459,7 @@ public static T Last<T>(this ImmutableArray<T> immutableArray, Func<T, bool> pre
459459
public static T Single<T>(this ImmutableArray<T> immutableArray)
460460
{
461461
immutableArray.ThrowNullRefIfNotInitialized();
462-
return immutableArray.array!.Single();
462+
return immutableArray.array.Single();
463463
}
464464

465465
/// <summary>
@@ -504,7 +504,7 @@ public static T Single<T>(this ImmutableArray<T> immutableArray, Func<T, bool> p
504504
public static T? SingleOrDefault<T>(this ImmutableArray<T> immutableArray)
505505
{
506506
immutableArray.ThrowNullRefIfNotInitialized();
507-
return immutableArray.array!.SingleOrDefault()!;
507+
return immutableArray.array.SingleOrDefault()!;
508508
}
509509

510510
/// <summary>
@@ -618,7 +618,7 @@ public static Dictionary<TKey, TElement> ToDictionary<TKey, TElement, T>(this Im
618618
public static T[] ToArray<T>(this ImmutableArray<T> immutableArray)
619619
{
620620
immutableArray.ThrowNullRefIfNotInitialized();
621-
if (immutableArray.array!.Length == 0)
621+
if (immutableArray.array.Length == 0)
622622
{
623623
return ImmutableArray<T>.Empty.array!;
624624
}

0 commit comments

Comments
 (0)