From d4233d856014b1b7ecf79f7c880a1de0d44472a3 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Wed, 24 Jul 2019 10:42:39 -0400 Subject: [PATCH] Re-enable CA1825 (zero-length array allocations) (dotnet/corefx#39708) Commit migrated from https://github.com/dotnet/corefx/commit/d0fc0a2b193d11645acc0fe72bbf1b97678a5161 --- src/libraries/CodeAnalysis.ruleset | 1 - .../Runtime/Serialization/SerializationGuard.cs | 2 +- .../CSharp/RuntimeBinder/DynamicDebuggerProxy.cs | 2 +- .../Immutable/ImmutableArray_1.Minimal.cs | 2 ++ src/libraries/System.Data.OleDb/src/ColumnBinding.cs | 2 +- .../System.Data.OleDb/src/OleDbCommandBuilder.cs | 2 +- .../System.Data.OleDb/src/OleDbConnectionInternal.cs | 4 ++-- .../System.Data.OleDb/src/OleDbDataReader.cs | 2 +- .../src/System/Diagnostics/Activity.cs | 4 +++- .../src/System/Diagnostics/Reader/EventLogRecord.cs | 2 +- .../src/System/Diagnostics/Reader/NativeWrapper.cs | 20 ++++++++++---------- .../src/System/Dynamic/DynamicObject.cs | 4 +++- .../src/System/Net/Security/NegoState.cs | 4 +++- .../src/System/Xml/Dom/XPathNodeList.cs | 2 -- .../Reflection/Internal/Utilities/EmptyArray.cs | 2 ++ .../src/System/Text/Encodings/Web/TextEncoder.cs | 4 +++- .../Json/Serialization/ReflectionMemberAccessor.cs | 4 ++-- .../src/Internal/ImmutableArray.cs | 2 ++ 18 files changed, 38 insertions(+), 27 deletions(-) diff --git a/src/libraries/CodeAnalysis.ruleset b/src/libraries/CodeAnalysis.ruleset index 199a9e9..7bf718d 100644 --- a/src/libraries/CodeAnalysis.ruleset +++ b/src/libraries/CodeAnalysis.ruleset @@ -55,7 +55,6 @@ - diff --git a/src/libraries/Common/src/System/Runtime/Serialization/SerializationGuard.cs b/src/libraries/Common/src/System/Runtime/Serialization/SerializationGuard.cs index e875455..72ededa 100644 --- a/src/libraries/Common/src/System/Runtime/Serialization/SerializationGuard.cs +++ b/src/libraries/Common/src/System/Runtime/Serialization/SerializationGuard.cs @@ -22,7 +22,7 @@ namespace System.Runtime.Serialization { ThrowIfDeserializationInProgressWithSwitchDel throwIfDeserializationInProgressDelegate = null; MethodInfo throwMethod = typeof(SerializationInfo).GetMethod("ThrowIfDeserializationInProgress", - BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public, null, new Type[] { typeof(string), typeof(int).MakeByRefType() }, new ParameterModifier[0]); + BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public, null, new Type[] { typeof(string), typeof(int).MakeByRefType() }, Array.Empty()); if (throwMethod != null) { diff --git a/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/DynamicDebuggerProxy.cs b/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/DynamicDebuggerProxy.cs index d27b382..8fbc1f4 100644 --- a/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/DynamicDebuggerProxy.cs +++ b/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/DynamicDebuggerProxy.cs @@ -437,7 +437,7 @@ namespace Microsoft.CSharp.RuntimeBinder return result; } } - return new KeyValuePair[0]; + return Array.Empty>(); } [Serializable] diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Minimal.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Minimal.cs index f453f29..04da719 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Minimal.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Minimal.cs @@ -39,7 +39,9 @@ namespace System.Collections.Immutable /// /// An empty (initialized) instance of . /// +#pragma warning disable CA1825 // Array.Empty() doesn't exist in all configurations public static readonly ImmutableArray Empty = new ImmutableArray(new T[0]); +#pragma warning restore CA1825 /// /// The backing field for this instance. References to this value should never be shared with outside code. diff --git a/src/libraries/System.Data.OleDb/src/ColumnBinding.cs b/src/libraries/System.Data.OleDb/src/ColumnBinding.cs index 50bcc2e..607052b 100644 --- a/src/libraries/System.Data.OleDb/src/ColumnBinding.cs +++ b/src/libraries/System.Data.OleDb/src/ColumnBinding.cs @@ -685,7 +685,7 @@ namespace System.Data.OleDb bindings.DangerousRelease(); } } - return ((null != value) ? value : new byte[0]); + return ((null != value) ? value : Array.Empty()); } private void Value_ByRefBYTES(Byte[] value) { diff --git a/src/libraries/System.Data.OleDb/src/OleDbCommandBuilder.cs b/src/libraries/System.Data.OleDb/src/OleDbCommandBuilder.cs index 814e8a9..f1c3550 100644 --- a/src/libraries/System.Data.OleDb/src/OleDbCommandBuilder.cs +++ b/src/libraries/System.Data.OleDb/src/OleDbCommandBuilder.cs @@ -149,7 +149,7 @@ namespace System.Data.OleDb // the return value gets marked as an output parameter. static private OleDbParameter[] DeriveParametersFromStoredProcedure(OleDbConnection connection, OleDbCommand command) { - OleDbParameter[] plist = new OleDbParameter[0]; + OleDbParameter[] plist = Array.Empty(); if (connection.SupportSchemaRowset(OleDbSchemaGuid.Procedure_Parameters)) { diff --git a/src/libraries/System.Data.OleDb/src/OleDbConnectionInternal.cs b/src/libraries/System.Data.OleDb/src/OleDbConnectionInternal.cs index 59e5d58..6c68e31 100644 --- a/src/libraries/System.Data.OleDb/src/OleDbConnectionInternal.cs +++ b/src/libraries/System.Data.OleDb/src/OleDbConnectionInternal.cs @@ -617,7 +617,7 @@ namespace System.Data.OleDb { if (null == restrictions) { - restrictions = new object[0]; + restrictions = Array.Empty(); } DataTable dataTable = null; using (IDBSchemaRowsetWrapper wrapper = IDBSchemaRowset()) @@ -827,7 +827,7 @@ namespace System.Data.OleDb if (null == propertySets) { - propertySets = new Guid[0]; + propertySets = Array.Empty(); } using (PropertyIDSet propidset = new PropertyIDSet(propertySets)) { diff --git a/src/libraries/System.Data.OleDb/src/OleDbDataReader.cs b/src/libraries/System.Data.OleDb/src/OleDbDataReader.cs index ced5a8a..07861ac 100644 --- a/src/libraries/System.Data.OleDb/src/OleDbDataReader.cs +++ b/src/libraries/System.Data.OleDb/src/OleDbDataReader.cs @@ -311,7 +311,7 @@ namespace System.Data.OleDb { _hasRows = false; _visibleFieldCount = 0; - _metadata = new MetaData[0]; + _metadata = Array.Empty(); } } diff --git a/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.cs b/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.cs index 743bced..9dcead8 100644 --- a/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.cs +++ b/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.cs @@ -31,7 +31,9 @@ namespace System.Diagnostics /// public partial class Activity { - private static readonly IEnumerable> s_emptyBaggageTags = new KeyValuePair[0]; // Array.Empty() doesn't exist in all configurations +#pragma warning disable CA1825 // Array.Empty() doesn't exist in all configurations + private static readonly IEnumerable> s_emptyBaggageTags = new KeyValuePair[0]; +#pragma warning restore CA1825 private const byte ActivityTraceFlagsIsSet = 0b_1_0000000; // Internal flag to indicate if flags have been set private const int RequestIdMaxLength = 1024; diff --git a/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/EventLogRecord.cs b/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/EventLogRecord.cs index ac9532f..64a0d07 100644 --- a/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/EventLogRecord.cs +++ b/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/EventLogRecord.cs @@ -286,7 +286,7 @@ namespace System.Diagnostics.Eventing.Reader return this.FormatDescription(); // Copy the value IEnumerable to an array. - string[] theValues = new string[0]; + string[] theValues = Array.Empty(); int i = 0; foreach (object o in values) { diff --git a/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/NativeWrapper.cs b/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/NativeWrapper.cs index 5b05caa..2cb4af6 100644 --- a/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/NativeWrapper.cs +++ b/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/NativeWrapper.cs @@ -1163,37 +1163,37 @@ namespace System.Diagnostics.Eventing.Reader case (int)UnsafeNativeMethods.EvtVariantType.EvtVarTypeBinary: case ((int)UnsafeNativeMethods.EvtMasks.EVT_VARIANT_TYPE_ARRAY | (int)UnsafeNativeMethods.EvtVariantType.EvtVarTypeByte): if (val.Reference == IntPtr.Zero) - return new Byte[0]; + return Array.Empty(); Byte[] arByte = new Byte[val.Count]; Marshal.Copy(val.Reference, arByte, 0, (int)val.Count); return arByte; case ((int)UnsafeNativeMethods.EvtMasks.EVT_VARIANT_TYPE_ARRAY | (int)UnsafeNativeMethods.EvtVariantType.EvtVarTypeInt16): if (val.Reference == IntPtr.Zero) - return new Int16[0]; + return Array.Empty(); Int16[] arInt16 = new Int16[val.Count]; Marshal.Copy(val.Reference, arInt16, 0, (int)val.Count); return arInt16; case ((int)UnsafeNativeMethods.EvtMasks.EVT_VARIANT_TYPE_ARRAY | (int)UnsafeNativeMethods.EvtVariantType.EvtVarTypeInt32): if (val.Reference == IntPtr.Zero) - return new Int32[0]; + return Array.Empty(); Int32[] arInt32 = new Int32[val.Count]; Marshal.Copy(val.Reference, arInt32, 0, (int)val.Count); return arInt32; case ((int)UnsafeNativeMethods.EvtMasks.EVT_VARIANT_TYPE_ARRAY | (int)UnsafeNativeMethods.EvtVariantType.EvtVarTypeInt64): if (val.Reference == IntPtr.Zero) - return new Int64[0]; + return Array.Empty(); Int64[] arInt64 = new Int64[val.Count]; Marshal.Copy(val.Reference, arInt64, 0, (int)val.Count); return arInt64; case ((int)UnsafeNativeMethods.EvtMasks.EVT_VARIANT_TYPE_ARRAY | (int)UnsafeNativeMethods.EvtVariantType.EvtVarTypeSingle): if (val.Reference == IntPtr.Zero) - return new Single[0]; + return Array.Empty(); Single[] arSingle = new Single[val.Count]; Marshal.Copy(val.Reference, arSingle, 0, (int)val.Count); return arSingle; case ((int)UnsafeNativeMethods.EvtMasks.EVT_VARIANT_TYPE_ARRAY | (int)UnsafeNativeMethods.EvtVariantType.EvtVarTypeDouble): if (val.Reference == IntPtr.Zero) - return new Double[0]; + return Array.Empty(); Double[] arDouble = new Double[val.Count]; Marshal.Copy(val.Reference, arDouble, 0, (int)val.Count); return arDouble; @@ -1286,7 +1286,7 @@ namespace System.Diagnostics.Eventing.Reader IntPtr ptr = val.Reference; if (ptr == IntPtr.Zero) { - return new bool[0]; + return Array.Empty(); } else { @@ -1306,7 +1306,7 @@ namespace System.Diagnostics.Eventing.Reader IntPtr ptr = val.Reference; if (ptr == IntPtr.Zero) { - return new DateTime[0]; + return Array.Empty(); } else { @@ -1325,7 +1325,7 @@ namespace System.Diagnostics.Eventing.Reader IntPtr ptr = val.Reference; if (ptr == IntPtr.Zero) { - return new DateTime[0]; + return Array.Empty(); } else { @@ -1344,7 +1344,7 @@ namespace System.Diagnostics.Eventing.Reader { if (val.Reference == IntPtr.Zero) { - return new string[0]; + return Array.Empty(); } else { diff --git a/src/libraries/System.Linq.Expressions/src/System/Dynamic/DynamicObject.cs b/src/libraries/System.Linq.Expressions/src/System/Dynamic/DynamicObject.cs index d7ffc87..23aa5ce 100644 --- a/src/libraries/System.Linq.Expressions/src/System/Dynamic/DynamicObject.cs +++ b/src/libraries/System.Linq.Expressions/src/System/Dynamic/DynamicObject.cs @@ -442,7 +442,9 @@ namespace System.Dynamic private delegate DynamicMetaObject Fallback(MetaDynamic @this, TBinder binder, DynamicMetaObject errorSuggestion); - private static readonly Expression[] s_noArgs = new Expression[0]; // used in reference comparison, requires unique object identity +#pragma warning disable CA1825 // used in reference comparison, requires unique object identity + private static readonly Expression[] s_noArgs = new Expression[0]; +#pragma warning restore CA1825 private static ReadOnlyCollection GetConvertedArgs(params Expression[] args) { diff --git a/src/libraries/System.Net.Security/src/System/Net/Security/NegoState.cs b/src/libraries/System.Net.Security/src/System/Net/Security/NegoState.cs index d225969..bd6ecd7 100644 --- a/src/libraries/System.Net.Security/src/System/Net/Security/NegoState.cs +++ b/src/libraries/System.Net.Security/src/System/Net/Security/NegoState.cs @@ -21,7 +21,9 @@ namespace System.Net.Security // internal class NegoState { - private static readonly byte[] s_emptyMessage = new byte[0]; // used in reference comparisons +#pragma warning disable CA1825 // used in reference comparison, requires unique object identity + private static readonly byte[] s_emptyMessage = new byte[0]; +#pragma warning restore CA1825 private static readonly AsyncCallback s_readCallback = new AsyncCallback(ReadCallback); private static readonly AsyncCallback s_writeCallback = new AsyncCallback(WriteCallback); diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Dom/XPathNodeList.cs b/src/libraries/System.Private.Xml/src/System/Xml/Dom/XPathNodeList.cs index c87a4eb..7dd2223 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Dom/XPathNodeList.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Dom/XPathNodeList.cs @@ -34,8 +34,6 @@ namespace System.Xml } } - private static readonly object[] s_nullparams = { }; - private XmlNode GetNode(XPathNavigator n) { IHasXmlNode iHasNode = (IHasXmlNode)n; diff --git a/src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/Utilities/EmptyArray.cs b/src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/Utilities/EmptyArray.cs index 94a14ff..ee60cfe 100644 --- a/src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/Utilities/EmptyArray.cs +++ b/src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/Utilities/EmptyArray.cs @@ -6,6 +6,8 @@ namespace System.Reflection.Internal { internal static class EmptyArray { +#pragma warning disable CA1825 // Array.Empty() doesn't exist in all configurations internal static readonly T[] Instance = new T[0]; +#pragma warning restore CA1825 } } diff --git a/src/libraries/System.Text.Encodings.Web/src/System/Text/Encodings/Web/TextEncoder.cs b/src/libraries/System.Text.Encodings.Web/src/System/Text/Encodings/Web/TextEncoder.cs index 0a3cae9..082abf95 100644 --- a/src/libraries/System.Text.Encodings.Web/src/System/Text/Encodings/Web/TextEncoder.cs +++ b/src/libraries/System.Text.Encodings.Web/src/System/Text/Encodings/Web/TextEncoder.cs @@ -22,7 +22,9 @@ namespace System.Text.Encodings.Web public abstract class TextEncoder { // Fast cache for Ascii - private static readonly byte[] s_noEscape = new byte[] { }; // Should not be Array.Empty since used as a singleton for comparison. +#pragma warning disable CA1825 // should not be Array.Empty as this is used as a singleton for comparisons + private static readonly byte[] s_noEscape = new byte[] { }; +#pragma warning restore CA1825 private byte[][] _asciiEscape = new byte[0x80][]; // The following pragma disables a warning complaining about non-CLS compliant members being abstract, diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReflectionMemberAccessor.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReflectionMemberAccessor.cs index 9e73e49..2214e79 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReflectionMemberAccessor.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReflectionMemberAccessor.cs @@ -59,7 +59,7 @@ namespace System.Text.Json Type.EmptyTypes, modifiers: null); - ImmutableCollectionCreator creator = (ImmutableCollectionCreator)constructor.Invoke(new object[] { }); + ImmutableCollectionCreator creator = (ImmutableCollectionCreator)constructor.Invoke(Array.Empty()); creator.RegisterCreatorDelegateFromMethod(createRange); return creator; } @@ -82,7 +82,7 @@ namespace System.Text.Json Type.EmptyTypes, modifiers: null); - ImmutableCollectionCreator creator = (ImmutableCollectionCreator)constructor.Invoke(new object[] { }); + ImmutableCollectionCreator creator = (ImmutableCollectionCreator)constructor.Invoke(Array.Empty()); creator.RegisterCreatorDelegateFromMethod(createRange); return creator; } diff --git a/src/libraries/System.Threading.Tasks.Dataflow/src/Internal/ImmutableArray.cs b/src/libraries/System.Threading.Tasks.Dataflow/src/Internal/ImmutableArray.cs index 96336e5..4ef93e6 100644 --- a/src/libraries/System.Threading.Tasks.Dataflow/src/Internal/ImmutableArray.cs +++ b/src/libraries/System.Threading.Tasks.Dataflow/src/Internal/ImmutableArray.cs @@ -26,7 +26,9 @@ namespace System.Threading.Tasks.Dataflow.Internal internal readonly struct ImmutableArray { /// An empty array. +#pragma warning disable CA1825 // Array.Empty() doesn't exist in all configurations private static readonly ImmutableArray s_empty = new ImmutableArray(new T[0]); +#pragma warning restore CA1825 /// The immutable data in this array instance. private readonly T[] _array; -- 2.7.4