Re-enable CA1825 (zero-length array allocations) (dotnet/corefx#39708)
authorStephen Toub <stoub@microsoft.com>
Wed, 24 Jul 2019 14:42:39 +0000 (10:42 -0400)
committerGitHub <noreply@github.com>
Wed, 24 Jul 2019 14:42:39 +0000 (10:42 -0400)
Commit migrated from https://github.com/dotnet/corefx/commit/d0fc0a2b193d11645acc0fe72bbf1b97678a5161

18 files changed:
src/libraries/CodeAnalysis.ruleset
src/libraries/Common/src/System/Runtime/Serialization/SerializationGuard.cs
src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/DynamicDebuggerProxy.cs
src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Minimal.cs
src/libraries/System.Data.OleDb/src/ColumnBinding.cs
src/libraries/System.Data.OleDb/src/OleDbCommandBuilder.cs
src/libraries/System.Data.OleDb/src/OleDbConnectionInternal.cs
src/libraries/System.Data.OleDb/src/OleDbDataReader.cs
src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.cs
src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/EventLogRecord.cs
src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/NativeWrapper.cs
src/libraries/System.Linq.Expressions/src/System/Dynamic/DynamicObject.cs
src/libraries/System.Net.Security/src/System/Net/Security/NegoState.cs
src/libraries/System.Private.Xml/src/System/Xml/Dom/XPathNodeList.cs
src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/Utilities/EmptyArray.cs
src/libraries/System.Text.Encodings.Web/src/System/Text/Encodings/Web/TextEncoder.cs
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReflectionMemberAccessor.cs
src/libraries/System.Threading.Tasks.Dataflow/src/Internal/ImmutableArray.cs

index 199a9e9..7bf718d 100644 (file)
@@ -55,7 +55,6 @@
     <Rule Id="CA1820" Action="None" /> <!-- Test for empty strings using string length -->
     <Rule Id="CA1822" Action="None" /> <!-- Mark members as static -->
     <Rule Id="CA1823" Action="None" /> <!-- Avoid unused private fields -->
-    <Rule Id="CA1825" Action="None" /> <!-- Avoid zero-length array allocations. -->
     <Rule Id="CA2000" Action="None" /> <!-- Dispose objects before losing scope -->
     <Rule Id="CA2010" Action="None" /> <!-- Always consume the value returned by methods marked with PreserveSigAttribute -->
     <Rule Id="CA2100" Action="None" /> <!-- Review SQL queries for security vulnerabilities -->
index e875455..72ededa 100644 (file)
@@ -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<ParameterModifier>());
 
             if (throwMethod != null)
             {
index d27b382..8fbc1f4 100644 (file)
@@ -437,7 +437,7 @@ namespace Microsoft.CSharp.RuntimeBinder
                     return result;
                 }
             }
-            return new KeyValuePair<string, object>[0];
+            return Array.Empty<KeyValuePair<string, object>>();
         }
 
         [Serializable]
index f453f29..04da719 100644 (file)
@@ -39,7 +39,9 @@ namespace System.Collections.Immutable
         /// <summary>
         /// An empty (initialized) instance of <see cref="ImmutableArray{T}"/>.
         /// </summary>
+#pragma warning disable CA1825 // Array.Empty<T>() doesn't exist in all configurations
         public static readonly ImmutableArray<T> Empty = new ImmutableArray<T>(new T[0]);
+#pragma warning restore CA1825
 
         /// <summary>
         /// The backing field for this instance. References to this value should never be shared with outside code.
index 50bcc2e..607052b 100644 (file)
@@ -685,7 +685,7 @@ namespace System.Data.OleDb
                     bindings.DangerousRelease();
                 }
             }
-            return ((null != value) ? value : new byte[0]);
+            return ((null != value) ? value : Array.Empty<byte>());
         }
         private void Value_ByRefBYTES(Byte[] value)
         {
index 814e8a9..f1c3550 100644 (file)
@@ -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<OleDbParameter>();
 
             if (connection.SupportSchemaRowset(OleDbSchemaGuid.Procedure_Parameters))
             {
index 59e5d58..6c68e31 100644 (file)
@@ -617,7 +617,7 @@ namespace System.Data.OleDb
         {
             if (null == restrictions)
             {
-                restrictions = new object[0];
+                restrictions = Array.Empty<object>();
             }
             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<Guid>();
             }
             using (PropertyIDSet propidset = new PropertyIDSet(propertySets))
             {
index ced5a8a..07861ac 100644 (file)
@@ -311,7 +311,7 @@ namespace System.Data.OleDb
             {
                 _hasRows = false;
                 _visibleFieldCount = 0;
-                _metadata = new MetaData[0];
+                _metadata = Array.Empty<MetaData>();
             }
         }
 
index 743bced..9dcead8 100644 (file)
@@ -31,7 +31,9 @@ namespace System.Diagnostics
     /// </summary>
     public partial class Activity
     {
-        private static readonly IEnumerable<KeyValuePair<string, string>> s_emptyBaggageTags = new KeyValuePair<string, string>[0]; // Array.Empty<T>() doesn't exist in all configurations
+#pragma warning disable CA1825 // Array.Empty<T>() doesn't exist in all configurations
+        private static readonly IEnumerable<KeyValuePair<string, string>> s_emptyBaggageTags = new KeyValuePair<string, string>[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;
index ac9532f..64a0d07 100644 (file)
@@ -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<string>();
             int i = 0;
             foreach (object o in values)
             {
index 5b05caa..2cb4af6 100644 (file)
@@ -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>();
                     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<short>();
                     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<int>();
                     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<long>();
                     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<float>();
                     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>();
                     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<bool>();
             }
             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<DateTime>();
             }
             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<DateTime>();
             }
             else
             {
@@ -1344,7 +1344,7 @@ namespace System.Diagnostics.Eventing.Reader
         {
             if (val.Reference == IntPtr.Zero)
             {
-                return new string[0];
+                return Array.Empty<string>();
             }
             else
             {
index d7ffc87..23aa5ce 100644 (file)
@@ -442,7 +442,9 @@ namespace System.Dynamic
 
             private delegate DynamicMetaObject Fallback<TBinder>(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<Expression> GetConvertedArgs(params Expression[] args)
             {
index d225969..bd6ecd7 100644 (file)
@@ -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);
 
index c87a4eb..7dd2223 100644 (file)
@@ -34,8 +34,6 @@ namespace System.Xml
             }
         }
 
-        private static readonly object[] s_nullparams = { };
-
         private XmlNode GetNode(XPathNavigator n)
         {
             IHasXmlNode iHasNode = (IHasXmlNode)n;
index 94a14ff..ee60cfe 100644 (file)
@@ -6,6 +6,8 @@ namespace System.Reflection.Internal
 {
     internal static class EmptyArray<T>
     {
+#pragma warning disable CA1825 // Array.Empty<T>() doesn't exist in all configurations
         internal static readonly T[] Instance = new T[0];
+#pragma warning restore CA1825
     }
 }
index 0a3cae9..082abf9 100644 (file)
@@ -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<byte> since used as a singleton for comparison.
+#pragma warning disable CA1825 // should not be Array.Empty<byte> 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, 
index 9e73e49..2214e79 100644 (file)
@@ -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<object>());
             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<object>());
             creator.RegisterCreatorDelegateFromMethod(createRange);
             return creator;
         }
index 96336e5..4ef93e6 100644 (file)
@@ -26,7 +26,9 @@ namespace System.Threading.Tasks.Dataflow.Internal
     internal readonly struct ImmutableArray<T>
     {
         /// <summary>An empty array.</summary>
+#pragma warning disable CA1825 // Array.Empty<T>() doesn't exist in all configurations
         private static readonly ImmutableArray<T> s_empty = new ImmutableArray<T>(new T[0]);
+#pragma warning restore CA1825
         /// <summary>The immutable data in this array instance.</summary>
         private readonly T[] _array;