Skip to content

Commit 210a7a9

Browse files
authored
Fix most violations of new CA1859 (#80335)
* Fix most violations of new CA1859 The analyzer recommends replacing uses of types with other types that could reduce overheads (e.g. `List<T>` instead of `IList<T>`). This fixes most of the violations we currently have of the rule, but it doesn't enable it yet because there are too many false positives; fixes for those are in-flight. Some of these replacements won't help with perf (e.g. using `ArgumentException` instead of `Exception` as the return type of a throw helper create method), but there's little harm to them, and some of them do avoid overheads like allocation (e.g. foreach'ing something typed as `Dictionary<>` instead of `IDictionary<>` will avoid an enumerator allocation, accessing `Count` on a `List<T>` instead of `IList<T>` will avoid an interface dispatch, etc.) * Fix mistaken replacement
1 parent 1630725 commit 210a7a9

File tree

237 files changed

+648
-710
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

237 files changed

+648
-710
lines changed

src/coreclr/System.Private.CoreLib/src/System/Reflection/Assembly.CoreCLR.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public static Assembly GetCallingAssembly()
8383
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "AssemblyNative_GetEntryAssembly")]
8484
private static partial void GetEntryAssemblyNative(ObjectHandleOnStack retAssembly);
8585

86-
private static Assembly? GetEntryAssemblyInternal()
86+
private static RuntimeAssembly? GetEntryAssemblyInternal()
8787
{
8888
RuntimeAssembly? entryAssembly = null;
8989
GetEntryAssemblyNative(ObjectHandleOnStack.Create(ref entryAssembly));

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/DynamicILGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ public override void EndScope()
403403
throw new NotSupportedException(SR.InvalidOperation_NotAllowedInDynamicMethod);
404404
}
405405

406-
private int GetMemberRefToken(MethodBase methodInfo, Type[]? optionalParameterTypes)
406+
private int GetMemberRefToken(MethodInfo methodInfo, Type[]? optionalParameterTypes)
407407
{
408408
Type[]? parameterTypes;
409409
Type[][]? requiredCustomModifiers;

src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Collections.Generic;
5+
using System.Collections.ObjectModel;
56
using System.Diagnostics;
67
using System.Diagnostics.CodeAnalysis;
78
using System.Runtime.CompilerServices;
@@ -92,7 +93,7 @@ internal static IList<CustomAttributeData> GetCustomAttributesInternal(RuntimePa
9293
return pcas.Count > 0 ? GetCombinedList(cad, ref pcas) : cad;
9394
}
9495

95-
private static IList<CustomAttributeData> GetCombinedList(IList<CustomAttributeData> customAttributes, ref RuntimeType.ListBuilder<Attribute> pseudoAttributes)
96+
private static ReadOnlyCollection<CustomAttributeData> GetCombinedList(IList<CustomAttributeData> customAttributes, ref RuntimeType.ListBuilder<Attribute> pseudoAttributes)
9697
{
9798
Debug.Assert(pseudoAttributes.Count != 0);
9899

src/coreclr/System.Private.CoreLib/src/System/Runtime/Loader/AssemblyLoadContext.CoreCLR.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public partial class AssemblyLoadContext
5454
internal static partial bool TraceSatelliteSubdirectoryPathProbed(string filePath, int hResult);
5555

5656
[RequiresUnreferencedCode("Types and members the loaded assembly depends on might be removed")]
57-
private Assembly InternalLoadFromPath(string? assemblyPath, string? nativeImagePath)
57+
private RuntimeAssembly InternalLoadFromPath(string? assemblyPath, string? nativeImagePath)
5858
{
5959
RuntimeAssembly? loadedAssembly = null;
6060
LoadFromPath(_nativeAssemblyLoadContext, assemblyPath, nativeImagePath, ObjectHandleOnStack.Create(ref loadedAssembly));

src/coreclr/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1995,7 +1995,7 @@ internal static FieldInfo GetFieldInfo(RuntimeType? reflectedType, IRuntimeField
19951995
}
19961996

19971997
// Called internally
1998-
private static PropertyInfo GetPropertyInfo(RuntimeType reflectedType, int tkProperty)
1998+
private static RuntimePropertyInfo GetPropertyInfo(RuntimeType reflectedType, int tkProperty)
19991999
{
20002000
RuntimePropertyInfo property;
20012001
RuntimePropertyInfo[] candidates =
@@ -3166,7 +3166,7 @@ public override MemberInfo GetMemberWithSameMetadataDefinitionAs(MemberInfo memb
31663166
throw CreateGetMemberWithSameMetadataDefinitionAsNotFoundException(member);
31673167
}
31683168

3169-
private static MemberInfo? GetMethodWithSameMetadataDefinitionAs(RuntimeType runtimeType, MemberInfo method)
3169+
private static RuntimeMethodInfo? GetMethodWithSameMetadataDefinitionAs(RuntimeType runtimeType, MemberInfo method)
31703170
{
31713171
RuntimeMethodInfo[] cache = runtimeType.Cache.GetMethodList(MemberListType.CaseSensitive, method.Name);
31723172

@@ -3182,7 +3182,7 @@ public override MemberInfo GetMemberWithSameMetadataDefinitionAs(MemberInfo memb
31823182
return null;
31833183
}
31843184

3185-
private static MemberInfo? GetConstructorWithSameMetadataDefinitionAs(RuntimeType runtimeType, MemberInfo constructor)
3185+
private static RuntimeConstructorInfo? GetConstructorWithSameMetadataDefinitionAs(RuntimeType runtimeType, MemberInfo constructor)
31863186
{
31873187
RuntimeConstructorInfo[] cache = runtimeType.Cache.GetConstructorList(MemberListType.CaseSensitive, constructor.Name);
31883188

@@ -3198,7 +3198,7 @@ public override MemberInfo GetMemberWithSameMetadataDefinitionAs(MemberInfo memb
31983198
return null;
31993199
}
32003200

3201-
private static MemberInfo? GetPropertyWithSameMetadataDefinitionAs(RuntimeType runtimeType, MemberInfo property)
3201+
private static RuntimePropertyInfo? GetPropertyWithSameMetadataDefinitionAs(RuntimeType runtimeType, MemberInfo property)
32023202
{
32033203
RuntimePropertyInfo[] cache = runtimeType.Cache.GetPropertyList(MemberListType.CaseSensitive, property.Name);
32043204

@@ -3214,7 +3214,7 @@ public override MemberInfo GetMemberWithSameMetadataDefinitionAs(MemberInfo memb
32143214
return null;
32153215
}
32163216

3217-
private static MemberInfo? GetFieldWithSameMetadataDefinitionAs(RuntimeType runtimeType, MemberInfo field)
3217+
private static RuntimeFieldInfo? GetFieldWithSameMetadataDefinitionAs(RuntimeType runtimeType, MemberInfo field)
32183218
{
32193219
RuntimeFieldInfo[] cache = runtimeType.Cache.GetFieldList(MemberListType.CaseSensitive, field.Name);
32203220

@@ -3230,7 +3230,7 @@ public override MemberInfo GetMemberWithSameMetadataDefinitionAs(MemberInfo memb
32303230
return null;
32313231
}
32323232

3233-
private static MemberInfo? GetEventWithSameMetadataDefinitionAs(RuntimeType runtimeType, MemberInfo eventInfo)
3233+
private static RuntimeEventInfo? GetEventWithSameMetadataDefinitionAs(RuntimeType runtimeType, MemberInfo eventInfo)
32343234
{
32353235
RuntimeEventInfo[] cache = runtimeType.Cache.GetEventList(MemberListType.CaseSensitive, eventInfo.Name);
32363236

@@ -3246,7 +3246,7 @@ public override MemberInfo GetMemberWithSameMetadataDefinitionAs(MemberInfo memb
32463246
return null;
32473247
}
32483248

3249-
private static MemberInfo? GetNestedTypeWithSameMetadataDefinitionAs(RuntimeType runtimeType, MemberInfo nestedType)
3249+
private static RuntimeType? GetNestedTypeWithSameMetadataDefinitionAs(RuntimeType runtimeType, MemberInfo nestedType)
32503250
{
32513251
RuntimeType[] cache = runtimeType.Cache.GetNestedTypeList(MemberListType.CaseSensitive, nestedType.Name);
32523252

src/libraries/Common/src/Interop/Windows/BCrypt/Cng.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
using System.Text;
66
using System.Diagnostics;
77
using System.Runtime.InteropServices;
8+
using System.Security.Cryptography;
89

910
using Internal.Cryptography;
11+
using Microsoft.Win32.SafeHandles;
12+
1013
using static Interop;
1114
using static Interop.BCrypt;
12-
using Microsoft.Win32.SafeHandles;
1315

1416
namespace Internal.NativeCrypto
1517
{
@@ -112,7 +114,7 @@ public static void SetEffectiveKeyLength(this SafeAlgorithmHandle hAlg, int effe
112114
}
113115
}
114116

115-
private static Exception CreateCryptographicException(NTSTATUS ntStatus)
117+
private static CryptographicException CreateCryptographicException(NTSTATUS ntStatus)
116118
{
117119
int hr = ((int)ntStatus) | 0x01000000;
118120
return hr.ToCryptographicException();

src/libraries/Common/src/System/Security/Cryptography/RSAAndroid.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ internal SafeRsaHandle DuplicateKeyHandle()
792792
return _key.Value.DuplicateHandle();
793793
}
794794

795-
private static Exception PaddingModeNotSupported() =>
795+
private static CryptographicException PaddingModeNotSupported() =>
796796
new CryptographicException(SR.Cryptography_InvalidPaddingMode);
797797
}
798798
}

src/libraries/Common/src/System/Security/Cryptography/RSAOpenSsl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@ private static void ValidatePadding(RSASignaturePadding padding)
902902

903903
static partial void ThrowIfNotSupported();
904904

905-
private static Exception PaddingModeNotSupported() =>
905+
private static CryptographicException PaddingModeNotSupported() =>
906906
new CryptographicException(SR.Cryptography_InvalidPaddingMode);
907907
}
908908
}

src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ private static Array BindArray(Type type, IEnumerable? source, IConfiguration co
691691
elementType = type.GetGenericArguments()[0];
692692
}
693693

694-
IList list = new List<object?>();
694+
var list = new List<object?>();
695695

696696
if (source != null)
697697
{
@@ -727,7 +727,7 @@ private static Array BindArray(Type type, IEnumerable? source, IConfiguration co
727727
}
728728

729729
Array result = Array.CreateInstance(elementType, list.Count);
730-
list.CopyTo(result, 0);
730+
((IList)list).CopyTo(result, 0);
731731
return result;
732732
}
733733

@@ -976,7 +976,7 @@ private static List<PropertyInfo> GetAllProperties([DynamicallyAccessedMembers(D
976976
return propertyBindingPoint.Value;
977977
}
978978

979-
private static string GetPropertyName(MemberInfo property)
979+
private static string GetPropertyName(PropertyInfo property)
980980
{
981981
ThrowHelper.ThrowIfNull(property);
982982

src/libraries/Microsoft.Extensions.Configuration.Json/src/JsonConfigurationFileParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ private JsonConfigurationFileParser() { }
1919
public static IDictionary<string, string?> Parse(Stream input)
2020
=> new JsonConfigurationFileParser().ParseStream(input);
2121

22-
private IDictionary<string, string?> ParseStream(Stream input)
22+
private Dictionary<string, string?> ParseStream(Stream input)
2323
{
2424
var jsonDocumentOptions = new JsonDocumentOptions
2525
{

0 commit comments

Comments
 (0)