Skip to content

Commit 9e29fc1

Browse files
authored
Use ArgumentOutOfRangeException.ThrowIf* across codebase (#8403)
* apply CA1512 * rollback erroneous change a bug in the fixer?
1 parent 1210a7b commit 9e29fc1

File tree

34 files changed

+73
-229
lines changed

34 files changed

+73
-229
lines changed

src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Ink/InkSerializedFormat/GorillaCodec.cs

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -377,10 +377,7 @@ internal void Compress(int bitCount, int[] input, int startInputIndex, DeltaDelt
377377
{
378378
throw new ArgumentNullException(StrokeCollectionSerializer.ISFDebugMessage("input or compressed data was null in Compress"));
379379
}
380-
if (bitCount < 0)
381-
{
382-
throw new ArgumentOutOfRangeException("bitCount");
383-
}
380+
ArgumentOutOfRangeException.ThrowIfNegative(bitCount);
384381

385382
if (bitCount == 0)
386383
{
@@ -428,14 +425,8 @@ internal void Compress(int bitCount, BitStreamReader reader, GorillaEncodingType
428425
{
429426
throw new ArgumentNullException(StrokeCollectionSerializer.ISFDebugMessage("reader or compressedData was null in compress"));
430427
}
431-
if (bitCount < 0)
432-
{
433-
throw new ArgumentOutOfRangeException("bitCount");
434-
}
435-
if (unitsToEncode < 0)
436-
{
437-
throw new ArgumentOutOfRangeException("unitsToEncode");
438-
}
428+
ArgumentOutOfRangeException.ThrowIfNegative(bitCount);
429+
ArgumentOutOfRangeException.ThrowIfNegative(unitsToEncode);
439430

440431
if (bitCount == 0)
441432
{
@@ -518,20 +509,11 @@ private int GetDataFromReader(BitStreamReader reader, GorillaEncodingType type)
518509
internal uint Uncompress(int bitCount, byte[] input, int inputIndex, DeltaDelta dtxf, int[] outputBuffer, int outputBufferIndex)
519510
{
520511
ArgumentNullException.ThrowIfNull(input);
521-
if (inputIndex >= input.Length)
522-
{
523-
throw new ArgumentOutOfRangeException("inputIndex");
524-
}
512+
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(inputIndex, input.Length);
525513
ArgumentNullException.ThrowIfNull(outputBuffer);
526-
if (outputBufferIndex >= outputBuffer.Length)
527-
{
528-
throw new ArgumentOutOfRangeException("outputBufferIndex");
529-
}
514+
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(outputBufferIndex, outputBuffer.Length);
530515

531-
if (bitCount < 0)
532-
{
533-
throw new ArgumentOutOfRangeException("bitCount");
534-
}
516+
ArgumentOutOfRangeException.ThrowIfNegative(bitCount);
535517

536518
// Adjust bit count if 0 passed in
537519
if (bitCount == 0)
@@ -603,14 +585,8 @@ internal uint Uncompress(int bitCount, byte[] input, int inputIndex, DeltaDelta
603585
internal byte[] Uncompress(int bitCount, BitStreamReader reader, GorillaEncodingType encodingType, int unitsToDecode)
604586
{
605587
ArgumentNullException.ThrowIfNull(reader);
606-
if (bitCount < 0)
607-
{
608-
throw new ArgumentOutOfRangeException("bitCount");
609-
}
610-
if (unitsToDecode < 0)
611-
{
612-
throw new ArgumentOutOfRangeException("unitsToDecode");
613-
}
588+
ArgumentOutOfRangeException.ThrowIfNegative(bitCount);
589+
ArgumentOutOfRangeException.ThrowIfNegative(unitsToDecode);
614590

615591
int bitsToWrite = 0;
616592

src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Ink/InkSerializedFormat/MultiByteCodec.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,7 @@ internal uint Decode(byte[] input, int inputIndex, ref uint data)
112112
internal uint SignDecode(byte[] input, int inputIndex, ref int data)
113113
{
114114
Debug.Assert(input != null); //already validated at the AlgoModule level
115-
if (inputIndex >= input.Length)
116-
{
117-
throw new ArgumentOutOfRangeException("inputIndex");
118-
}
115+
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(inputIndex, input.Length);
119116
uint xfData = 0;
120117
uint cb = Decode(input, inputIndex, ref xfData);
121118
data = (0 != (0x01 & xfData)) ? -(int)(xfData >> 1) : (int)(xfData >> 1);

src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/PartialArray.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,7 @@ public void CopyTo(T[] array, int arrayIndex)
145145
"array");
146146
}
147147

148-
if (arrayIndex < 0)
149-
{
150-
throw new ArgumentOutOfRangeException("arrayIndex");
151-
}
148+
ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex);
152149

153150
if (arrayIndex >= array.Length)
154151
{

src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Text/TextInterface/Factory.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,7 @@ internal FontFace CreateFontFace(Uri filePathUri, uint faceIndex, FontSimulation
206206
&hr
207207
))
208208
{
209-
if (faceIndex >= numberOfFaces)
210-
{
211-
throw new ArgumentOutOfRangeException("faceIndex");
212-
}
209+
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(faceIndex, numberOfFaces);
213210

214211
byte dwriteFontSimulationsFlags = DWriteTypeConverter.Convert(fontSimulationFlags);
215212
IDWriteFontFace* dwriteFontFace = null;

src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/TextFormatting/ThousandthOfEmRealDoubles.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,7 @@ public void CopyTo(double[] array, int arrayIndex)
187187
"array");
188188
}
189189

190-
if (arrayIndex < 0)
191-
{
192-
throw new ArgumentOutOfRangeException("arrayIndex");
193-
}
190+
ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex);
194191

195192
if (arrayIndex >= array.Length)
196193
{

src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/TextFormatting/ThousandthOfEmRealPoints.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,7 @@ public void CopyTo(Point[] array, int arrayIndex)
130130
"array");
131131
}
132132

133-
if (arrayIndex < 0)
134-
{
135-
throw new ArgumentOutOfRangeException("arrayIndex");
136-
}
133+
ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex);
137134

138135
if (arrayIndex >= array.Length)
139136
{

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetricsDictionary.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,7 @@ public void CopyTo(KeyValuePair<int, CharacterMetrics>[] array, int index)
133133
{
134134
ArgumentNullException.ThrowIfNull(array);
135135

136-
if (index < 0)
137-
throw new ArgumentOutOfRangeException("index");
136+
ArgumentOutOfRangeException.ThrowIfNegative(index);
138137

139138
if (index >= array.Length)
140139
throw new ArgumentException(SR.Format(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, "index", "array"));
@@ -191,8 +190,7 @@ void SC.ICollection.CopyTo(Array array, int index)
191190
{
192191
ArgumentNullException.ThrowIfNull(array);
193192

194-
if (index < 0)
195-
throw new ArgumentOutOfRangeException("index");
193+
ArgumentOutOfRangeException.ThrowIfNegative(index);
196194

197195
if (index >= array.Length)
198196
throw new ArgumentException(SR.Format(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, "index", "array"));

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CultureSpecificStringDictionary.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,7 @@ public void CopyTo(KeyValuePair<XmlLanguage, string>[] array, int index)
122122
{
123123
ArgumentNullException.ThrowIfNull(array);
124124

125-
if (index < 0)
126-
throw new ArgumentOutOfRangeException("index");
125+
ArgumentOutOfRangeException.ThrowIfNegative(index);
127126

128127
if (index >= array.Length)
129128
throw new ArgumentException(SR.Format(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, "index", "array"));
@@ -157,8 +156,7 @@ void SC.ICollection.CopyTo(Array array, int index)
157156
{
158157
ArgumentNullException.ThrowIfNull(array);
159158

160-
if (index < 0)
161-
throw new ArgumentOutOfRangeException("index");
159+
ArgumentOutOfRangeException.ThrowIfNegative(index);
162160

163161
if (index >= array.Length)
164162
throw new ArgumentException(SR.Format(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, "index", "array"));

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/VisualCollection.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -794,18 +794,9 @@ public void RemoveRange(int index, int count)
794794
VerifyAPIReadWrite();
795795

796796
// Do we really need this extra check index >= _size.
797-
if (index < 0)
798-
{
799-
throw new ArgumentOutOfRangeException(nameof(index));
800-
}
801-
if (count < 0)
802-
{
803-
throw new ArgumentOutOfRangeException(nameof(count));
804-
}
805-
if (_size - index < count)
806-
{
807-
throw new ArgumentOutOfRangeException(nameof(index));
808-
}
797+
ArgumentOutOfRangeException.ThrowIfNegative(index);
798+
ArgumentOutOfRangeException.ThrowIfNegative(count);
799+
ArgumentOutOfRangeException.ThrowIfGreaterThan(count, _size - index);
809800

810801
if (count > 0)
811802
{

src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/CollectionViewProxy.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,7 @@ public override bool PassesFilter(object item)
351351
public override object GetItemAt(int index)
352352
{
353353
// only check lower bound because Count could be expensive
354-
if (index < 0)
355-
throw new ArgumentOutOfRangeException("index");
354+
ArgumentOutOfRangeException.ThrowIfNegative(index);
356355
return EnumerableWrapper[index];
357356
}
358357

0 commit comments

Comments
 (0)