Commit db391eb9 authored by Marc Gravell's avatar Marc Gravell

fix CheckDatabaseMethodsUseKeys (false positive); remove all the `new...

fix CheckDatabaseMethodsUseKeys (false positive); remove all the `new SomeType[0]` usages (static and local)
parent 889a3bb4
...@@ -98,6 +98,7 @@ public void CheckDatabaseMethodsUseKeys(Type type) ...@@ -98,6 +98,7 @@ public void CheckDatabaseMethodsUseKeys(Type type)
private static bool UsesKey(Type type) private static bool UsesKey(Type type)
{ {
if (type == typeof(RedisKey)) return true; if (type == typeof(RedisKey)) return true;
if (type == typeof(StreamIdPair)) return true;
if (type.IsArray) if (type.IsArray)
{ {
......
...@@ -269,10 +269,6 @@ public sealed class ClusterNode : IEquatable<ClusterNode>, IComparable<ClusterN ...@@ -269,10 +269,6 @@ public sealed class ClusterNode : IEquatable<ClusterNode>, IComparable<ClusterN
{ {
private static readonly ClusterNode Dummy = new ClusterNode(); private static readonly ClusterNode Dummy = new ClusterNode();
private static readonly IList<ClusterNode> NoNodes = new ClusterNode[0];
private static readonly IList<SlotRange> NoSlots = new SlotRange[0];
private readonly ClusterConfiguration configuration; private readonly ClusterConfiguration configuration;
private IList<ClusterNode> children; private IList<ClusterNode> children;
...@@ -322,7 +318,7 @@ internal ClusterNode(ClusterConfiguration configuration, string raw, EndPoint or ...@@ -322,7 +318,7 @@ internal ClusterNode(ClusterConfiguration configuration, string raw, EndPoint or
(slots ?? (slots = new List<SlotRange>(parts.Length - i))).Add(range); (slots ?? (slots = new List<SlotRange>(parts.Length - i))).Add(range);
} }
} }
Slots = slots?.AsReadOnly() ?? NoSlots; Slots = slots?.AsReadOnly() ?? (IList<SlotRange>)Array.Empty<SlotRange>();
IsConnected = parts[7] == "connected"; // Can be "connected" or "disconnected" IsConnected = parts[7] == "connected"; // Can be "connected" or "disconnected"
} }
/// <summary> /// <summary>
...@@ -342,7 +338,7 @@ public IList<ClusterNode> Children ...@@ -342,7 +338,7 @@ public IList<ClusterNode> Children
(nodes ?? (nodes = new List<ClusterNode>())).Add(node); (nodes ?? (nodes = new List<ClusterNode>())).Add(node);
} }
} }
children = nodes?.AsReadOnly() ?? NoNodes; children = nodes?.AsReadOnly() ?? (IList<ClusterNode>)Array.Empty<ClusterNode>();
return children; return children;
} }
} }
......
...@@ -851,10 +851,7 @@ private static ConnectionMultiplexer ConnectImpl(Func<ConnectionMultiplexer> mul ...@@ -851,10 +851,7 @@ private static ConnectionMultiplexer ConnectImpl(Func<ConnectionMultiplexer> mul
private string failureMessage; private string failureMessage;
private readonly Hashtable servers = new Hashtable(); private readonly Hashtable servers = new Hashtable();
private volatile ServerEndPoint[] serverSnapshot = NilServers; private volatile ServerEndPoint[] serverSnapshot = Array.Empty<ServerEndPoint>();
private static readonly ServerEndPoint[] NilServers = new ServerEndPoint[0];
internal ServerEndPoint GetServerEndPoint(EndPoint endpoint) internal ServerEndPoint GetServerEndPoint(EndPoint endpoint)
{ {
if (endpoint == null) return null; if (endpoint == null) return null;
......
...@@ -122,7 +122,6 @@ public static class ExtensionMethods ...@@ -122,7 +122,6 @@ public static class ExtensionMethods
return result; return result;
} }
private static readonly RedisValue[] nixValues = new RedisValue[0];
/// <summary> /// <summary>
/// Create an array of RedisValues from an array of strings. /// Create an array of RedisValues from an array of strings.
/// </summary> /// </summary>
...@@ -130,11 +129,10 @@ public static class ExtensionMethods ...@@ -130,11 +129,10 @@ public static class ExtensionMethods
public static RedisValue[] ToRedisValueArray(this string[] values) public static RedisValue[] ToRedisValueArray(this string[] values)
{ {
if (values == null) return null; if (values == null) return null;
if (values.Length == 0) return nixValues; if (values.Length == 0) return Array.Empty<RedisValue>();
return Array.ConvertAll(values, x => (RedisValue)x); return Array.ConvertAll(values, x => (RedisValue)x);
} }
private static readonly string[] nix = new string[0];
/// <summary> /// <summary>
/// Create an array of strings from an array of values /// Create an array of strings from an array of values
/// </summary> /// </summary>
...@@ -142,7 +140,7 @@ public static RedisValue[] ToRedisValueArray(this string[] values) ...@@ -142,7 +140,7 @@ public static RedisValue[] ToRedisValueArray(this string[] values)
public static string[] ToStringArray(this RedisValue[] values) public static string[] ToStringArray(this RedisValue[] values)
{ {
if (values == null) return null; if (values == null) return null;
if (values.Length == 0) return nix; if (values.Length == 0) return Array.Empty<string>();
return Array.ConvertAll(values, x => (string)x); return Array.ConvertAll(values, x => (string)x);
} }
......
...@@ -46,7 +46,6 @@ internal override void WriteImpl(PhysicalConnection physical) ...@@ -46,7 +46,6 @@ internal override void WriteImpl(PhysicalConnection physical)
internal abstract class Message : ICompletable internal abstract class Message : ICompletable
{ {
public static readonly Message[] EmptyArray = new Message[0];
public readonly int Db; public readonly int Db;
internal const CommandFlags InternalCallFlag = (CommandFlags)128; internal const CommandFlags InternalCallFlag = (CommandFlags)128;
......
...@@ -212,7 +212,7 @@ internal RedisKey[] GetItemsAsKeys() ...@@ -212,7 +212,7 @@ internal RedisKey[] GetItemsAsKeys()
} }
else if (items.Length == 0) else if (items.Length == 0)
{ {
return RedisKey.EmptyArray; return Array.Empty<RedisKey>();
} }
else else
{ {
...@@ -246,8 +246,6 @@ internal RedisValue[] GetItemsAsValues() ...@@ -246,8 +246,6 @@ internal RedisValue[] GetItemsAsValues()
return arr; return arr;
} }
} }
private static readonly string[] NilStrings = new string[0];
internal string[] GetItemsAsStrings() internal string[] GetItemsAsStrings()
{ {
var items = GetItems(); var items = GetItems();
...@@ -257,7 +255,7 @@ internal string[] GetItemsAsStrings() ...@@ -257,7 +255,7 @@ internal string[] GetItemsAsStrings()
} }
else if (items.Length == 0) else if (items.Length == 0)
{ {
return NilStrings; return Array.Empty<string>();
} }
else else
{ {
...@@ -295,7 +293,7 @@ internal string[] GetItemsAsStrings() ...@@ -295,7 +293,7 @@ internal string[] GetItemsAsStrings()
} }
else if (items.Length == 0) else if (items.Length == 0)
{ {
return new GeoPosition?[0]; return Array.Empty<GeoPosition?>();
} }
else else
{ {
......
...@@ -8,8 +8,6 @@ namespace StackExchange.Redis ...@@ -8,8 +8,6 @@ namespace StackExchange.Redis
/// </summary> /// </summary>
public struct RedisChannel : IEquatable<RedisChannel> public struct RedisChannel : IEquatable<RedisChannel>
{ {
internal static readonly RedisChannel[] EmptyArray = new RedisChannel[0];
internal readonly byte[] Value; internal readonly byte[] Value;
internal readonly bool IsPatternBased; internal readonly bool IsPatternBased;
......
...@@ -302,7 +302,7 @@ public RedisValue HashGet(RedisKey key, RedisValue hashField, CommandFlags flags ...@@ -302,7 +302,7 @@ public RedisValue HashGet(RedisKey key, RedisValue hashField, CommandFlags flags
public RedisValue[] HashGet(RedisKey key, RedisValue[] hashFields, CommandFlags flags = CommandFlags.None) public RedisValue[] HashGet(RedisKey key, RedisValue[] hashFields, CommandFlags flags = CommandFlags.None)
{ {
if (hashFields == null) throw new ArgumentNullException(nameof(hashFields)); if (hashFields == null) throw new ArgumentNullException(nameof(hashFields));
if (hashFields.Length == 0) return new RedisValue[0]; if (hashFields.Length == 0) return Array.Empty<RedisValue>();
var msg = Message.Create(Database, flags, RedisCommand.HMGET, key, hashFields); var msg = Message.Create(Database, flags, RedisCommand.HMGET, key, hashFields);
return ExecuteSync(msg, ResultProcessor.RedisValueArray); return ExecuteSync(msg, ResultProcessor.RedisValueArray);
} }
...@@ -2018,7 +2018,7 @@ public RedisValue StringGet(RedisKey key, CommandFlags flags = CommandFlags.None ...@@ -2018,7 +2018,7 @@ public RedisValue StringGet(RedisKey key, CommandFlags flags = CommandFlags.None
public RedisValue[] StringGet(RedisKey[] keys, CommandFlags flags = CommandFlags.None) public RedisValue[] StringGet(RedisKey[] keys, CommandFlags flags = CommandFlags.None)
{ {
if (keys == null) throw new ArgumentNullException(nameof(keys)); if (keys == null) throw new ArgumentNullException(nameof(keys));
if (keys.Length == 0) return new RedisValue[0]; if (keys.Length == 0) return Array.Empty<RedisValue>();
var msg = Message.Create(Database, flags, RedisCommand.MGET, keys); var msg = Message.Create(Database, flags, RedisCommand.MGET, keys);
return ExecuteSync(msg, ResultProcessor.RedisValueArray); return ExecuteSync(msg, ResultProcessor.RedisValueArray);
} }
...@@ -2032,7 +2032,7 @@ public Task<RedisValue> StringGetAsync(RedisKey key, CommandFlags flags = Comman ...@@ -2032,7 +2032,7 @@ public Task<RedisValue> StringGetAsync(RedisKey key, CommandFlags flags = Comman
public Task<RedisValue[]> StringGetAsync(RedisKey[] keys, CommandFlags flags = CommandFlags.None) public Task<RedisValue[]> StringGetAsync(RedisKey[] keys, CommandFlags flags = CommandFlags.None)
{ {
if (keys == null) throw new ArgumentNullException(nameof(keys)); if (keys == null) throw new ArgumentNullException(nameof(keys));
if (keys.Length == 0) return CompletedTask<RedisValue[]>.FromResult(new RedisValue[0], asyncState); if (keys.Length == 0) return CompletedTask<RedisValue[]>.FromResult(Array.Empty<RedisValue>(), asyncState);
var msg = Message.Create(Database, flags, RedisCommand.MGET, keys); var msg = Message.Create(Database, flags, RedisCommand.MGET, keys);
return ExecuteAsync(msg, ResultProcessor.RedisValueArray); return ExecuteAsync(msg, ResultProcessor.RedisValueArray);
} }
...@@ -3163,12 +3163,11 @@ protected override bool SetResultCore(PhysicalConnection connection, Message mes ...@@ -3163,12 +3163,11 @@ protected override bool SetResultCore(PhysicalConnection connection, Message mes
private sealed class ExecuteMessage : Message private sealed class ExecuteMessage : Message
{ {
private readonly string _command; private readonly string _command;
private static readonly object[] NoArgs = new object[0];
private readonly ICollection<object> args; private readonly ICollection<object> args;
public ExecuteMessage(int db, CommandFlags flags, string command, ICollection<object> args) : base(db, flags, RedisCommand.UNKNOWN) public ExecuteMessage(int db, CommandFlags flags, string command, ICollection<object> args) : base(db, flags, RedisCommand.UNKNOWN)
{ {
_command = command; _command = command;
this.args = args ?? NoArgs; this.args = args ?? Array.Empty<object>();
} }
internal override void WriteImpl(PhysicalConnection physical) internal override void WriteImpl(PhysicalConnection physical)
...@@ -3234,8 +3233,8 @@ private ScriptEvalMessage(int db, CommandFlags flags, RedisCommand command, stri ...@@ -3234,8 +3233,8 @@ private ScriptEvalMessage(int db, CommandFlags flags, RedisCommand command, stri
this.script = script; this.script = script;
this.hexHash = hexHash; this.hexHash = hexHash;
if (keys == null) keys = RedisKey.EmptyArray; if (keys == null) keys = Array.Empty<RedisKey>();
if (values == null) values = RedisValue.EmptyArray; if (values == null) values = Array.Empty<RedisValue>();
for (int i = 0; i < keys.Length; i++) for (int i = 0; i < keys.Length; i++)
keys[i].AssertNotNull(); keys[i].AssertNotNull();
this.keys = keys; this.keys = keys;
......
...@@ -8,7 +8,6 @@ namespace StackExchange.Redis ...@@ -8,7 +8,6 @@ namespace StackExchange.Redis
/// </summary> /// </summary>
public struct RedisKey : IEquatable<RedisKey> public struct RedisKey : IEquatable<RedisKey>
{ {
internal static readonly RedisKey[] EmptyArray = new RedisKey[0];
private readonly byte[] keyPrefix; private readonly byte[] keyPrefix;
private readonly object keyValue; // always either a string or a byte[] private readonly object keyValue; // always either a string or a byte[]
internal RedisKey(byte[] keyPrefix, object keyValue) internal RedisKey(byte[] keyPrefix, object keyValue)
......
...@@ -173,16 +173,14 @@ protected override bool SetResultCore(PhysicalConnection connection, Message mes ...@@ -173,16 +173,14 @@ protected override bool SetResultCore(PhysicalConnection connection, Message mes
private class TransactionMessage : Message, IMultiMessage private class TransactionMessage : Message, IMultiMessage
{ {
private static readonly ConditionResult[] NixConditions = new ConditionResult[0];
private static readonly QueuedMessage[] NixMessages = new QueuedMessage[0];
private readonly ConditionResult[] conditions; private readonly ConditionResult[] conditions;
public QueuedMessage[] InnerOperations { get; } public QueuedMessage[] InnerOperations { get; }
public TransactionMessage(int db, CommandFlags flags, List<ConditionResult> conditions, List<QueuedMessage> operations) public TransactionMessage(int db, CommandFlags flags, List<ConditionResult> conditions, List<QueuedMessage> operations)
: base(db, flags, RedisCommand.EXEC) : base(db, flags, RedisCommand.EXEC)
{ {
this.InnerOperations = (operations == null || operations.Count == 0) ? NixMessages : operations.ToArray(); this.InnerOperations = (operations == null || operations.Count == 0) ? Array.Empty<QueuedMessage>() : operations.ToArray();
this.conditions = (conditions == null || conditions.Count == 0) ? NixConditions : conditions.ToArray(); this.conditions = (conditions == null || conditions.Count == 0) ? Array.Empty<ConditionResult>(): conditions.ToArray();
} }
public bool IsAborted => command != RedisCommand.EXEC; public bool IsAborted => command != RedisCommand.EXEC;
......
...@@ -506,8 +506,6 @@ protected override HashEntry Parse(RawResult first, RawResult second) ...@@ -506,8 +506,6 @@ protected override HashEntry Parse(RawResult first, RawResult second)
internal abstract class ValuePairInterleavedProcessorBase<T> : ResultProcessor<T[]> internal abstract class ValuePairInterleavedProcessorBase<T> : ResultProcessor<T[]>
{ {
private static readonly T[] nix = new T[0];
public bool TryParse(RawResult result, out T[] pairs) public bool TryParse(RawResult result, out T[] pairs)
{ {
switch (result.Type) switch (result.Type)
...@@ -523,7 +521,7 @@ public bool TryParse(RawResult result, out T[] pairs) ...@@ -523,7 +521,7 @@ public bool TryParse(RawResult result, out T[] pairs)
int count = arr.Length / 2; int count = arr.Length / 2;
if (count == 0) if (count == 0)
{ {
pairs = nix; pairs = Array.Empty<T>();
} }
else else
{ {
...@@ -1069,7 +1067,7 @@ protected override bool SetResultCore(PhysicalConnection connection, Message mes ...@@ -1069,7 +1067,7 @@ protected override bool SetResultCore(PhysicalConnection connection, Message mes
RedisChannel[] final; RedisChannel[] final;
if (arr.Length == 0) if (arr.Length == 0)
{ {
final = RedisChannel.EmptyArray; final = Array.Empty<RedisChannel>();
} }
else else
{ {
...@@ -1347,7 +1345,7 @@ protected override bool SetResultCore(PhysicalConnection connection, Message mes ...@@ -1347,7 +1345,7 @@ protected override bool SetResultCore(PhysicalConnection connection, Message mes
if (result.IsNull) if (result.IsNull)
{ {
// Server returns 'nil' if no entries are returned for the given stream. // Server returns 'nil' if no entries are returned for the given stream.
SetResult(message, new RedisStreamEntry[0]); SetResult(message, Array.Empty<RedisStreamEntry>());
return true; return true;
} }
...@@ -1647,7 +1645,7 @@ protected override bool SetResultCore(PhysicalConnection connection, Message mes ...@@ -1647,7 +1645,7 @@ protected override bool SetResultCore(PhysicalConnection connection, Message mes
var pendingInfo = new StreamPendingInfo(pendingMessageCount: (int)arr[0].AsRedisValue(), var pendingInfo = new StreamPendingInfo(pendingMessageCount: (int)arr[0].AsRedisValue(),
lowestId: arr[1].AsRedisValue(), lowestId: arr[1].AsRedisValue(),
highestId: arr[2].AsRedisValue(), highestId: arr[2].AsRedisValue(),
consumers: consumers ?? new StreamConsumer[0]); consumers: consumers ?? Array.Empty<StreamConsumer>());
// ^^^^^ // ^^^^^
// Should we bother allocating an empty array only to prevent the need for a null check? // Should we bother allocating an empty array only to prevent the need for a null check?
...@@ -1744,10 +1742,7 @@ protected NameValueEntry[] ParseStreamEntryValues(RawResult result) ...@@ -1744,10 +1742,7 @@ protected NameValueEntry[] ParseStreamEntryValues(RawResult result)
// Calculate how many name/value pairs are in the stream entry. // Calculate how many name/value pairs are in the stream entry.
int count = arr.Length / 2; int count = arr.Length / 2;
if (count == 0) if (count == 0) return Array.Empty<NameValueEntry>();
{
return new NameValueEntry[0];
}
var pairs = new NameValueEntry[count]; var pairs = new NameValueEntry[count];
int offset = 0; int offset = 0;
......
...@@ -24,9 +24,8 @@ internal enum UnselectableFlags ...@@ -24,9 +24,8 @@ internal enum UnselectableFlags
internal sealed partial class ServerEndPoint : IDisposable internal sealed partial class ServerEndPoint : IDisposable
{ {
internal volatile ServerEndPoint Master; internal volatile ServerEndPoint Master;
internal volatile ServerEndPoint[] Slaves = NoSlaves; internal volatile ServerEndPoint[] Slaves = Array.Empty<ServerEndPoint>();
private static readonly Regex nameSanitizer = new Regex("[^!-~]", RegexOptions.Compiled); private static readonly Regex nameSanitizer = new Regex("[^!-~]", RegexOptions.Compiled);
private static readonly ServerEndPoint[] NoSlaves = new ServerEndPoint[0];
private readonly Hashtable knownScripts = new Hashtable(StringComparer.Ordinal); private readonly Hashtable knownScripts = new Hashtable(StringComparer.Ordinal);
...@@ -215,7 +214,7 @@ public void SetClusterConfiguration(ClusterConfiguration configuration) ...@@ -215,7 +214,7 @@ public void SetClusterConfiguration(ClusterConfiguration configuration)
} }
} }
Master = master; Master = master;
Slaves = slaves?.ToArray() ?? NoSlaves; Slaves = slaves?.ToArray() ?? Array.Empty<ServerEndPoint>();
} }
Multiplexer.Trace("Cluster configured"); Multiplexer.Trace("Cluster configured");
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment