From 43c98fcead6566bb53abca53a36c29225d0a58fc Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Sun, 20 Dec 2020 22:36:51 +0000 Subject: [PATCH 1/3] Enable SA1314: Type parameter names should begin with T https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1314.md --- .globalconfig | 2 +- .../commands/utility/ImplicitRemotingCommands.cs | 14 +++++++++----- .../commands/utility/Measure-Object.cs | 14 +++++++------- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/.globalconfig b/.globalconfig index f0cc41c388e..54f5bac867c 100644 --- a/.globalconfig +++ b/.globalconfig @@ -1307,7 +1307,7 @@ dotnet_diagnostic.SA1312.severity = none dotnet_diagnostic.SA1313.severity = none # SA1314: Type parameter names should begin with T -dotnet_diagnostic.SA1314.severity = none +dotnet_diagnostic.SA1314.severity = warning # SA1316: Tuple element names should use correct casing dotnet_diagnostic.SA1316.severity = none diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImplicitRemotingCommands.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImplicitRemotingCommands.cs index 03ca0953e94..67d25128558 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImplicitRemotingCommands.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImplicitRemotingCommands.cs @@ -1066,17 +1066,21 @@ private List RehydrateList(string commandName, object deserializedList, Fu return result; } - private Dictionary RehydrateDictionary(string commandName, PSObject deserializedObject, string propertyName, Func valueRehydrator) + private Dictionary RehydrateDictionary( + string commandName, + PSObject deserializedObject, + string propertyName, + Func valueRehydrator) { Dbg.Assert(deserializedObject != null, "deserializedObject parameter != null"); Dbg.Assert(!string.IsNullOrEmpty(propertyName), "propertyName parameter != null"); if (valueRehydrator == null) { - valueRehydrator = (PSObject pso) => ConvertTo(commandName, pso); + valueRehydrator = (PSObject pso) => ConvertTo(commandName, pso); } - Dictionary result = new(); + Dictionary result = new(); PSPropertyInfo deserializedDictionaryProperty = deserializedObject.Properties[propertyName]; if (deserializedDictionaryProperty != null) { @@ -1085,10 +1089,10 @@ private Dictionary RehydrateDictionary(string commandName, PSObject { foreach (DictionaryEntry deserializedItem in deserializedDictionary) { - K itemKey = ConvertTo(commandName, deserializedItem.Key); + TKey itemKey = ConvertTo(commandName, deserializedItem.Key); PSObject deserializedItemValue = ConvertTo(commandName, deserializedItem.Value); - V itemValue = valueRehydrator(deserializedItemValue); + TValue itemValue = valueRehydrator(deserializedItemValue); result.Add(itemKey, itemValue); } diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs index 4bc6a3b42e0..452e8bfe09c 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs @@ -159,12 +159,12 @@ public sealed class MeasureObjectCommand : PSCmdlet /// Dictionary to be used by Measure-Object implementation. /// Keys are strings. Keys are compared with OrdinalIgnoreCase. /// - /// Value type. - private class MeasureObjectDictionary : Dictionary - where V : new() + /// Value type. + private class MeasureObjectDictionary : Dictionary + where TValue : new() { /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// Default ctor. /// internal MeasureObjectDictionary() : base(StringComparer.OrdinalIgnoreCase) @@ -181,12 +181,12 @@ internal MeasureObjectDictionary() : base(StringComparer.OrdinalIgnoreCase) /// /// The existing value, or a newly-created value. /// - public V EnsureEntry(string key) + public TValue EnsureEntry(string key) { - V val; + TValue val; if (!TryGetValue(key, out val)) { - val = new V(); + val = new TValue(); this[key] = val; } From 39f788f01b92ac68b51bf6688542159a4a35294b Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Mon, 21 Dec 2020 00:20:53 +0000 Subject: [PATCH 2/3] Rename type arguments --- .../engine/MshObject.cs | 15 ++-- .../engine/hostifaces/PSDataCollection.cs | 22 +++--- .../engine/interpreter/Utilities.cs | 10 +-- .../common/WireDataFormat/EncodeAndDecode.cs | 8 +-- .../engine/serialization.cs | 12 ++-- .../utils/ObjectReader.cs | 70 +++++++++---------- 6 files changed, 69 insertions(+), 68 deletions(-) diff --git a/src/System.Management.Automation/engine/MshObject.cs b/src/System.Management.Automation/engine/MshObject.cs index 2253c7c913e..7ea7ef1bd72 100644 --- a/src/System.Management.Automation/engine/MshObject.cs +++ b/src/System.Management.Automation/engine/MshObject.cs @@ -164,20 +164,21 @@ private static T AdapterGetFirstMemberOrDefaultDelegate(PSObject msjObj, Memb return retValue; } - internal static PSMemberInfoInternalCollection TransformMemberInfoCollection(PSMemberInfoCollection source) where T : PSMemberInfo where U : PSMemberInfo + internal static PSMemberInfoInternalCollection TransformMemberInfoCollection(PSMemberInfoCollection source) + where TSource : PSMemberInfo where TResult : PSMemberInfo { - if (typeof(T) == typeof(U)) + if (typeof(TSource) == typeof(TResult)) { // If the types are the same, don't make a copy, return the cached collection. - return source as PSMemberInfoInternalCollection; + return source as PSMemberInfoInternalCollection; } - PSMemberInfoInternalCollection returnValue = new PSMemberInfoInternalCollection(); - foreach (T member in source) + PSMemberInfoInternalCollection returnValue = new PSMemberInfoInternalCollection(); + foreach (TSource member in source) { - if (member is U tAsU) + if (member is TResult result) { - returnValue.Add(tAsU); + returnValue.Add(result); } } diff --git a/src/System.Management.Automation/engine/hostifaces/PSDataCollection.cs b/src/System.Management.Automation/engine/hostifaces/PSDataCollection.cs index 1743e6d4678..f484fbe865e 100644 --- a/src/System.Management.Automation/engine/hostifaces/PSDataCollection.cs +++ b/src/System.Management.Automation/engine/hostifaces/PSDataCollection.cs @@ -1809,8 +1809,8 @@ protected void Dispose(bool disposing) /// Needed to provide a way to get to the non-blocking /// MoveNext implementation. /// - /// - internal interface IBlockingEnumerator : IEnumerator + /// + internal interface IBlockingEnumerator : IEnumerator { bool MoveNext(bool block); } @@ -1822,14 +1822,14 @@ internal interface IBlockingEnumerator : IEnumerator /// either all the PowerShell operations are completed or the /// PSDataCollection is closed. /// - /// - internal sealed class PSDataCollectionEnumerator : IBlockingEnumerator + /// + internal sealed class PSDataCollectionEnumerator : IBlockingEnumerator { #region Private Data - private W _currentElement; + private T _currentElement; private int _index; - private readonly PSDataCollection _collToEnumerate; + private readonly PSDataCollection _collToEnumerate; private readonly bool _neverBlock; #endregion @@ -1845,7 +1845,7 @@ internal sealed class PSDataCollectionEnumerator : IBlockingEnumerator /// /// Controls if the enumerator is blocking by default or not. /// - internal PSDataCollectionEnumerator(PSDataCollection collection, bool neverBlock) + internal PSDataCollectionEnumerator(PSDataCollection collection, bool neverBlock) { Dbg.Assert(collection != null, "Collection cannot be null"); @@ -1854,7 +1854,7 @@ internal PSDataCollectionEnumerator(PSDataCollection collection, bool neverBl _collToEnumerate = collection; _index = 0; - _currentElement = default(W); + _currentElement = default(T); _collToEnumerate.IsEnumerated = true; _neverBlock = neverBlock; } @@ -1872,7 +1872,7 @@ internal PSDataCollectionEnumerator(PSDataCollection collection, bool neverBl /// if the enumerator is positioned before the first element or after /// the last element; the value of the property is undefined. /// - W IEnumerator.Current + T IEnumerator.Current { get { @@ -1933,7 +1933,7 @@ public bool MoveNext(bool block) _currentElement = _collToEnumerate[_index]; if (_collToEnumerate.ReleaseOnEnumeration) { - _collToEnumerate[_index] = default(W); + _collToEnumerate[_index] = default(T); } _index++; @@ -1975,7 +1975,7 @@ public bool MoveNext(bool block) /// public void Reset() { - _currentElement = default(W); + _currentElement = default(T); _index = 0; } diff --git a/src/System.Management.Automation/engine/interpreter/Utilities.cs b/src/System.Management.Automation/engine/interpreter/Utilities.cs index 3d4307f1d3a..ac896719d62 100644 --- a/src/System.Management.Automation/engine/interpreter/Utilities.cs +++ b/src/System.Management.Automation/engine/interpreter/Utilities.cs @@ -1084,14 +1084,14 @@ internal static bool TrueForAll(this IEnumerable collection, Predicate return true; } - internal static U[] Map(this ICollection collection, Func select) + internal static TResult[] Map(this ICollection source, Func selector) { - int count = collection.Count; - U[] result = new U[count]; + int count = source.Count; + TResult[] result = new TResult[count]; count = 0; - foreach (T t in collection) + foreach (TSource t in source) { - result[count++] = select(t); + result[count++] = selector(t); } return result; diff --git a/src/System.Management.Automation/engine/remoting/common/WireDataFormat/EncodeAndDecode.cs b/src/System.Management.Automation/engine/remoting/common/WireDataFormat/EncodeAndDecode.cs index 03a3cd51a87..d99fbe21126 100644 --- a/src/System.Management.Automation/engine/remoting/common/WireDataFormat/EncodeAndDecode.cs +++ b/src/System.Management.Automation/engine/remoting/common/WireDataFormat/EncodeAndDecode.cs @@ -1803,7 +1803,7 @@ internal static IEnumerable EnumerateListProperty(PSObject psObject, strin } } - internal static IEnumerable> EnumerateHashtableProperty(PSObject psObject, string propertyName) + internal static IEnumerable> EnumerateHashtableProperty(PSObject psObject, string propertyName) { if (psObject == null) { @@ -1820,9 +1820,9 @@ internal static IEnumerable> EnumerateHashtable { foreach (DictionaryEntry e in h) { - KeyType key = ConvertPropertyValueTo(propertyName, e.Key); - ValueType value = ConvertPropertyValueTo(propertyName, e.Value); - yield return new KeyValuePair(key, value); + TKey key = ConvertPropertyValueTo(propertyName, e.Key); + TValue value = ConvertPropertyValueTo(propertyName, e.Value); + yield return new KeyValuePair(key, value); } } } diff --git a/src/System.Management.Automation/engine/serialization.cs b/src/System.Management.Automation/engine/serialization.cs index a01f67b81e2..b4115de05cb 100644 --- a/src/System.Management.Automation/engine/serialization.cs +++ b/src/System.Management.Automation/engine/serialization.cs @@ -6871,27 +6871,27 @@ internal static T GetPropertyValue(PSObject pso, string propertyName, Rehydra } } - private static ListType RehydrateList(PSObject pso, string propertyName, RehydrationFlags flags) - where ListType : IList, new() + private static TList RehydrateList(PSObject pso, string propertyName, RehydrationFlags flags) + where TList : IList, new() { ArrayList deserializedList = GetPropertyValue(pso, propertyName, flags); if (deserializedList == null) { if ((flags & RehydrationFlags.NullValueMeansEmptyList) == RehydrationFlags.NullValueMeansEmptyList) { - return new ListType(); + return new TList(); } else { - return default(ListType); + return default(TList); } } else { - ListType newList = new ListType(); + TList newList = new TList(); foreach (object deserializedItem in deserializedList) { - ItemType item = (ItemType)LanguagePrimitives.ConvertTo(deserializedItem, typeof(ItemType), CultureInfo.InvariantCulture); + TItem item = (TItem)LanguagePrimitives.ConvertTo(deserializedItem, typeof(TItem), CultureInfo.InvariantCulture); newList.Add(item); } diff --git a/src/System.Management.Automation/utils/ObjectReader.cs b/src/System.Management.Automation/utils/ObjectReader.cs index f06243ddd5b..b333ec25be2 100644 --- a/src/System.Management.Automation/utils/ObjectReader.cs +++ b/src/System.Management.Automation/utils/ObjectReader.cs @@ -483,12 +483,12 @@ private static Collection MakePSObjectCollection( /// commands concurrently. /// Only Read() operation is supported currently. /// - internal class PSDataCollectionReader - : ObjectReaderBase + internal class PSDataCollectionReader + : ObjectReaderBase { #region Private Data - private readonly PSDataCollectionEnumerator _enumerator; + private readonly PSDataCollectionEnumerator _enumerator; #endregion @@ -498,12 +498,12 @@ internal class PSDataCollectionReader /// /// The stream to read. /// Thrown if the specified stream is null. - public PSDataCollectionReader(PSDataCollectionStream stream) + public PSDataCollectionReader(PSDataCollectionStream stream) : base(stream) { System.Management.Automation.Diagnostics.Assert(stream.ObjectStore != null, "Stream should have a valid data store"); - _enumerator = (PSDataCollectionEnumerator)stream.ObjectStore.GetEnumerator(); + _enumerator = (PSDataCollectionEnumerator)stream.ObjectStore.GetEnumerator(); } #endregion ctor @@ -513,7 +513,7 @@ public PSDataCollectionReader(PSDataCollectionStream stream) /// /// The maximum number of objects to read. /// The objects read. - public override Collection Read(int count) + public override Collection Read(int count) { throw new NotSupportedException(); } @@ -528,7 +528,7 @@ public override Collection Read(int count) /// /// This method blocks if the buffer is empty. /// - public override ReturnType Read() + public override TResult Read() { object result = AutomationNull.Value; if (_enumerator.MoveNext()) @@ -544,7 +544,7 @@ public override ReturnType Read() /// /// /// - public override Collection ReadToEnd() + public override Collection ReadToEnd() { throw new NotSupportedException(); } @@ -554,7 +554,7 @@ public override Collection ReadToEnd() /// /// /// - public override Collection NonBlockingRead() + public override Collection NonBlockingRead() { return NonBlockingRead(Int32.MaxValue); } @@ -567,7 +567,7 @@ public override Collection NonBlockingRead() /// /// Return no more than maxRequested objects. /// - public override Collection NonBlockingRead(int maxRequested) + public override Collection NonBlockingRead(int maxRequested) { if (maxRequested < 0) { @@ -576,31 +576,31 @@ public override Collection NonBlockingRead(int maxRequested) if (maxRequested == 0) { - return new Collection(); + return new Collection(); } - Collection results = new Collection(); + Collection result = new Collection(); int readCount = maxRequested; while (readCount > 0) { if (_enumerator.MoveNext(false)) { - results.Add(ConvertToReturnType(_enumerator.Current)); + result.Add(ConvertToReturnType(_enumerator.Current)); continue; } break; } - return results; + return result; } /// /// This method is not supported. /// /// - public override ReturnType Peek() + public override TResult Peek() { throw new NotSupportedException(); } @@ -617,12 +617,12 @@ protected override void Dispose(bool disposing) } } - private static ReturnType ConvertToReturnType(object inputObject) + private static TResult ConvertToReturnType(object inputObject) { - Type resultType = typeof(ReturnType); + Type resultType = typeof(TResult); if (typeof(PSObject) == resultType || typeof(object) == resultType) { - ReturnType result; + TResult result; LanguagePrimitives.TryConvertTo(inputObject, out result); return result; } @@ -642,12 +642,12 @@ private static ReturnType ConvertToReturnType(object inputObject) /// commands concurrently. /// Only Read() operation is supported currently. /// - internal class PSDataCollectionPipelineReader - : ObjectReaderBase + internal class PSDataCollectionPipelineReader + : ObjectReaderBase { #region Private Data - private readonly PSDataCollection _datastore; + private readonly PSDataCollection _datastore; #endregion Private Data @@ -658,7 +658,7 @@ internal class PSDataCollectionPipelineReader /// The stream to read. /// /// - internal PSDataCollectionPipelineReader(PSDataCollectionStream stream, + internal PSDataCollectionPipelineReader(PSDataCollectionStream stream, string computerName, Guid runspaceId) : base(stream) { @@ -688,7 +688,7 @@ internal PSDataCollectionPipelineReader(PSDataCollectionStream st /// /// The maximum number of objects to read. /// The objects read. - public override Collection Read(int count) + public override Collection Read(int count) { throw new NotSupportedException(); } @@ -703,14 +703,14 @@ public override Collection Read(int count) /// /// This method blocks if the buffer is empty. /// - public override ReturnType Read() + public override TReturn Read() { object result = AutomationNull.Value; if (_datastore.Count > 0) { - Collection resultCollection = _datastore.ReadAndRemove(1); + Collection resultCollection = _datastore.ReadAndRemove(1); - // ReadAndRemove returns a Collection type but we + // ReadAndRemove returns a Collection type but we // just want the single object contained in the collection. if (resultCollection.Count == 1) { @@ -726,7 +726,7 @@ public override ReturnType Read() /// /// /// - public override Collection ReadToEnd() + public override Collection ReadToEnd() { throw new NotSupportedException(); } @@ -736,7 +736,7 @@ public override Collection ReadToEnd() /// /// /// - public override Collection NonBlockingRead() + public override Collection NonBlockingRead() { return NonBlockingRead(Int32.MaxValue); } @@ -749,7 +749,7 @@ public override Collection NonBlockingRead() /// /// Return no more than maxRequested objects. /// - public override Collection NonBlockingRead(int maxRequested) + public override Collection NonBlockingRead(int maxRequested) { if (maxRequested < 0) { @@ -758,10 +758,10 @@ public override Collection NonBlockingRead(int maxRequested) if (maxRequested == 0) { - return new Collection(); + return new Collection(); } - Collection results = new Collection(); + Collection results = new Collection(); int readCount = maxRequested; while (readCount > 0) @@ -783,7 +783,7 @@ public override Collection NonBlockingRead(int maxRequested) /// This method is not supported. /// /// - public override ReturnType Peek() + public override TReturn Peek() { throw new NotSupportedException(); } @@ -793,12 +793,12 @@ public override ReturnType Peek() /// /// Input object to convert. /// Input object converted to the specified return type. - private static ReturnType ConvertToReturnType(object inputObject) + private static TReturn ConvertToReturnType(object inputObject) { - Type resultType = typeof(ReturnType); + Type resultType = typeof(TReturn); if (typeof(PSObject) == resultType || typeof(object) == resultType) { - ReturnType result; + TReturn result; LanguagePrimitives.TryConvertTo(inputObject, out result); return result; } From c3804e9777cf0456216f770d8a795b6290cef3b6 Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Mon, 21 Dec 2020 00:26:51 +0000 Subject: [PATCH 3/3] Remove unnecessary type argument The type argument for the `struct Enumerator` is unnecessary, we can use the argument from the class declaration. --- .../engine/MshMemberInfo.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/System.Management.Automation/engine/MshMemberInfo.cs b/src/System.Management.Automation/engine/MshMemberInfo.cs index 9bbec9c4312..34a3a1f9ee6 100644 --- a/src/System.Management.Automation/engine/MshMemberInfo.cs +++ b/src/System.Management.Automation/engine/MshMemberInfo.cs @@ -4974,7 +4974,7 @@ internal override ReadOnlyPSMemberInfoCollection Match(string name, PSMemberT /// The enumerator for this collection. public override IEnumerator GetEnumerator() { - return new Enumerator(this); + return new Enumerator(this); } internal override T FirstOrDefault(MemberNamePredicate predicate) @@ -5028,17 +5028,17 @@ internal override T FirstOrDefault(MemberNamePredicate predicate) /// /// Enumerable for this class. /// - internal struct Enumerator : IEnumerator where S : PSMemberInfo + internal struct Enumerator : IEnumerator { - private S _current; + private T _current; private int _currentIndex; - private readonly PSMemberInfoInternalCollection _allMembers; + private readonly PSMemberInfoInternalCollection _allMembers; /// /// Constructs this instance to enumerate over members. /// /// Members we are enumerating. - internal Enumerator(PSMemberInfoIntegratingCollection integratingCollection) + internal Enumerator(PSMemberInfoIntegratingCollection integratingCollection) { using (PSObject.MemberResolution.TraceScope("Enumeration Start")) { @@ -5070,7 +5070,7 @@ public bool MoveNext() { _currentIndex++; - S member = null; + T member = null; while (_currentIndex < _allMembers.Count) { member = _allMembers[_currentIndex]; @@ -5096,7 +5096,7 @@ public bool MoveNext() /// Current PSMemberInfo in the enumeration. /// /// For invalid arguments. - S IEnumerator.Current + T IEnumerator.Current { get { @@ -5109,7 +5109,7 @@ S IEnumerator.Current } } - object IEnumerator.Current => ((IEnumerator)this).Current; + object IEnumerator.Current => ((IEnumerator)this).Current; void IEnumerator.Reset() {