From 4b3a04b6a26ec04c0eda3ed59cf5567e86ea4833 Mon Sep 17 00:00:00 2001 From: halgab <24685886+halgab@users.noreply.github.com> Date: Wed, 8 Nov 2023 10:37:31 +0100 Subject: [PATCH 1/2] apply CA1512 --- .../Ink/InkSerializedFormat/GorillaCodec.cs | 40 ++++--------------- .../Ink/InkSerializedFormat/MultiByteCodec.cs | 5 +-- .../MS/internal/PartialArray.cs | 5 +-- .../MS/internal/Text/TextInterface/Factory.cs | 5 +-- .../ThousandthOfEmRealDoubles.cs | 5 +-- .../ThousandthOfEmRealPoints.cs | 5 +-- .../System/Windows/InterOp/D3DImage.cs | 5 +-- .../Media/CharacterMetricsDictionary.cs | 6 +-- .../Media/CultureSpecificStringDictionary.cs | 6 +-- .../System/Windows/Media/VisualCollection.cs | 15 ++----- .../MS/Internal/Data/CollectionViewProxy.cs | 3 +- .../Internal/Data/CompositeCollectionView.cs | 11 ++--- .../MS/Internal/Data/IndexedEnumerable.cs | 6 +-- .../MS/Internal/Data/RBTree.cs | 3 +- .../MS/Internal/WeakDictionary.cs | 5 +-- .../MS/Internal/WeakHashSet.cs | 5 +-- .../WinRT/Projections/IReadOnlyList.cs | 3 +- .../MS/Internal/documents/DocumentGrid.cs | 15 ++----- .../MS/Internal/documents/PageCache.cs | 27 +++---------- .../MS/Internal/documents/RowCache.cs | 35 ++++------------ .../MS/Internal/documents/TextBoxView.cs | 5 +-- .../GridViewColumnHeaderAutomationPeer.cs | 10 +---- .../System/Windows/Controls/ItemCollection.cs | 27 ++++++------- .../System/Windows/Data/CollectionView.cs | 3 +- .../Documents/TextElementCollection.cs | 5 +-- .../Windows/Markup/Baml2006/SharedStream.cs | 10 +---- .../System/Windows/StyleHelper.cs | 5 +-- .../src/Shared/MS/Internal/PartialList.cs | 3 +- .../Shared/MS/Internal/SizeLimitedCache.cs | 5 +-- .../Automation/HwndProxyElementProvider.cs | 3 +- .../Packaging/CompoundFile/VersionedStream.cs | 3 +- .../CompoundFile/VersionedStreamOwner.cs | 3 +- .../IO/Packaging/CompressEmulationStream.cs | 3 +- .../IO/Packaging/SparseMemoryStream.cs | 7 +--- .../System/Security/RightsManagement/Grant.cs | 5 +-- 35 files changed, 74 insertions(+), 233 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Ink/InkSerializedFormat/GorillaCodec.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Ink/InkSerializedFormat/GorillaCodec.cs index 57f91bb15a6..3a1ef916208 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Ink/InkSerializedFormat/GorillaCodec.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Ink/InkSerializedFormat/GorillaCodec.cs @@ -377,10 +377,7 @@ internal void Compress(int bitCount, int[] input, int startInputIndex, DeltaDelt { throw new ArgumentNullException(StrokeCollectionSerializer.ISFDebugMessage("input or compressed data was null in Compress")); } - if (bitCount < 0) - { - throw new ArgumentOutOfRangeException("bitCount"); - } + ArgumentOutOfRangeException.ThrowIfNegative(bitCount); if (bitCount == 0) { @@ -428,14 +425,8 @@ internal void Compress(int bitCount, BitStreamReader reader, GorillaEncodingType { throw new ArgumentNullException(StrokeCollectionSerializer.ISFDebugMessage("reader or compressedData was null in compress")); } - if (bitCount < 0) - { - throw new ArgumentOutOfRangeException("bitCount"); - } - if (unitsToEncode < 0) - { - throw new ArgumentOutOfRangeException("unitsToEncode"); - } + ArgumentOutOfRangeException.ThrowIfNegative(bitCount); + ArgumentOutOfRangeException.ThrowIfNegative(unitsToEncode); if (bitCount == 0) { @@ -518,20 +509,11 @@ private int GetDataFromReader(BitStreamReader reader, GorillaEncodingType type) internal uint Uncompress(int bitCount, byte[] input, int inputIndex, DeltaDelta dtxf, int[] outputBuffer, int outputBufferIndex) { ArgumentNullException.ThrowIfNull(input); - if (inputIndex >= input.Length) - { - throw new ArgumentOutOfRangeException("inputIndex"); - } + ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(inputIndex, input.Length); ArgumentNullException.ThrowIfNull(outputBuffer); - if (outputBufferIndex >= outputBuffer.Length) - { - throw new ArgumentOutOfRangeException("outputBufferIndex"); - } + ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(outputBufferIndex, outputBuffer.Length); - if (bitCount < 0) - { - throw new ArgumentOutOfRangeException("bitCount"); - } + ArgumentOutOfRangeException.ThrowIfNegative(bitCount); // Adjust bit count if 0 passed in if (bitCount == 0) @@ -603,14 +585,8 @@ internal uint Uncompress(int bitCount, byte[] input, int inputIndex, DeltaDelta internal byte[] Uncompress(int bitCount, BitStreamReader reader, GorillaEncodingType encodingType, int unitsToDecode) { ArgumentNullException.ThrowIfNull(reader); - if (bitCount < 0) - { - throw new ArgumentOutOfRangeException("bitCount"); - } - if (unitsToDecode < 0) - { - throw new ArgumentOutOfRangeException("unitsToDecode"); - } + ArgumentOutOfRangeException.ThrowIfNegative(bitCount); + ArgumentOutOfRangeException.ThrowIfNegative(unitsToDecode); int bitsToWrite = 0; diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Ink/InkSerializedFormat/MultiByteCodec.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Ink/InkSerializedFormat/MultiByteCodec.cs index 325aab57ecb..d58fdb4b890 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Ink/InkSerializedFormat/MultiByteCodec.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Ink/InkSerializedFormat/MultiByteCodec.cs @@ -112,10 +112,7 @@ internal uint Decode(byte[] input, int inputIndex, ref uint data) internal uint SignDecode(byte[] input, int inputIndex, ref int data) { Debug.Assert(input != null); //already validated at the AlgoModule level - if (inputIndex >= input.Length) - { - throw new ArgumentOutOfRangeException("inputIndex"); - } + ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(inputIndex, input.Length); uint xfData = 0; uint cb = Decode(input, inputIndex, ref xfData); data = (0 != (0x01 & xfData)) ? -(int)(xfData >> 1) : (int)(xfData >> 1); diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/PartialArray.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/PartialArray.cs index 411ee136657..ae51e536dbb 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/PartialArray.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/PartialArray.cs @@ -145,10 +145,7 @@ public void CopyTo(T[] array, int arrayIndex) "array"); } - if (arrayIndex < 0) - { - throw new ArgumentOutOfRangeException("arrayIndex"); - } + ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex); if (arrayIndex >= array.Length) { diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Text/TextInterface/Factory.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Text/TextInterface/Factory.cs index d7b077ea9f5..0a3ee63147a 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Text/TextInterface/Factory.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Text/TextInterface/Factory.cs @@ -206,10 +206,7 @@ internal FontFace CreateFontFace(Uri filePathUri, uint faceIndex, FontSimulation &hr )) { - if (faceIndex >= numberOfFaces) - { - throw new ArgumentOutOfRangeException("faceIndex"); - } + ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(faceIndex, numberOfFaces); byte dwriteFontSimulationsFlags = DWriteTypeConverter.Convert(fontSimulationFlags); IDWriteFontFace* dwriteFontFace = null; diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/TextFormatting/ThousandthOfEmRealDoubles.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/TextFormatting/ThousandthOfEmRealDoubles.cs index 746c0a44de7..675decd50de 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/TextFormatting/ThousandthOfEmRealDoubles.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/TextFormatting/ThousandthOfEmRealDoubles.cs @@ -187,10 +187,7 @@ public void CopyTo(double[] array, int arrayIndex) "array"); } - if (arrayIndex < 0) - { - throw new ArgumentOutOfRangeException("arrayIndex"); - } + ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex); if (arrayIndex >= array.Length) { diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/TextFormatting/ThousandthOfEmRealPoints.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/TextFormatting/ThousandthOfEmRealPoints.cs index ea8a781ac1a..6cb60c789cc 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/TextFormatting/ThousandthOfEmRealPoints.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/TextFormatting/ThousandthOfEmRealPoints.cs @@ -130,10 +130,7 @@ public void CopyTo(Point[] array, int arrayIndex) "array"); } - if (arrayIndex < 0) - { - throw new ArgumentOutOfRangeException("arrayIndex"); - } + ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex); if (arrayIndex >= array.Length) { diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/D3DImage.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/D3DImage.cs index dca515e49d5..bb16e5f58f9 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/D3DImage.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/D3DImage.cs @@ -252,10 +252,7 @@ public bool TryLock(Duration timeout) { WritePreamble(); - if (timeout == Duration.Automatic) - { - throw new ArgumentOutOfRangeException("timeout"); - } + ArgumentOutOfRangeException.ThrowIfEqual(timeout, Duration.Automatic); return LockImpl(timeout); } diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetricsDictionary.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetricsDictionary.cs index 689e5d227ee..f78b7c47bf6 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetricsDictionary.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetricsDictionary.cs @@ -133,8 +133,7 @@ public void CopyTo(KeyValuePair[] array, int index) { ArgumentNullException.ThrowIfNull(array); - if (index < 0) - throw new ArgumentOutOfRangeException("index"); + ArgumentOutOfRangeException.ThrowIfNegative(index); if (index >= array.Length) throw new ArgumentException(SR.Format(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, "index", "array")); @@ -191,8 +190,7 @@ void SC.ICollection.CopyTo(Array array, int index) { ArgumentNullException.ThrowIfNull(array); - if (index < 0) - throw new ArgumentOutOfRangeException("index"); + ArgumentOutOfRangeException.ThrowIfNegative(index); if (index >= array.Length) throw new ArgumentException(SR.Format(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, "index", "array")); diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CultureSpecificStringDictionary.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CultureSpecificStringDictionary.cs index 11dc37d684c..0b1786149da 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CultureSpecificStringDictionary.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CultureSpecificStringDictionary.cs @@ -122,8 +122,7 @@ public void CopyTo(KeyValuePair[] array, int index) { ArgumentNullException.ThrowIfNull(array); - if (index < 0) - throw new ArgumentOutOfRangeException("index"); + ArgumentOutOfRangeException.ThrowIfNegative(index); if (index >= array.Length) throw new ArgumentException(SR.Format(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, "index", "array")); @@ -157,8 +156,7 @@ void SC.ICollection.CopyTo(Array array, int index) { ArgumentNullException.ThrowIfNull(array); - if (index < 0) - throw new ArgumentOutOfRangeException("index"); + ArgumentOutOfRangeException.ThrowIfNegative(index); if (index >= array.Length) throw new ArgumentException(SR.Format(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, "index", "array")); diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/VisualCollection.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/VisualCollection.cs index e9aa705caac..231e967b126 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/VisualCollection.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/VisualCollection.cs @@ -794,18 +794,9 @@ public void RemoveRange(int index, int count) VerifyAPIReadWrite(); // Do we really need this extra check index >= _size. - if (index < 0) - { - throw new ArgumentOutOfRangeException(nameof(index)); - } - if (count < 0) - { - throw new ArgumentOutOfRangeException(nameof(count)); - } - if (_size - index < count) - { - throw new ArgumentOutOfRangeException(nameof(index)); - } + ArgumentOutOfRangeException.ThrowIfNegative(index); + ArgumentOutOfRangeException.ThrowIfNegative(count); + ArgumentOutOfRangeException.ThrowIfGreaterThan(count, _size - index); if (count > 0) { diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/CollectionViewProxy.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/CollectionViewProxy.cs index 02df7412516..41bd1c5e4d6 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/CollectionViewProxy.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/CollectionViewProxy.cs @@ -351,8 +351,7 @@ public override bool PassesFilter(object item) public override object GetItemAt(int index) { // only check lower bound because Count could be expensive - if (index < 0) - throw new ArgumentOutOfRangeException("index"); + ArgumentOutOfRangeException.ThrowIfNegative(index); return EnumerableWrapper[index]; } diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/CompositeCollectionView.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/CompositeCollectionView.cs index f67d0f77e19..11199433d12 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/CompositeCollectionView.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/CompositeCollectionView.cs @@ -198,8 +198,7 @@ public override int IndexOf(object item) /// public override object GetItemAt(int index) { - if (index < 0) - throw new ArgumentOutOfRangeException("index"); + ArgumentOutOfRangeException.ThrowIfNegative(index); int positionX, positionY; object item = GetItem(index, out positionX, out positionY); @@ -313,8 +312,7 @@ public override bool MoveCurrentToPrevious() /// public override bool MoveCurrentToPosition(int position) { - if (position < -1) - throw new ArgumentOutOfRangeException("position"); + ArgumentOutOfRangeException.ThrowIfLessThan(position, -1); int newPositionX, newPositionY; object item = GetItem(position, out newPositionX, out newPositionY); @@ -325,10 +323,7 @@ public override bool MoveCurrentToPosition(int position) { item = null; // check upper-bound only after GetItem() to avoid unnecessary pre-counting - if (position > Count) - { - throw new ArgumentOutOfRangeException("position"); - } + ArgumentOutOfRangeException.ThrowIfGreaterThan(position, Count); } if (OKToChangeCurrent()) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/IndexedEnumerable.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/IndexedEnumerable.cs index 95372dc9754..3e9ab066c6f 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/IndexedEnumerable.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/IndexedEnumerable.cs @@ -218,11 +218,7 @@ internal object this[int index] return value; } - if (index < 0) - { -#pragma warning suppress 6503 // "Property get methods should not throw exceptions." - throw new ArgumentOutOfRangeException("index"); // validating the index argument - } + ArgumentOutOfRangeException.ThrowIfNegative(index); int moveBy = (index - _cachedIndex); if (moveBy < 0) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/RBTree.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/RBTree.cs index fd23bf20e8c..3a53010888a 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/RBTree.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/RBTree.cs @@ -575,8 +575,7 @@ public bool Contains(T item) public void CopyTo(T[] array, int arrayIndex) { ArgumentNullException.ThrowIfNull(array); - if (arrayIndex < 0) - throw new ArgumentOutOfRangeException("arrayIndex"); + ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex); if (arrayIndex + Count > array.Length) throw new ArgumentException(SR.Argument_InvalidOffLen); diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/WeakDictionary.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/WeakDictionary.cs index 0cd49c5cb9e..9a251505211 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/WeakDictionary.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/WeakDictionary.cs @@ -292,10 +292,7 @@ public bool Contains(KeyValuePair item) public void CopyTo(KeyValuePair[] array, int arrayIndex) { - if (arrayIndex < 0) - { - throw new ArgumentOutOfRangeException("arrayIndex"); - } + ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex); ArgumentNullException.ThrowIfNull(array); int count = 0; diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/WeakHashSet.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/WeakHashSet.cs index 7ebec7beb8b..156eb2730bf 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/WeakHashSet.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/WeakHashSet.cs @@ -39,10 +39,7 @@ public bool Contains(T item) public void CopyTo(T[] array, int arrayIndex) { - if (arrayIndex < 0) - { - throw new ArgumentOutOfRangeException("arrayIndex"); - } + ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex); ArgumentNullException.ThrowIfNull(array); int count = 0; diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/WindowsRuntime/Generated/WinRT/Projections/IReadOnlyList.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/WindowsRuntime/Generated/WinRT/Projections/IReadOnlyList.cs index 47217ee49e7..a892ac4b07d 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/WindowsRuntime/Generated/WinRT/Projections/IReadOnlyList.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/WindowsRuntime/Generated/WinRT/Projections/IReadOnlyList.cs @@ -88,8 +88,7 @@ public int Count private T Indexer_Get(int index) { - if (index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); + ArgumentOutOfRangeException.ThrowIfNegative(index); try { diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/DocumentGrid.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/DocumentGrid.cs index af39121806d..2c93c097600 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/DocumentGrid.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/DocumentGrid.cs @@ -428,10 +428,7 @@ public void SetScale(double scale) { if (!DoubleUtil.AreClose(scale, Scale)) { - if (scale <= 0.0) - { - throw new ArgumentOutOfRangeException("scale"); - } + ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(scale, 0.0); if (!Helper.IsDoubleValid(scale)) { @@ -448,10 +445,7 @@ public void SetScale(double scale) /// public void SetColumns(int columns) { - if (columns < 1) - { - throw new ArgumentOutOfRangeException("columns"); - } + ArgumentOutOfRangeException.ThrowIfLessThan(columns, 1); //Perf Tracing - Mark Layout Change Start EventTrace.EasyTraceEvent(EventTrace.Keyword.KeywordXPS, EventTrace.Event.WClientDRXLayoutBegin); @@ -465,10 +459,7 @@ public void SetColumns(int columns) /// public void FitColumns(int columns) { - if (columns < 1) - { - throw new ArgumentOutOfRangeException("columns"); - } + ArgumentOutOfRangeException.ThrowIfLessThan(columns, 1); //Perf Tracing - Mark Layout Change Start EventTrace.EasyTraceEvent(EventTrace.Keyword.KeywordXPS, EventTrace.Event.WClientDRXLayoutBegin); diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/PageCache.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/PageCache.cs index ebcacad50b8..76cf5060efe 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/PageCache.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/PageCache.cs @@ -696,15 +696,8 @@ private object GetPageCompletedDelegate(object parameter) /// private void ValidatePaginationArgs(int start, int count) { - if (start < 0) - { - throw new ArgumentOutOfRangeException("start"); - } - - if (count <= 0) - { - throw new ArgumentOutOfRangeException("count"); - } + ArgumentOutOfRangeException.ThrowIfNegative(start); + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(count); } /// @@ -762,15 +755,8 @@ private void FirePageCacheChangedEvent(List changes) private PageCacheChange AddRange(int start, int count) { //Make sure we're in range. - if (start < 0) - { - throw new ArgumentOutOfRangeException("start"); - } - - if (count < 1) - { - throw new ArgumentOutOfRangeException("count"); - } + ArgumentOutOfRangeException.ThrowIfNegative(start); + ArgumentOutOfRangeException.ThrowIfLessThan(count, 1); Invariant.Assert(_defaultPageSize != Size.Empty, "Default Page Size is Empty."); @@ -833,10 +819,7 @@ private PageCacheChange UpdateEntry(int index, PageCacheEntry newEntry) private PageCacheChange DirtyRange(int start, int count) { //Make sure we're in range. - if (start >= _cache.Count) - { - throw new ArgumentOutOfRangeException("start"); - } + ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(start, _cache.Count); if (start + count > _cache.Count || count < 1) { diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/RowCache.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/RowCache.cs index 7637422085c..b580ff45bf2 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/RowCache.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/RowCache.cs @@ -399,10 +399,7 @@ public void GetVisibleRowIndices(double startOffset, double endOffset, out int s startRowIndex = 0; rowCount = 0; - if (endOffset < startOffset) - { - throw new ArgumentOutOfRangeException("endOffset"); - } + ArgumentOutOfRangeException.ThrowIfLessThan(endOffset, startOffset); //If the offsets we're given are out of range we'll just return now //because we have no rows to find at this offset. @@ -520,10 +517,7 @@ public void RecalcRows(int pivotPage, int columns) } //Can't lay out fewer than 1 column of pages. - if (columns < 1) - { - throw new ArgumentOutOfRangeException("columns"); - } + ArgumentOutOfRangeException.ThrowIfLessThan(columns, 1); //Store off the requested layout parameters. _layoutColumns = columns; @@ -663,10 +657,7 @@ private int RecalcRowsForDynamicPageSizes(int pivotPage, int columns) } //Can't lay out fewer than 1 column of pages. - if (columns < 1) - { - throw new ArgumentOutOfRangeException("columns"); - } + ArgumentOutOfRangeException.ThrowIfLessThan(columns, 1); //Adjust the pivot page as necessary so that the to-be-computed layout has the specified number //of columns on the row. @@ -744,10 +735,7 @@ private int RecalcRowsForDynamicPageSizes(int pivotPage, int columns) /// private RowInfo CreateDynamicRow(int startPage, double rowWidth, bool createForward) { - if (startPage >= PageCache.PageCount) - { - throw new ArgumentOutOfRangeException("startPage"); - } + ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(startPage, PageCache.PageCount); //Given a starting page for this row, and the specified //width for each row, figure out how many pages will fit on this row @@ -826,10 +814,7 @@ private int RecalcRowsForFixedPageSizes(int startPage, int columns) } //Can't lay out fewer than 1 column of pages. - if (columns < 1) - { - throw new ArgumentOutOfRangeException("columns"); - } + ArgumentOutOfRangeException.ThrowIfLessThan(columns, 1); //Since all pages in the document have been determined to be //the same size, we can use a simple algorithm to create our row layout. @@ -857,15 +842,9 @@ private int RecalcRowsForFixedPageSizes(int startPage, int columns) /// private RowInfo CreateFixedRow(int startPage, int columns) { - if (startPage >= PageCache.PageCount) - { - throw new ArgumentOutOfRangeException("startPage"); - } + ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(startPage, PageCache.PageCount); - if (columns < 1) - { - throw new ArgumentOutOfRangeException("columns"); - } + ArgumentOutOfRangeException.ThrowIfLessThan(columns, 1); //Given a starting page for this row and the number of columns in the row //calculate the width & height and return the resulting RowInfo struct diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/TextBoxView.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/TextBoxView.cs index 735ac71b47a..79fa8a27a53 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/TextBoxView.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/TextBoxView.cs @@ -551,10 +551,7 @@ protected override void OnRender(DrawingContext context) /// protected override Visual GetVisualChild(int index) { - if (index >= this.VisualChildrenCount) - { - throw new ArgumentOutOfRangeException("index"); - } + ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, this.VisualChildrenCount); return _visualChildren[index]; } diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/GridViewColumnHeaderAutomationPeer.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/GridViewColumnHeaderAutomationPeer.cs index 53b5b205515..8db7591a253 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/GridViewColumnHeaderAutomationPeer.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/GridViewColumnHeaderAutomationPeer.cs @@ -90,14 +90,8 @@ void ITransformProvider.Resize(double width, double height) if (!IsEnabled()) throw new ElementNotEnabledException(); - if (width < 0) - { - throw new ArgumentOutOfRangeException("width"); - } - if (height < 0) - { - throw new ArgumentOutOfRangeException("height"); - } + ArgumentOutOfRangeException.ThrowIfNegative(width); + ArgumentOutOfRangeException.ThrowIfNegative(height); GridViewColumnHeader header = Owner as GridViewColumnHeader; if (header != null) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemCollection.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemCollection.cs index 1b9cc5ef4ad..255bb73d927 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemCollection.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemCollection.cs @@ -297,8 +297,7 @@ public void CopyTo(Array array, int index) ArgumentNullException.ThrowIfNull(array); if (array.Rank > 1) throw new ArgumentException(SR.BadTargetArray, "array"); // array is multidimensional. - if (index < 0) - throw new ArgumentOutOfRangeException("index"); + ArgumentOutOfRangeException.ThrowIfNegative(index); // use the view instead of the collection, because it may have special sort/filter if (!EnsureCollectionView()) @@ -340,23 +339,21 @@ public override int IndexOf(object item) /// public override object GetItemAt(int index) { - // only check lower bound because Count could be expensive - if (index < 0) - throw new ArgumentOutOfRangeException("index"); + // only check lower bound because Count could be expensive + ArgumentOutOfRangeException.ThrowIfNegative(index); - VerifyRefreshNotDeferred(); + VerifyRefreshNotDeferred(); - if (!EnsureCollectionView()) - throw new InvalidOperationException(SR.ItemCollectionHasNoCollection); + if (!EnsureCollectionView()) + throw new InvalidOperationException(SR.ItemCollectionHasNoCollection); - if (_collectionView == _internalView) - { - // check upper bound here because we know it's not expensive - if (index >= _internalView.Count) - throw new ArgumentOutOfRangeException("index"); - } + if (_collectionView == _internalView) + { + // check upper bound here because we know it's not expensive + ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, _internalView.Count); + } - return _collectionView.GetItemAt(index); + return _collectionView.GetItemAt(index); } diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/CollectionView.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/CollectionView.cs index 547bf0a446d..229a0ab05c7 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/CollectionView.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/CollectionView.cs @@ -662,8 +662,7 @@ public virtual int IndexOf(object item) public virtual object GetItemAt(int index) { // only check lower bound because Count could be expensive - if (index < 0) - throw new ArgumentOutOfRangeException("index"); + ArgumentOutOfRangeException.ThrowIfNegative(index); return EnumerableWrapper[index]; } diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextElementCollection.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextElementCollection.cs index d5e8fa5187a..18fdad77226 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextElementCollection.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextElementCollection.cs @@ -605,10 +605,7 @@ void ICollection.CopyTo(Array array, int arrayIndex) throw new ArgumentException("array"); } - if (arrayIndex < 0) - { - throw new ArgumentOutOfRangeException("arrayIndex"); - } + ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex); if (arrayIndex > array.Length) { diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Baml2006/SharedStream.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Baml2006/SharedStream.cs index c8c2be9b480..949a7dabb1a 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Baml2006/SharedStream.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Baml2006/SharedStream.cs @@ -49,15 +49,9 @@ private void Initialize(Stream baseStream, long offset, long length) throw new ArgumentException("can\u2019t seek on baseStream"); } - if (offset < 0) - { - throw new ArgumentOutOfRangeException(nameof(offset)); - } + ArgumentOutOfRangeException.ThrowIfNegative(offset); - if (length < 0) - { - throw new ArgumentOutOfRangeException(nameof(length)); - } + ArgumentOutOfRangeException.ThrowIfNegative(length); SharedStream subStream = baseStream as SharedStream; if (subStream != null) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StyleHelper.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StyleHelper.cs index 2051328996e..520adfe9259 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StyleHelper.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StyleHelper.cs @@ -5443,10 +5443,7 @@ internal static DependencyObject GetChild(List styledChildren, return null; } - if (childIndex < 0) - { - throw new ArgumentOutOfRangeException("childIndex"); - } + ArgumentOutOfRangeException.ThrowIfNegative(childIndex); DependencyObject child = styledChildren[childIndex - 1]; diff --git a/src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/PartialList.cs b/src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/PartialList.cs index 7cd16ef8247..fb6d9479938 100644 --- a/src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/PartialList.cs +++ b/src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/PartialList.cs @@ -151,8 +151,7 @@ public int Count public void CopyTo(T[] array, int arrayIndex) { - if (arrayIndex < 0) - throw new ArgumentOutOfRangeException("arrayIndex"); + ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex); for (int i = 0; i < _count; ++i) array[arrayIndex + i] = this[i]; diff --git a/src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/SizeLimitedCache.cs b/src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/SizeLimitedCache.cs index d4f77b1a195..b982c2c623b 100644 --- a/src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/SizeLimitedCache.cs +++ b/src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/SizeLimitedCache.cs @@ -40,10 +40,7 @@ internal class SizeLimitedCache /// public SizeLimitedCache(int maximumItems) { - if (maximumItems <= 0) - { - throw new ArgumentOutOfRangeException("maximumItems"); - } + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(maximumItems); _maximumItems = maximumItems; _permanentCount = 0; diff --git a/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/MS/Internal/Automation/HwndProxyElementProvider.cs b/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/MS/Internal/Automation/HwndProxyElementProvider.cs index 5761b99f882..8f6d8363758 100644 --- a/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/MS/Internal/Automation/HwndProxyElementProvider.cs +++ b/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/MS/Internal/Automation/HwndProxyElementProvider.cs @@ -402,8 +402,7 @@ void IWindowProvider.Close() bool IWindowProvider.WaitForInputIdle( int milliseconds ) { - if( milliseconds < 0 ) - throw new ArgumentOutOfRangeException( "milliseconds" ); + ArgumentOutOfRangeException.ThrowIfNegative(milliseconds); // Implementation note: This method is usually used in response to handling a WindowPattern.WindowOpenedEvent. // In this case it works for both legacy and WCP windows. This is because the WindowOpenedEvent uses a private diff --git a/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/VersionedStream.cs b/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/VersionedStream.cs index 67ef630cf9c..f8d46ba1125 100644 --- a/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/VersionedStream.cs +++ b/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/VersionedStream.cs @@ -99,8 +99,7 @@ public override void SetLength(long newLength) { CheckDisposed(); - if (newLength < 0) - throw new ArgumentOutOfRangeException("newLength"); + ArgumentOutOfRangeException.ThrowIfNegative(newLength); _versionOwner.WriteAttempt(); _stream.SetLength(newLength); diff --git a/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/VersionedStreamOwner.cs b/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/VersionedStreamOwner.cs index 49691f612bc..637b2cdf1f0 100644 --- a/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/VersionedStreamOwner.cs +++ b/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/VersionedStreamOwner.cs @@ -105,8 +105,7 @@ public override long Seek(long offset, SeekOrigin origin) /// public override void SetLength(long newLength) { - if (newLength < 0) - throw new ArgumentOutOfRangeException("newLength"); + ArgumentOutOfRangeException.ThrowIfNegative(newLength); WriteAttempt(); checked { BaseStream.SetLength(newLength + _dataOffset); } diff --git a/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompressEmulationStream.cs b/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompressEmulationStream.cs index 9a1d89708ea..ea8fb437042 100644 --- a/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompressEmulationStream.cs +++ b/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompressEmulationStream.cs @@ -259,8 +259,7 @@ public override bool CanWrite /// another wrapper Stream class. internal CompressEmulationStream(Stream baseStream, Stream tempStream, long position, IDeflateTransform transformer) { - if (position < 0) - throw new ArgumentOutOfRangeException("position"); + ArgumentOutOfRangeException.ThrowIfNegative(position); ArgumentNullException.ThrowIfNull(baseStream); diff --git a/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/SparseMemoryStream.cs b/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/SparseMemoryStream.cs index a8ab3de194c..3cb9587e835 100644 --- a/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/SparseMemoryStream.cs +++ b/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/SparseMemoryStream.cs @@ -82,13 +82,10 @@ public override void SetLength(long newLength) { CheckDisposed(); - if (newLength < 0) - { - throw new ArgumentOutOfRangeException("newLength"); - } + ArgumentOutOfRangeException.ThrowIfNegative(newLength); #if DEBUG - DebugAssertConsistentArrayStructure(); + DebugAssertConsistentArrayStructure(); #endif if (_currentStreamLength != newLength) diff --git a/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Security/RightsManagement/Grant.cs b/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Security/RightsManagement/Grant.cs index e74eea61c25..c00cc0de396 100644 --- a/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Security/RightsManagement/Grant.cs +++ b/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Security/RightsManagement/Grant.cs @@ -61,10 +61,7 @@ public ContentGrant(ContentUser user, ContentRight right, DateTime validFrom, Da throw new ArgumentOutOfRangeException("right"); } - if (validFrom > validUntil) - { - throw new ArgumentOutOfRangeException("validFrom"); - } + ArgumentOutOfRangeException.ThrowIfGreaterThan(validFrom, validUntil); _user = user; _right = right; From 4b39503926feecdbe2be74655609ca1ab97a868e Mon Sep 17 00:00:00 2001 From: halgab <24685886+halgab@users.noreply.github.com> Date: Wed, 8 Nov 2023 11:14:56 +0100 Subject: [PATCH 2/2] rollback erroneous change a bug in the fixer? --- .../src/PresentationCore/System/Windows/InterOp/D3DImage.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/D3DImage.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/D3DImage.cs index bb16e5f58f9..dca515e49d5 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/D3DImage.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/D3DImage.cs @@ -252,7 +252,10 @@ public bool TryLock(Duration timeout) { WritePreamble(); - ArgumentOutOfRangeException.ThrowIfEqual(timeout, Duration.Automatic); + if (timeout == Duration.Automatic) + { + throw new ArgumentOutOfRangeException("timeout"); + } return LockImpl(timeout); }