Skip to content

Commit 0e7bff2

Browse files
authored
Use Array.Empty (#1588)
1 parent 4fb3dfc commit 0e7bff2

19 files changed

Lines changed: 34 additions & 37 deletions

File tree

.editorconfig

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,3 @@ dotnet_diagnostic.CA5350.severity = suggestion # CA5350: Do Not Use Weak Crypto
9393
dotnet_diagnostic.CA5351.severity = suggestion # CA5351: Do Not Use Broken Cryptographic Algorithms
9494
dotnet_diagnostic.CA5359.severity = suggestion # CA5359: Do Not Disable Certificate Validation
9595
dotnet_diagnostic.CA5372.severity = suggestion # CA5372: Use XmlReader For XPathDocument
96-
97-
# TODO: remove this
98-
dotnet_diagnostic.CA1825.severity = suggestion # CA1825: Avoid zero-length array allocations

Src/IronPython.Modules/_ctypes/StructType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static partial class CTypes {
3232
public class StructType : PythonType, INativeType {
3333
internal Field[] _fields;
3434
private int? _size, _alignment, _pack;
35-
private static readonly Field[] _emptyFields = new Field[0]; // fields were never initialized before a type was created
35+
private static readonly Field[] _emptyFields = System.Array.Empty<Field>(); // fields were never initialized before a type was created
3636

3737
public StructType(CodeContext/*!*/ context, string name, PythonTuple bases, PythonDictionary members)
3838
: base(context, name, bases, members) {

Src/IronPython.Modules/_ctypes/_ctypes.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ public static object call_cdeclfunction(CodeContext context, IntPtr address, Pyt
380380

381381
_CFuncPtr func = (_CFuncPtr)funcType.CreateInstance(context, address);
382382

383-
return PythonOps.CallWithArgsTuple(func, new object[0], args);
383+
return PythonOps.CallWithArgsTuple(func, System.Array.Empty<object>(), args);
384384
}
385385

386386
public static void call_commethod() {
@@ -399,7 +399,7 @@ public static object call_function(CodeContext context, IntPtr address, PythonTu
399399

400400
_CFuncPtr func = (_CFuncPtr)funcType.CreateInstance(context, address);
401401

402-
return PythonOps.CallWithArgsTuple(func, new object[0], args);
402+
return PythonOps.CallWithArgsTuple(func, System.Array.Empty<object>(), args);
403403
}
404404

405405
private static CFuncPtrType GetFunctionType(CodeContext context, int flags) {
@@ -513,7 +513,7 @@ private static ModuleBuilder DynamicModule {
513513
lock (_lock) {
514514
if (_dynamicModule == null) {
515515
var attributes = new[] {
516-
new CustomAttributeBuilder(typeof(UnverifiableCodeAttribute).GetConstructor(ReflectionUtils.EmptyTypes), new object[0]),
516+
new CustomAttributeBuilder(typeof(UnverifiableCodeAttribute).GetConstructor(ReflectionUtils.EmptyTypes), System.Array.Empty<object>()),
517517
#if !NETCOREAPP && !NETSTANDARD
518518
//PermissionSet(SecurityAction.Demand, Unrestricted = true)
519519
new CustomAttributeBuilder(typeof(PermissionSetAttribute).GetConstructor(new Type[] { typeof(SecurityAction) }),

Src/IronPython.Modules/hashlib/_hashlib.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public abstract class HashBase<T> : ICloneable where T : HashAlgorithm {
2323
protected T _hasher;
2424
private static MethodInfo _memberwiseClone;
2525

26-
private static readonly byte[] _empty = new byte[0];
26+
private static readonly byte[] _empty = Array.Empty<byte>();
2727

2828
public readonly string name;
2929
public readonly int block_size;
@@ -87,7 +87,7 @@ protected T CloneHasher() {
8787

8888
if (_memberwiseClone != null) {
8989
lock (_hasher) {
90-
clone = (T)_memberwiseClone.Invoke(_hasher, new object[0]);
90+
clone = (T)_memberwiseClone.Invoke(_hasher, Array.Empty<object>());
9191
}
9292
}
9393

Src/IronPython.SQLite/Cursor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ private object fetchOneRow(CodeContext context)
354354

355355
case Sqlite3.SQLITE_BLOB:
356356
default:
357-
converted = new Bytes(Sqlite3.sqlite3_column_blob(this.statement.st, i) ?? new byte[0]); // TODO: avoid creating a copy
357+
converted = new Bytes(Sqlite3.sqlite3_column_blob(this.statement.st, i) ?? Array.Empty<byte>()); // TODO: avoid creating a copy
358358
break;
359359
}
360360
}

Src/IronPython.SQLite/c#sqlite/vdbeaux_c.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1646,7 +1646,7 @@ Vdbe p /* The VDBE */
16461646
int i; /* Loop counter */
16471647
int rc = SQLITE_OK; /* Return code */
16481648
if ( p.pResultSet == null )
1649-
p.pResultSet = new Mem[0];//Mem* pMem = p.pResultSet = p.aMem[1]; /* First Mem of result set */
1649+
p.pResultSet = Array.Empty<Mem>();//Mem* pMem = p.pResultSet = p.aMem[1]; /* First Mem of result set */
16501650
Mem pMem;
16511651
Debug.Assert( p.explain != 0 );
16521652
Debug.Assert( p.magic == VDBE_MAGIC_RUN );

Src/IronPython/Compiler/Ast/Node.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace IronPython.Compiler.Ast {
3030

3131
public abstract class Node : MSAst.Expression {
3232
internal static readonly BlockExpression EmptyBlock = Ast.Block(AstUtils.Empty());
33-
internal static readonly MSAst.Expression[] EmptyExpression = new MSAst.Expression[0];
33+
internal static readonly MSAst.Expression[] EmptyExpression = Array.Empty<Ast>();
3434

3535
internal static ParameterExpression FunctionStackVariable = Ast.Variable(typeof(List<FunctionStack>), "$funcStack");
3636
internal static readonly LabelTarget GeneratorLabel = Ast.Label(typeof(object), "$generatorLabel");

Src/IronPython/Compiler/Ast/TryStatement.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace IronPython.Compiler.Ast {
2727
public class TryStatement : Statement {
2828
private readonly TryStatementHandler[] _handlers;
2929

30-
private static readonly TryStatementHandler[] emptyArray = new TryStatementHandler[0];
30+
private static readonly TryStatementHandler[] emptyArray = Array.Empty<TryStatementHandler>();
3131

3232
public TryStatement(Statement body, TryStatementHandler[]? handlers, Statement? else_, Statement? finally_) {
3333
Body = body;

Src/IronPython/Compiler/GeneratorRewriter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ protected override Expression VisitTry(TryExpression node) {
411411
block[1] = Expression.MakeTry(null, @try, null, null, new ReadOnlyCollection<CatchBlock>(handlers));
412412
@try = Expression.Block(block);
413413
Debug.Assert(@try.Type == node.Body.Type);
414-
handlers = new CatchBlock[0]; // so we don't reuse these
414+
handlers = Array.Empty<CatchBlock>(); // so we don't reuse these
415415
}
416416

417417
if (finallyYields != catchYields) {
@@ -433,7 +433,7 @@ protected override Expression VisitTry(TryExpression node) {
433433
if (handlers.Count > 0) {
434434
@try = Expression.MakeTry(null, @try, null, null, handlers);
435435
Debug.Assert(@try.Type == node.Body.Type);
436-
handlers = new CatchBlock[0];
436+
handlers = Array.Empty<CatchBlock>();
437437
}
438438

439439
// NOTE: the order of these routers is important

Src/IronPython/Modules/_ast.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ internal static PythonAst ConvertToPythonAst(CodeContext codeContext, AST source
6868
} else
6969
throw PythonOps.TypeError("unsupported type of AST: {0}", (source.GetType()));
7070

71-
return new PythonAst(stmt, false, ModuleOptions.ExecOrEvalCode, printExpression, compilerContext, new int[] { });
71+
return new PythonAst(stmt, false, ModuleOptions.ExecOrEvalCode, printExpression, compilerContext, Array.Empty<int>());
7272
}
7373

7474
internal static AST BuildAst(CodeContext context, SourceUnit sourceUnit, PythonCompilerOptions opts, string mode) {

0 commit comments

Comments
 (0)