chore: Use var everywhere (#2294)

This commit is contained in:
Kamron Batman 2025-12-27 16:47:28 -08:00 • committed by GitHub
parent 0b34cc4417
commit ebaf104935
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
267 changed files with 873 additions and 873 deletions

View file

@ -44,7 +44,7 @@ public struct ArrayEnumerator<T> : IEnumerator<T>
public bool MoveNext()
{
T[] localList = _array;
var localList = _array;
if ((uint)_index < (uint)localList.Length)
{

View file

@ -43,8 +43,8 @@ internal static class HashHelpers
{
if ((candidate & 1) != 0)
{
int limit = (int)Math.Sqrt(candidate);
for (int divisor = 3; divisor <= limit; divisor += 2)
var limit = (int)Math.Sqrt(candidate);
for (var divisor = 3; divisor <= limit; divisor += 2)
{
if (candidate % divisor == 0)
{
@ -63,9 +63,9 @@ internal static class HashHelpers
throw new ArgumentException("Hashtable's capacity overflowed and went negative. Check load factor, capacity and the current size of the table.");
}
for (int i = 0; i < primes.Length; i++)
for (var i = 0; i < primes.Length; i++)
{
int prime = primes[i];
var prime = primes[i];
if (prime >= min)
{
return prime;
@ -74,7 +74,7 @@ internal static class HashHelpers
//outside of our predefined table.
//compute the hard way.
for (int i = min | 1; i < int.MaxValue; i += 2)
for (var i = min | 1; i < int.MaxValue; i += 2)
{
if (IsPrime(i) && (i - 1) % HashPrime != 0)
{
@ -87,7 +87,7 @@ internal static class HashHelpers
// Returns size of hashtable to grow to.
public static int ExpandPrime(int oldSize)
{
int newSize = 2 * oldSize;
var newSize = 2 * oldSize;
// Allow the hashtables to grow to maximum possible size (~2G elements) before encountering capacity overflow.
// Note that this check works even when _items.Length overflowed thanks to the (uint) cast
@ -116,7 +116,7 @@ internal static class HashHelpers
// This is equivalent of (uint)Math.BigMul(multiplier * value, divisor, out _). This version
// is faster than BigMul currently because we only need the high bits.
uint highbits = (uint)(((((multiplier * value) >> 32) + 1) * divisor) >> 32);
var highbits = (uint)(((((multiplier * value) >> 32) + 1) * divisor) >> 32);
Debug.Assert(highbits == value % divisor);
return highbits;

View file

@ -70,7 +70,7 @@ public ref struct PooledRefList<T>
_version = 0;
_mt = mt;
int count = collection.Count;
var count = collection.Count;
if (count == 0)
{
_items = s_emptyArray;
@ -100,7 +100,7 @@ public ref struct PooledRefList<T>
if (collection is ICollection<T> c)
{
int count = c.Count;
var count = c.Count;
if (count == 0)
{
_items = s_emptyArray;
@ -117,7 +117,7 @@ public ref struct PooledRefList<T>
{
_size = 0;
_items = s_emptyArray;
using IEnumerator<T> en = collection!.GetEnumerator();
using var en = collection!.GetEnumerator();
while (en.MoveNext())
{
Add(en.Current);
@ -143,7 +143,7 @@ public ref struct PooledRefList<T>
{
if (value > 0)
{
T[] newItems = ArrayPool.Rent(value);
var newItems = ArrayPool.Rent(value);
if (_size > 0)
{
Array.Copy(_items, newItems, _size);
@ -211,8 +211,8 @@ public ref struct PooledRefList<T>
public void Add(T item)
{
_version++;
T[] array = _items;
int size = _size;
var array = _items;
var size = _size;
if ((uint)size < (uint)array.Length)
{
_size = size + 1;
@ -229,7 +229,7 @@ public ref struct PooledRefList<T>
private void AddWithResize(T item)
{
Debug.Assert(_size == _items.Length);
int size = _size;
var size = _size;
Grow(size + 1);
_size = size + 1;
_items[size] = item;
@ -292,7 +292,7 @@ public ref struct PooledRefList<T>
_version++;
if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
{
int size = _size;
var size = _size;
_size = 0;
if (size > 0)
{
@ -329,8 +329,8 @@ public ref struct PooledRefList<T>
throw new ArgumentNullException(nameof(converter));
}
PooledRefList<TOutput> list = new PooledRefList<TOutput>(_size);
for (int i = 0; i < _size; i++)
var list = new PooledRefList<TOutput>(_size);
for (var i = 0; i < _size; i++)
{
list._items[i] = converter(_items[i]);
}
@ -393,7 +393,7 @@ public ref struct PooledRefList<T>
{
Debug.Assert(_items.Length < capacity);
int newcapacity = _items.Length == 0 ? DefaultCapacity : 2 * _items.Length;
var newcapacity = _items.Length == 0 ? DefaultCapacity : 2 * _items.Length;
// Allow the list to grow to maximum possible capacity (~2G elements) before encountering overflow.
// Note that this check works even when _items.Length overflowed thanks to the (uint) cast
@ -421,7 +421,7 @@ public ref struct PooledRefList<T>
throw new ArgumentNullException(nameof(match));
}
for (int i = 0; i < _size; i++)
for (var i = 0; i < _size; i++)
{
if (match(_items[i]))
{
@ -438,8 +438,8 @@ public ref struct PooledRefList<T>
throw new ArgumentNullException(nameof(match));
}
PooledRefList<T> list = new PooledRefList<T>();
for (int i = 0; i < _size; i++)
var list = new PooledRefList<T>();
for (var i = 0; i < _size; i++)
{
if (match(_items[i]))
{
@ -470,8 +470,8 @@ public ref struct PooledRefList<T>
throw new ArgumentNullException(nameof(match));
}
int endIndex = startIndex + count;
for (int i = startIndex; i < endIndex; i++)
var endIndex = startIndex + count;
for (var i = startIndex; i < endIndex; i++)
{
if (match(_items[i]))
{
@ -488,7 +488,7 @@ public ref struct PooledRefList<T>
throw new ArgumentNullException(nameof(match));
}
for (int i = _size - 1; i >= 0; i--)
for (var i = _size - 1; i >= 0; i--)
{
if (match(_items[i]))
{
@ -532,8 +532,8 @@ public ref struct PooledRefList<T>
throw new ArgumentOutOfRangeException(nameof(count));
}
int endIndex = startIndex - count;
for (int i = startIndex; i > endIndex; i--)
var endIndex = startIndex - count;
for (var i = startIndex; i > endIndex; i--)
{
if (match(_items[i]))
{
@ -550,9 +550,9 @@ public ref struct PooledRefList<T>
throw new ArgumentNullException(nameof(action));
}
int version = _version;
var version = _version;
for (int i = 0; i < _size; i++)
for (var i = 0; i < _size; i++)
{
if (version != _version)
{
@ -591,7 +591,7 @@ public ref struct PooledRefList<T>
throw new ArgumentException("Length must be greater than zero");
}
PooledRefList<T> list = new PooledRefList<T>(count);
var list = new PooledRefList<T>(count);
Array.Copy(_items, index, list._items, 0, count);
list._size = count;
return list;
@ -694,7 +694,7 @@ public ref struct PooledRefList<T>
if (collection is ICollection<T> c)
{
int count = c.Count;
var count = c.Count;
if (count > 0)
{
if (_items.Length - _size < count)
@ -712,7 +712,7 @@ public ref struct PooledRefList<T>
}
else
{
using IEnumerator<T> en = collection.GetEnumerator();
using var en = collection.GetEnumerator();
while (en.MoveNext())
{
Insert(index++, en.Current);
@ -801,7 +801,7 @@ public ref struct PooledRefList<T>
// decreased by one.
public bool Remove(T item)
{
int index = IndexOf(item);
var index = IndexOf(item);
if (index >= 0)
{
RemoveAt(index);
@ -820,7 +820,7 @@ public ref struct PooledRefList<T>
throw new ArgumentNullException(nameof(match));
}
int freeIndex = 0; // the first free slot in items array
var freeIndex = 0; // the first free slot in items array
// Find the first item which needs to be removed.
while (freeIndex < _size && !match(_items[freeIndex]))
@ -833,7 +833,7 @@ public ref struct PooledRefList<T>
return 0;
}
int current = freeIndex + 1;
var current = freeIndex + 1;
while (current < _size)
{
// Find the first item which needs to be kept.
@ -854,7 +854,7 @@ public ref struct PooledRefList<T>
Array.Clear(_items, freeIndex, _size - freeIndex); // Clear the elements so that the gc can reclaim the references.
}
int result = _size - freeIndex;
var result = _size - freeIndex;
_size = freeIndex;
_version++;
return result;
@ -1002,7 +1002,7 @@ public ref struct PooledRefList<T>
return s_emptyArray;
}
T[] array = new T[_size];
var array = new T[_size];
Array.Copy(_items, array, _size);
return array;
}
@ -1014,7 +1014,7 @@ public ref struct PooledRefList<T>
return s_emptyArray;
}
T[] array = ArrayPool.Rent(_size);
var array = ArrayPool.Rent(_size);
Array.Copy(_items, array, _size);
return array;
}
@ -1030,7 +1030,7 @@ public ref struct PooledRefList<T>
//
public void TrimExcess()
{
int threshold = (int)(_items.Length * 0.9);
var threshold = (int)(_items.Length * 0.9);
if (_size < threshold)
{
Capacity = _size;
@ -1044,7 +1044,7 @@ public ref struct PooledRefList<T>
throw new ArgumentNullException(nameof(match));
}
for (int i = 0; i < _size; i++)
for (var i = 0; i < _size; i++)
{
if (!match(_items[i]))
{
@ -1077,7 +1077,7 @@ public ref struct PooledRefList<T>
public bool MoveNext()
{
PooledRefList<T> localList = _list;
var localList = _list;
if (_version == localList._version && (uint)_index < (uint)localList._size)
{

View file

@ -97,13 +97,13 @@ public ref struct PooledRefQueue<T>
throw new ArgumentException(CollectionThrowStrings.Argument_InvalidOffLen);
}
int numToCopy = _size;
var numToCopy = _size;
if (numToCopy == 0)
{
return;
}
int firstPart = Math.Min(_array.Length - _head, numToCopy);
var firstPart = Math.Min(_array.Length - _head, numToCopy);
Array.Copy(_array, _head, array, arrayIndex, firstPart);
numToCopy -= firstPart;
if (numToCopy > 0)
@ -135,15 +135,15 @@ public ref struct PooledRefQueue<T>
// InvalidOperationException.
public T Dequeue()
{
int head = _head;
T[] array = _array;
var head = _head;
var array = _array;
if (_size == 0)
{
ThrowForEmptyQueue();
}
T removed = array[head];
var removed = array[head];
if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
{
array[head] = default!;
@ -156,8 +156,8 @@ public ref struct PooledRefQueue<T>
public bool TryDequeue([MaybeNullWhen(false)] out T result)
{
int head = _head;
T[] array = _array;
var head = _head;
var array = _array;
if (_size == 0)
{
@ -248,7 +248,7 @@ public ref struct PooledRefQueue<T>
return s_emptyArray;
}
T[] arr = new T[_size];
var arr = new T[_size];
if (_head < _tail)
{
@ -270,7 +270,7 @@ public ref struct PooledRefQueue<T>
return s_emptyArray;
}
T[] arr = (mt ? ArrayPool<T>.Shared : STArrayPool<T>.Shared).Rent(_size);
var arr = (mt ? ArrayPool<T>.Shared : STArrayPool<T>.Shared).Rent(_size);
if (_head < _tail)
{
@ -289,7 +289,7 @@ public ref struct PooledRefQueue<T>
// must be >= _size.
private void SetCapacity(int capacity)
{
T[] newarray = (_mt ? ArrayPool<T>.Shared : STArrayPool<T>.Shared).Rent(capacity);
var newarray = (_mt ? ArrayPool<T>.Shared : STArrayPool<T>.Shared).Rent(capacity);
if (_size > 0)
{
if (_head < _tail)
@ -321,7 +321,7 @@ public ref struct PooledRefQueue<T>
// It is tempting to use the remainder operator here but it is actually much slower
// than a simple comparison and a rarely taken branch.
// JIT produces better code than with ternary operator ?:
int tmp = index + 1;
var tmp = index + 1;
if (tmp == _array.Length)
{
tmp = 0;
@ -359,7 +359,7 @@ public ref struct PooledRefQueue<T>
const int GrowFactor = 2;
const int MinimumGrow = 4;
int newcapacity = GrowFactor * _array.Length;
var newcapacity = GrowFactor * _array.Length;
// Allow the list to grow to maximum possible capacity (~2G elements) before encountering overflow.
// Note that this check works even when _items.Length overflowed thanks to the (uint) cast
@ -441,13 +441,13 @@ public ref struct PooledRefQueue<T>
}
// Cache some fields in locals to decrease code size
T[] array = _q._array;
int capacity = array.Length;
var array = _q._array;
var capacity = array.Length;
// _index represents the 0-based index into the queue, however the queue
// doesn't have to start from 0 and it may not even be stored contiguously in memory.
int arrayIndex = _q._head + _index; // this is the actual index into the queue's backing array
var arrayIndex = _q._head + _index; // this is the actual index into the queue's backing array
if (arrayIndex >= capacity)
{
// NOTE: Originally we were using the modulo operator here, however