Skip to content

Commit d813198

Browse files
authored
Use Collections.EmptyList where applicable (#4762)
Signed-off-by: Corniel Nobel <[email protected]>
1 parent 6722835 commit d813198

File tree

7 files changed

+19
-11
lines changed

7 files changed

+19
-11
lines changed

runtime/CSharp/src/Atn/ATN.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ public class ATN
7777
private readonly PredictionContextCache contextCache = new PredictionContextCache();
7878

7979
[NotNull]
80-
public DFA[] decisionToDFA = new DFA[0];
80+
public DFA[] decisionToDFA = Collections.EmptyList<DFA>();
8181

8282
[NotNull]
83-
public DFA[] modeToDFA = new DFA[0];
83+
public DFA[] modeToDFA = Collections.EmptyList<DFA>();
8484

8585
protected internal readonly ConcurrentDictionary<int, int> LL1Table = new ConcurrentDictionary<int, int>();
8686

runtime/CSharp/src/Dfa/DFA.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public DFA(DecisionState atnStartState, int decision)
4646
{
4747
this.precedenceDfa = true;
4848
DFAState precedenceState = new DFAState(new ATNConfigSet());
49-
precedenceState.edges = new DFAState[0];
49+
precedenceState.edges = Collections.EmptyList<DFAState>();
5050
precedenceState.isAcceptState = false;
5151
precedenceState.requiresFullContext = false;
5252
this.s0 = precedenceState;

runtime/CSharp/src/LexerInterpreter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class LexerInterpreter: Lexer
3333

3434
[Obsolete("Use constructor with channelNames argument")]
3535
public LexerInterpreter(string grammarFileName, IVocabulary vocabulary, IEnumerable<string> ruleNames, IEnumerable<string> modeNames, ATN atn, ICharStream input)
36-
: this(grammarFileName, vocabulary, ruleNames, new string[0], modeNames, atn, input)
36+
: this(grammarFileName, vocabulary, ruleNames, Collections.EmptyList<string>(), modeNames, atn, input)
3737
{
3838
}
3939

runtime/CSharp/src/Recognizer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Runtime.CompilerServices;
88
using Antlr4.Runtime.Atn;
99
using Antlr4.Runtime.Misc;
10+
using Antlr4.Runtime.Sharpen;
1011

1112
namespace Antlr4.Runtime
1213
{
@@ -286,7 +287,7 @@ public virtual void RemoveErrorListener(IAntlrErrorListener<Symbol> listener)
286287

287288
public virtual void RemoveErrorListeners()
288289
{
289-
_listeners = new IAntlrErrorListener<Symbol>[0];
290+
_listeners = Collections.EmptyList<IAntlrErrorListener<Symbol>>();
290291
}
291292

292293
[NotNull]

runtime/CSharp/src/Sharpen/BitSet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Antlr4.Runtime.Sharpen
99

1010
public class BitSet
1111
{
12-
private static readonly ulong[] EmptyBits = new ulong[0];
12+
private static readonly ulong[] EmptyBits = Collections.EmptyList<ulong>();
1313
private const int BitsPerElement = 8 * sizeof(ulong);
1414

1515
private ulong[] _data = EmptyBits;

runtime/CSharp/src/Sharpen/Collections.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ namespace Antlr4.Runtime.Sharpen
99

1010
internal static class Collections
1111
{
12-
public static T[] EmptyList<T>()
12+
/// <remarks>
13+
/// Available in .NET as Array.Empty but not to the net45 target.
14+
/// See: https://learn.microsoft.com/dotnet/api/system.array.empty.
15+
/// </remarks>
16+
public static T[] EmptyList<T>()
1317
{
1418
return EmptyListImpl<T>.Instance;
1519
}
@@ -31,10 +35,13 @@ public static ReadOnlyDictionary<TKey, TValue> SingletonMap<TKey, TValue>(TKey k
3135

3236
private static class EmptyListImpl<T>
3337
{
34-
public static readonly T[] Instance = new T[0];
35-
}
38+
#pragma warning disable CA1825
39+
// Provides a solution for CA1825.
40+
public static readonly T[] Instance = new T[0];
41+
#pragma warning restore CA1825
42+
}
3643

37-
private static class EmptyMapImpl<TKey, TValue>
44+
private static class EmptyMapImpl<TKey, TValue>
3845
{
3946
public static readonly ReadOnlyDictionary<TKey, TValue> Instance =
4047
new ReadOnlyDictionary<TKey, TValue>(new Dictionary<TKey, TValue>());

runtime/CSharp/src/Vocabulary.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace Antlr4.Runtime
1616
/// <author>Sam Harwell</author>
1717
public class Vocabulary : IVocabulary
1818
{
19-
private static readonly string[] EmptyNames = new string[0];
19+
private static readonly string[] EmptyNames = Collections.EmptyList<string>();
2020

2121
/// <summary>
2222
/// Gets an empty

0 commit comments

Comments
 (0)