Browse Source

Fix CA1825:AvoidZeroLengthArrayAllocations, use Empty<T>.Array consistently.

pull/1790/head
Siegfried Pammer 6 years ago
parent
commit
6e4db66b61
  1. 2
      ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
  2. 12
      ICSharpCode.Decompiler/CSharp/Resolver/CSharpResolver.cs
  3. 6
      ICSharpCode.Decompiler/CSharp/Resolver/TypeInference.cs
  4. 7
      ICSharpCode.Decompiler/Documentation/XmlDocumentationProvider.cs
  5. 2
      ICSharpCode.Decompiler/Util/EmptyList.cs
  6. 3
      ILSpy/Analyzers/Builtin/AttributeAppliedToAnalyzer.cs

2
ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

@ -369,7 +369,7 @@ namespace ICSharpCode.Decompiler.CSharp @@ -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<ResolveResult>.Array));
}
protected internal override TranslatedExpression VisitLocAlloc(LocAlloc inst, TranslationContext context)

12
ICSharpCode.Decompiler/CSharp/Resolver/CSharpResolver.cs

@ -379,14 +379,14 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver @@ -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<IType>.Instance, NameLookupMode.InvocationTarget);
ResolveResult getAwaiterInvocation = ResolveInvocation(getAwaiterMethodGroup, new ResolveResult[0], argumentNames: null, allowOptionalParameters: false);
ResolveResult getAwaiterInvocation = ResolveInvocation(getAwaiterMethodGroup, Empty<ResolveResult>.Array, argumentNames: null, allowOptionalParameters: false);
var lookup = CreateMemberLookup();
IMethod getResultMethod;
IType awaitResultType;
var getResultMethodGroup = lookup.Lookup(getAwaiterInvocation, "GetResult", EmptyList<IType>.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<ResolveResult>.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 @@ -1698,12 +1698,12 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver
}
getEnumeratorInvocation = ResolveCast(collectionType, expression);
getEnumeratorInvocation = ResolveMemberAccess(getEnumeratorInvocation, "GetEnumerator", EmptyList<IType>.Instance, NameLookupMode.InvocationTarget);
getEnumeratorInvocation = ResolveInvocation(getEnumeratorInvocation, new ResolveResult[0]);
getEnumeratorInvocation = ResolveInvocation(getEnumeratorInvocation, Empty<ResolveResult>.Array);
} else {
var getEnumeratorMethodGroup = memberLookup.Lookup(expression, "GetEnumerator", EmptyList<IType>.Instance, true) as MethodGroupResolveResult;
if (getEnumeratorMethodGroup != null) {
var or = getEnumeratorMethodGroup.PerformOverloadResolution(
compilation, new ResolveResult[0],
compilation, Empty<ResolveResult>.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 @@ -1722,7 +1722,7 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver
var moveNextMethodGroup = memberLookup.Lookup(new ResolveResult(enumeratorType), "MoveNext", EmptyList<IType>.Instance, false) as MethodGroupResolveResult;
if (moveNextMethodGroup != null) {
var or = moveNextMethodGroup.PerformOverloadResolution(
compilation, new ResolveResult[0],
compilation, Empty<ResolveResult>.Array,
allowExtensionMethods: false, allowExpandingParams: false, allowOptionalParameters: false);
moveNextMethod = or.GetBestCandidateWithSubstitutedTypeArguments() as IMethod;
}
@ -1763,7 +1763,7 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver @@ -1763,7 +1763,7 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver
}
getEnumeratorInvocation = ResolveCast(collectionType, expression);
getEnumeratorInvocation = ResolveMemberAccess(getEnumeratorInvocation, "GetEnumerator", EmptyList<IType>.Instance, NameLookupMode.InvocationTarget);
getEnumeratorInvocation = ResolveInvocation(getEnumeratorInvocation, new ResolveResult[0]);
getEnumeratorInvocation = ResolveInvocation(getEnumeratorInvocation, Empty<ResolveResult>.Array);
}
#endregion

6
ICSharpCode.Decompiler/CSharp/Resolver/TypeInference.cs

@ -361,8 +361,6 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver @@ -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 @@ -377,7 +375,7 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver
return inputTypes;
}
}
return emptyTypeArray;
return Empty<IType>.Array;
}
IType[] OutputTypes(ResolveResult e, IType t)
@ -390,7 +388,7 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver @@ -390,7 +388,7 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver
return new[] { m.ReturnType };
}
}
return emptyTypeArray;
return Empty<IType>.Array;
}
static IMethod GetDelegateOrExpressionTreeSignature(IType t)

7
ICSharpCode.Decompiler/Documentation/XmlDocumentationProvider.cs

@ -24,6 +24,7 @@ using System.Runtime.Serialization; @@ -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 @@ -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<IndexEntry>.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<IndexEntry>.Array; // clear index to avoid future load attempts
return null;
}
return GetDocumentation(key, allowReload: false); // prevent infinite reload loops
}

2
ICSharpCode.Decompiler/Util/EmptyList.cs

@ -114,7 +114,7 @@ namespace ICSharpCode.Decompiler.Util @@ -114,7 +114,7 @@ namespace ICSharpCode.Decompiler.Util
public static class Empty<T>
{
public static readonly T[] Array = new T[0];
public static readonly T[] Array = System.Array.Empty<T>();
}
public struct Unit { }

3
ILSpy/Analyzers/Builtin/AttributeAppliedToAnalyzer.cs

@ -24,6 +24,7 @@ using System.Reflection.Metadata; @@ -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 @@ -33,7 +34,7 @@ namespace ICSharpCode.ILSpy.Analyzers.Builtin
public IEnumerable<ISymbol> Analyze(ISymbol analyzedSymbol, AnalyzerContext context)
{
if (!(analyzedSymbol is ITypeDefinition attributeType))
return Array.Empty<ISymbol>();
return Empty<ISymbol>.Array;
var scope = context.GetScopeOf(attributeType);
// TODO: DeclSecurity attributes are not supported.

Loading…
Cancel
Save