From b5d0bf08507f4f00aea543268424b98a6f19db6a Mon Sep 17 00:00:00 2001 From: Corniel Nobel Date: Fri, 24 Jan 2025 10:53:54 +0100 Subject: [PATCH] Use Collections.EmptyList where applicable Signed-off-by: Corniel Nobel --- runtime/CSharp/src/Atn/ATN.cs | 4 ++-- runtime/CSharp/src/Dfa/DFA.cs | 2 +- runtime/CSharp/src/LexerInterpreter.cs | 2 +- runtime/CSharp/src/Recognizer.cs | 3 ++- runtime/CSharp/src/Sharpen/BitSet.cs | 2 +- runtime/CSharp/src/Sharpen/Collections.cs | 15 +++++++++++---- runtime/CSharp/src/Vocabulary.cs | 2 +- 7 files changed, 19 insertions(+), 11 deletions(-) diff --git a/runtime/CSharp/src/Atn/ATN.cs b/runtime/CSharp/src/Atn/ATN.cs index a8044ca2cf..966ded44b4 100644 --- a/runtime/CSharp/src/Atn/ATN.cs +++ b/runtime/CSharp/src/Atn/ATN.cs @@ -77,10 +77,10 @@ public class ATN private readonly PredictionContextCache contextCache = new PredictionContextCache(); [NotNull] - public DFA[] decisionToDFA = new DFA[0]; + public DFA[] decisionToDFA = Collections.EmptyList(); [NotNull] - public DFA[] modeToDFA = new DFA[0]; + public DFA[] modeToDFA = Collections.EmptyList(); protected internal readonly ConcurrentDictionary LL1Table = new ConcurrentDictionary(); diff --git a/runtime/CSharp/src/Dfa/DFA.cs b/runtime/CSharp/src/Dfa/DFA.cs index de3ca19ac1..18be55a703 100644 --- a/runtime/CSharp/src/Dfa/DFA.cs +++ b/runtime/CSharp/src/Dfa/DFA.cs @@ -46,7 +46,7 @@ public DFA(DecisionState atnStartState, int decision) { this.precedenceDfa = true; DFAState precedenceState = new DFAState(new ATNConfigSet()); - precedenceState.edges = new DFAState[0]; + precedenceState.edges = Collections.EmptyList(); precedenceState.isAcceptState = false; precedenceState.requiresFullContext = false; this.s0 = precedenceState; diff --git a/runtime/CSharp/src/LexerInterpreter.cs b/runtime/CSharp/src/LexerInterpreter.cs index edf0c4e6f9..6739eacc55 100644 --- a/runtime/CSharp/src/LexerInterpreter.cs +++ b/runtime/CSharp/src/LexerInterpreter.cs @@ -33,7 +33,7 @@ public class LexerInterpreter: Lexer [Obsolete("Use constructor with channelNames argument")] public LexerInterpreter(string grammarFileName, IVocabulary vocabulary, IEnumerable ruleNames, IEnumerable modeNames, ATN atn, ICharStream input) - : this(grammarFileName, vocabulary, ruleNames, new string[0], modeNames, atn, input) + : this(grammarFileName, vocabulary, ruleNames, Collections.EmptyList(), modeNames, atn, input) { } diff --git a/runtime/CSharp/src/Recognizer.cs b/runtime/CSharp/src/Recognizer.cs index 0ab2894551..3abbabccc1 100644 --- a/runtime/CSharp/src/Recognizer.cs +++ b/runtime/CSharp/src/Recognizer.cs @@ -7,6 +7,7 @@ using System.Runtime.CompilerServices; using Antlr4.Runtime.Atn; using Antlr4.Runtime.Misc; +using Antlr4.Runtime.Sharpen; namespace Antlr4.Runtime { @@ -286,7 +287,7 @@ public virtual void RemoveErrorListener(IAntlrErrorListener listener) public virtual void RemoveErrorListeners() { - _listeners = new IAntlrErrorListener[0]; + _listeners = Collections.EmptyList>(); } [NotNull] diff --git a/runtime/CSharp/src/Sharpen/BitSet.cs b/runtime/CSharp/src/Sharpen/BitSet.cs index 76751b41f4..59e59a9319 100644 --- a/runtime/CSharp/src/Sharpen/BitSet.cs +++ b/runtime/CSharp/src/Sharpen/BitSet.cs @@ -9,7 +9,7 @@ namespace Antlr4.Runtime.Sharpen public class BitSet { - private static readonly ulong[] EmptyBits = new ulong[0]; + private static readonly ulong[] EmptyBits = Collections.EmptyList(); private const int BitsPerElement = 8 * sizeof(ulong); private ulong[] _data = EmptyBits; diff --git a/runtime/CSharp/src/Sharpen/Collections.cs b/runtime/CSharp/src/Sharpen/Collections.cs index 786240722e..b9df0ba56c 100644 --- a/runtime/CSharp/src/Sharpen/Collections.cs +++ b/runtime/CSharp/src/Sharpen/Collections.cs @@ -9,7 +9,11 @@ namespace Antlr4.Runtime.Sharpen internal static class Collections { - public static T[] EmptyList() + /// + /// Available in .NET as Array.Empty but not to the net45 target. + /// See: https://learn.microsoft.com/dotnet/api/system.array.empty. + /// + public static T[] EmptyList() { return EmptyListImpl.Instance; } @@ -31,10 +35,13 @@ public static ReadOnlyDictionary SingletonMap(TKey k private static class EmptyListImpl { - public static readonly T[] Instance = new T[0]; - } +#pragma warning disable CA1825 + // Provides a solution for CA1825. + public static readonly T[] Instance = new T[0]; +#pragma warning restore CA1825 + } - private static class EmptyMapImpl + private static class EmptyMapImpl { public static readonly ReadOnlyDictionary Instance = new ReadOnlyDictionary(new Dictionary()); diff --git a/runtime/CSharp/src/Vocabulary.cs b/runtime/CSharp/src/Vocabulary.cs index 9b12932d2c..ee18daae90 100644 --- a/runtime/CSharp/src/Vocabulary.cs +++ b/runtime/CSharp/src/Vocabulary.cs @@ -16,7 +16,7 @@ namespace Antlr4.Runtime /// Sam Harwell public class Vocabulary : IVocabulary { - private static readonly string[] EmptyNames = new string[0]; + private static readonly string[] EmptyNames = Collections.EmptyList(); /// /// Gets an empty