Skip to content

Commit

Permalink
Remove extraneous use of unchecked from System.Linq (#96622)
Browse files Browse the repository at this point in the history
The code doesn't use a checked configuration, and if it did there are other expected overflows that would erroneously fail.
  • Loading branch information
stephentoub authored Jan 8, 2024
1 parent 17335f4 commit 0451127
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 25 deletions.
26 changes: 13 additions & 13 deletions src/libraries/System.Linq/src/System/Linq/Partition.SpeedOpt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ public OrderedPartition(OrderedEnumerable<TElement> source, int minIdxInclusive,

public IPartition<TElement>? Skip(int count)
{
int minIndex = unchecked(_minIndexInclusive + count);
return unchecked((uint)minIndex > (uint)_maxIndexInclusive) ? null : new OrderedPartition<TElement>(_source, minIndex, _maxIndexInclusive);
int minIndex = _minIndexInclusive + count;
return (uint)minIndex > (uint)_maxIndexInclusive ? null : new OrderedPartition<TElement>(_source, minIndex, _maxIndexInclusive);
}

public IPartition<TElement> Take(int count)
{
int maxIndex = unchecked(_minIndexInclusive + count - 1);
if (unchecked((uint)maxIndex >= (uint)_maxIndexInclusive))
int maxIndex = _minIndexInclusive + count - 1;
if ((uint)maxIndex >= (uint)_maxIndexInclusive)
{
return this;
}
Expand All @@ -43,7 +43,7 @@ public IPartition<TElement> Take(int count)

public TElement? TryGetElementAt(int index, out bool found)
{
if (unchecked((uint)index <= (uint)(_maxIndexInclusive - _minIndexInclusive)))
if ((uint)index <= (uint)(_maxIndexInclusive - _minIndexInclusive))
{
return _source.TryGetElementAt(index + _minIndexInclusive, out found);
}
Expand Down Expand Up @@ -96,7 +96,7 @@ public override bool MoveNext()
// Having a separate field for the index would be more readable. However, we save it
// into _state with a bias to minimize field size of the iterator.
int index = _state - 1;
if (unchecked((uint)index <= (uint)(_maxIndexInclusive - _minIndexInclusive) && index < _source.Count - _minIndexInclusive))
if ((uint)index <= (uint)(_maxIndexInclusive - _minIndexInclusive) && index < _source.Count - _minIndexInclusive)
{
_current = _source[_minIndexInclusive + index];
++_state;
Expand All @@ -118,13 +118,13 @@ public override IEnumerable<TResult> Select<TResult>(Func<TSource, TResult> sele

public IPartition<TSource> Take(int count)
{
int maxIndex = unchecked(_minIndexInclusive + count - 1);
return unchecked((uint)maxIndex >= (uint)_maxIndexInclusive) ? this : new ListPartition<TSource>(_source, _minIndexInclusive, maxIndex);
int maxIndex = _minIndexInclusive + count - 1;
return (uint)maxIndex >= (uint)_maxIndexInclusive ? this : new ListPartition<TSource>(_source, _minIndexInclusive, maxIndex);
}

public TSource? TryGetElementAt(int index, out bool found)
{
if (unchecked((uint)index <= (uint)(_maxIndexInclusive - _minIndexInclusive) && index < _source.Count - _minIndexInclusive))
if ((uint)index <= (uint)(_maxIndexInclusive - _minIndexInclusive) && index < _source.Count - _minIndexInclusive)
{
found = true;
return _source[_minIndexInclusive + index];
Expand Down Expand Up @@ -286,7 +286,7 @@ internal EnumerablePartition(IEnumerable<TSource> source, int minIndexInclusive,
// on how many elements we can have.
private bool HasLimit => _maxIndexInclusive != -1;

private int Limit => unchecked((_maxIndexInclusive + 1) - _minIndexInclusive); // This is that upper bound.
private int Limit => _maxIndexInclusive + 1 - _minIndexInclusive; // This is that upper bound.

public override Iterator<TSource> Clone() =>
new EnumerablePartition<TSource>(_source, _minIndexInclusive, _maxIndexInclusive);
Expand Down Expand Up @@ -388,7 +388,7 @@ public override IEnumerable<TResult> Select<TResult>(Func<TSource, TResult> sele

public IPartition<TSource>? Skip(int count)
{
int minIndex = unchecked(_minIndexInclusive + count);
int minIndex = _minIndexInclusive + count;

if (!HasLimit)
{
Expand All @@ -414,7 +414,7 @@ public override IEnumerable<TResult> Select<TResult>(Func<TSource, TResult> sele

public IPartition<TSource> Take(int count)
{
int maxIndex = unchecked(_minIndexInclusive + count - 1);
int maxIndex = _minIndexInclusive + count - 1;
if (!HasLimit)
{
if (maxIndex < 0)
Expand All @@ -428,7 +428,7 @@ public IPartition<TSource> Take(int count)
return new EnumerablePartition<TSource>(this, 0, count - 1);
}
}
else if (unchecked((uint)maxIndex >= (uint)_maxIndexInclusive))
else if ((uint)maxIndex >= (uint)_maxIndexInclusive)
{
// If we don't know our max count, we can't go down this branch.
// It's always possible for us to contain more than count items, as the rest
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/System.Linq/src/System/Linq/Range.SpeedOpt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private static void Fill(Span<int> destination, int value)
}
}

public int GetCount(bool onlyIfCheap) => unchecked(_end - _start);
public int GetCount(bool onlyIfCheap) => _end - _start;

public int Count => _end - _start;

Expand All @@ -95,7 +95,7 @@ public IPartition<int> Take(int count)

public int TryGetElementAt(int index, out bool found)
{
if (unchecked((uint)index < (uint)(_end - _start)))
if ((uint)index < (uint)(_end - _start))
{
found = true;
return _start + index;
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/System.Linq/src/System/Linq/Range.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public RangeIterator(int start, int count)
{
Debug.Assert(count > 0);
_start = start;
_end = unchecked(start + count);
_end = start + count;
}

private int CountForDebugger => _end - _start;
Expand All @@ -54,7 +54,7 @@ public override bool MoveNext()
_state = 2;
return true;
case 2:
if (unchecked(++_current) == _end)
if (++_current == _end)
{
break;
}
Expand Down
8 changes: 4 additions & 4 deletions src/libraries/System.Linq/src/System/Linq/Select.SpeedOpt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public IPartition<TResult> Take(int count)

public TResult? TryGetElementAt(int index, out bool found)
{
if (unchecked((uint)index < (uint)_source.Length))
if ((uint)index < (uint)_source.Length)
{
found = true;
return _selector(_source[index]);
Expand Down Expand Up @@ -364,7 +364,7 @@ public IPartition<TResult> Take(int count)

public TResult? TryGetElementAt(int index, out bool found)
{
if (unchecked((uint)index < (uint)_source.Count))
if ((uint)index < (uint)_source.Count)
{
found = true;
return _selector(_source[index]);
Expand Down Expand Up @@ -467,7 +467,7 @@ public IPartition<TResult> Take(int count)

public TResult? TryGetElementAt(int index, out bool found)
{
if (unchecked((uint)index < (uint)_source.Count))
if ((uint)index < (uint)_source.Count)
{
found = true;
return _selector(_source[index]);
Expand Down Expand Up @@ -733,7 +733,7 @@ public override bool MoveNext()
// Having a separate field for the index would be more readable. However, we save it
// into _state with a bias to minimize field size of the iterator.
int index = _state - 1;
if (unchecked((uint)index <= (uint)(_maxIndexInclusive - _minIndexInclusive) && index < _source.Count - _minIndexInclusive))
if ((uint)index <= (uint)(_maxIndexInclusive - _minIndexInclusive) && index < _source.Count - _minIndexInclusive)
{
_current = _selector(_source[_minIndexInclusive + index]);
++_state;
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/System.Linq/src/System/Linq/Where.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public override bool MoveNext()
int index = _state - 1;
TSource[] source = _source;

while (unchecked((uint)index < (uint)source.Length))
while ((uint)index < (uint)source.Length)
{
TSource item = source[index];
index = _state++;
Expand Down Expand Up @@ -276,7 +276,7 @@ public override bool MoveNext()
int index = _state - 1;
TSource[] source = _source;

while (unchecked((uint)index < (uint)source.Length))
while ((uint)index < (uint)source.Length)
{
TSource item = source[index];
index = _state++;
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/System.Linq/tests/SelectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,7 @@ public static IEnumerable<object[]> RunSelectorDuringCountData()
e => new LinkedList<int>(e) // Implements IList<T>.
};

var r = new Random(unchecked((int)0x984bf1a3));
var r = new Random(42);

for (int i = 0; i <= 5; i++)
{
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/System.Linq/tests/WhereTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,7 @@ public static IEnumerable<object[]> ToCollectionData()

private static IEnumerable<int> GenerateRandomSequnce(uint seed, int count)
{
var random = new Random(unchecked((int)seed));
var random = new Random((int)seed);

for (int i = 0; i < count; i++)
{
Expand Down

0 comments on commit 0451127

Please sign in to comment.