From 6e4db66b61665506d32e746590e5e1955722bd1e Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Mon, 4 Nov 2019 12:41:38 +0100 Subject: [PATCH] Fix CA1825:AvoidZeroLengthArrayAllocations, use Empty.Array consistently. --- ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs | 2 +- .../CSharp/Resolver/CSharpResolver.cs | 12 ++++++------ .../CSharp/Resolver/TypeInference.cs | 6 ++---- .../Documentation/XmlDocumentationProvider.cs | 7 ++++--- ICSharpCode.Decompiler/Util/EmptyList.cs | 2 +- .../Analyzers/Builtin/AttributeAppliedToAnalyzer.cs | 3 ++- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs b/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs index c9cd50eab..a8474dd41 100644 --- a/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs +++ b/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs @@ -369,7 +369,7 @@ namespace ICSharpCode.Decompiler.CSharp } expr.Arguments.AddRange(args.Select(arg => arg.Expression)); return expr.WithILInstruction(inst) - .WithRR(new ArrayCreateResolveResult(new ArrayType(compilation, inst.Type, dimensions), args.Select(a => a.ResolveResult).ToList(), new ResolveResult[0])); + .WithRR(new ArrayCreateResolveResult(new ArrayType(compilation, inst.Type, dimensions), args.Select(a => a.ResolveResult).ToList(), Empty.Array)); } protected internal override TranslatedExpression VisitLocAlloc(LocAlloc inst, TranslationContext context) diff --git a/ICSharpCode.Decompiler/CSharp/Resolver/CSharpResolver.cs b/ICSharpCode.Decompiler/CSharp/Resolver/CSharpResolver.cs index 0ef09c1bb..d7838bf25 100644 --- a/ICSharpCode.Decompiler/CSharp/Resolver/CSharpResolver.cs +++ b/ICSharpCode.Decompiler/CSharp/Resolver/CSharpResolver.cs @@ -379,14 +379,14 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver return UnaryOperatorResolveResult(new PointerType(expression.Type), op, expression); case UnaryOperatorType.Await: { ResolveResult getAwaiterMethodGroup = ResolveMemberAccess(expression, "GetAwaiter", EmptyList.Instance, NameLookupMode.InvocationTarget); - ResolveResult getAwaiterInvocation = ResolveInvocation(getAwaiterMethodGroup, new ResolveResult[0], argumentNames: null, allowOptionalParameters: false); + ResolveResult getAwaiterInvocation = ResolveInvocation(getAwaiterMethodGroup, Empty.Array, argumentNames: null, allowOptionalParameters: false); var lookup = CreateMemberLookup(); IMethod getResultMethod; IType awaitResultType; var getResultMethodGroup = lookup.Lookup(getAwaiterInvocation, "GetResult", EmptyList.Instance, true) as MethodGroupResolveResult; if (getResultMethodGroup != null) { - var getResultOR = getResultMethodGroup.PerformOverloadResolution(compilation, new ResolveResult[0], allowExtensionMethods: false, conversions: conversions); + var getResultOR = getResultMethodGroup.PerformOverloadResolution(compilation, Empty.Array, allowExtensionMethods: false, conversions: conversions); getResultMethod = getResultOR.FoundApplicableCandidate ? getResultOR.GetBestCandidateWithSubstitutedTypeArguments() as IMethod : null; awaitResultType = getResultMethod != null ? getResultMethod.ReturnType : SpecialType.UnknownType; } @@ -1698,12 +1698,12 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver } getEnumeratorInvocation = ResolveCast(collectionType, expression); getEnumeratorInvocation = ResolveMemberAccess(getEnumeratorInvocation, "GetEnumerator", EmptyList.Instance, NameLookupMode.InvocationTarget); - getEnumeratorInvocation = ResolveInvocation(getEnumeratorInvocation, new ResolveResult[0]); + getEnumeratorInvocation = ResolveInvocation(getEnumeratorInvocation, Empty.Array); } else { var getEnumeratorMethodGroup = memberLookup.Lookup(expression, "GetEnumerator", EmptyList.Instance, true) as MethodGroupResolveResult; if (getEnumeratorMethodGroup != null) { var or = getEnumeratorMethodGroup.PerformOverloadResolution( - compilation, new ResolveResult[0], + compilation, Empty.Array, allowExtensionMethods: false, allowExpandingParams: false, allowOptionalParameters: false); if (or.FoundApplicableCandidate && !or.IsAmbiguous && !or.BestCandidate.IsStatic && or.BestCandidate.Accessibility == Accessibility.Public) { collectionType = expression.Type; @@ -1722,7 +1722,7 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver var moveNextMethodGroup = memberLookup.Lookup(new ResolveResult(enumeratorType), "MoveNext", EmptyList.Instance, false) as MethodGroupResolveResult; if (moveNextMethodGroup != null) { var or = moveNextMethodGroup.PerformOverloadResolution( - compilation, new ResolveResult[0], + compilation, Empty.Array, allowExtensionMethods: false, allowExpandingParams: false, allowOptionalParameters: false); moveNextMethod = or.GetBestCandidateWithSubstitutedTypeArguments() as IMethod; } @@ -1763,7 +1763,7 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver } getEnumeratorInvocation = ResolveCast(collectionType, expression); getEnumeratorInvocation = ResolveMemberAccess(getEnumeratorInvocation, "GetEnumerator", EmptyList.Instance, NameLookupMode.InvocationTarget); - getEnumeratorInvocation = ResolveInvocation(getEnumeratorInvocation, new ResolveResult[0]); + getEnumeratorInvocation = ResolveInvocation(getEnumeratorInvocation, Empty.Array); } #endregion diff --git a/ICSharpCode.Decompiler/CSharp/Resolver/TypeInference.cs b/ICSharpCode.Decompiler/CSharp/Resolver/TypeInference.cs index 40a804025..d506da820 100644 --- a/ICSharpCode.Decompiler/CSharp/Resolver/TypeInference.cs +++ b/ICSharpCode.Decompiler/CSharp/Resolver/TypeInference.cs @@ -361,8 +361,6 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver #endregion #region Input Types / Output Types (§7.5.2.3 + §7.5.2.4) - static readonly IType[] emptyTypeArray = new IType[0]; - IType[] InputTypes(ResolveResult e, IType t) { // C# 4.0 spec: §7.5.2.3 Input types @@ -377,7 +375,7 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver return inputTypes; } } - return emptyTypeArray; + return Empty.Array; } IType[] OutputTypes(ResolveResult e, IType t) @@ -390,7 +388,7 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver return new[] { m.ReturnType }; } } - return emptyTypeArray; + return Empty.Array; } static IMethod GetDelegateOrExpressionTreeSignature(IType t) diff --git a/ICSharpCode.Decompiler/Documentation/XmlDocumentationProvider.cs b/ICSharpCode.Decompiler/Documentation/XmlDocumentationProvider.cs index b92613ce7..2ef9d8ea6 100644 --- a/ICSharpCode.Decompiler/Documentation/XmlDocumentationProvider.cs +++ b/ICSharpCode.Decompiler/Documentation/XmlDocumentationProvider.cs @@ -24,6 +24,7 @@ using System.Runtime.Serialization; using System.Text; using System.Xml; using ICSharpCode.Decompiler.TypeSystem; +using ICSharpCode.Decompiler.Util; namespace ICSharpCode.Decompiler.Documentation { @@ -392,11 +393,11 @@ namespace ICSharpCode.Decompiler.Documentation } } catch (IOException) { // Ignore errors on reload; IEntity.Documentation callers aren't prepared to handle exceptions - this.index = new IndexEntry[0]; // clear index to avoid future load attempts + this.index = Empty.Array; // clear index to avoid future load attempts return null; } catch (XmlException) { - this.index = new IndexEntry[0]; // clear index to avoid future load attempts - return null; + this.index = Empty.Array; // clear index to avoid future load attempts + return null; } return GetDocumentation(key, allowReload: false); // prevent infinite reload loops } diff --git a/ICSharpCode.Decompiler/Util/EmptyList.cs b/ICSharpCode.Decompiler/Util/EmptyList.cs index 6fdef375b..5b95473e8 100644 --- a/ICSharpCode.Decompiler/Util/EmptyList.cs +++ b/ICSharpCode.Decompiler/Util/EmptyList.cs @@ -114,7 +114,7 @@ namespace ICSharpCode.Decompiler.Util public static class Empty { - public static readonly T[] Array = new T[0]; + public static readonly T[] Array = System.Array.Empty(); } public struct Unit { } diff --git a/ILSpy/Analyzers/Builtin/AttributeAppliedToAnalyzer.cs b/ILSpy/Analyzers/Builtin/AttributeAppliedToAnalyzer.cs index 08f75db76..1f35d1867 100644 --- a/ILSpy/Analyzers/Builtin/AttributeAppliedToAnalyzer.cs +++ b/ILSpy/Analyzers/Builtin/AttributeAppliedToAnalyzer.cs @@ -24,6 +24,7 @@ using System.Reflection.Metadata; using System.Runtime.InteropServices; using ICSharpCode.Decompiler; using ICSharpCode.Decompiler.TypeSystem; +using ICSharpCode.Decompiler.Util; namespace ICSharpCode.ILSpy.Analyzers.Builtin { @@ -33,7 +34,7 @@ namespace ICSharpCode.ILSpy.Analyzers.Builtin public IEnumerable Analyze(ISymbol analyzedSymbol, AnalyzerContext context) { if (!(analyzedSymbol is ITypeDefinition attributeType)) - return Array.Empty(); + return Empty.Array; var scope = context.GetScopeOf(attributeType); // TODO: DeclSecurity attributes are not supported.