From e0498476f7d117e9c1430205ea0d3a57a0b7846e Mon Sep 17 00:00:00 2001 From: Luke Bakken Date: Mon, 13 May 2024 09:29:17 -0700 Subject: [PATCH] Various editor suggestions Part of #1413 * Prefer explicit types instead of `var` --- projects/RabbitMQ.Client.OAuth2/OAuth2Client.cs | 8 +++++--- .../client/impl/AutorecoveringConnection.Recording.cs | 8 ++++---- projects/RabbitMQ.Client/client/impl/ChannelBase.cs | 8 ++++---- .../RabbitMQ.Client/client/impl/Connection.Commands.cs | 6 +++++- .../impl/ConsumerDispatching/ConsumerDispatcherBase.cs | 4 ++-- .../RabbitMQ.Client/client/impl/WireFormatting.Read.cs | 10 +++++----- .../client/impl/WireFormatting.Write.cs | 2 +- 7 files changed, 26 insertions(+), 20 deletions(-) diff --git a/projects/RabbitMQ.Client.OAuth2/OAuth2Client.cs b/projects/RabbitMQ.Client.OAuth2/OAuth2Client.cs index ce93659a93..e6066b7d16 100644 --- a/projects/RabbitMQ.Client.OAuth2/OAuth2Client.cs +++ b/projects/RabbitMQ.Client.OAuth2/OAuth2Client.cs @@ -232,9 +232,11 @@ public void Dispose() private Dictionary buildRequestParameters() { - var dict = new Dictionary(_additionalRequestParameters); - dict.Add(CLIENT_ID, _clientId); - dict.Add(CLIENT_SECRET, _clientSecret); + var dict = new Dictionary(_additionalRequestParameters) + { + { CLIENT_ID, _clientId }, + { CLIENT_SECRET, _clientSecret } + }; if (_scope != null && _scope.Length > 0) { dict.Add(SCOPE, _scope); diff --git a/projects/RabbitMQ.Client/client/impl/AutorecoveringConnection.Recording.cs b/projects/RabbitMQ.Client/client/impl/AutorecoveringConnection.Recording.cs index ac5c453bff..ca08d4c027 100644 --- a/projects/RabbitMQ.Client/client/impl/AutorecoveringConnection.Recording.cs +++ b/projects/RabbitMQ.Client/client/impl/AutorecoveringConnection.Recording.cs @@ -170,7 +170,7 @@ private void DoDeleteAutoDeleteExchange(string exchangeName) bool AnyBindingsOnExchange(string exchange) { - foreach (var recordedBinding in _recordedBindings) + foreach (RecordedBinding recordedBinding in _recordedBindings) { if (recordedBinding.Source == exchange) { @@ -400,7 +400,7 @@ await _recordedEntitiesSemaphore.WaitAsync() private void DoDeleteRecordedConsumer(string consumerTag) { - if (_recordedConsumers.Remove(consumerTag, out var recordedConsumer)) + if (_recordedConsumers.Remove(consumerTag, out RecordedConsumer recordedConsumer)) { DeleteAutoDeleteQueue(recordedConsumer.Queue); } @@ -408,7 +408,7 @@ private void DoDeleteRecordedConsumer(string consumerTag) private void DeleteAutoDeleteQueue(string queue) { - if (_recordedQueues.TryGetValue(queue, out var recordedQueue) && recordedQueue.AutoDelete) + if (_recordedQueues.TryGetValue(queue, out RecordedQueue recordedQueue) && recordedQueue.AutoDelete) { // last consumer on this connection is gone, remove recorded queue if it is auto-deleted. if (!AnyConsumersOnQueue(queue)) @@ -420,7 +420,7 @@ private void DeleteAutoDeleteQueue(string queue) private bool AnyConsumersOnQueue(string queue) { - foreach (var pair in _recordedConsumers) + foreach (KeyValuePair pair in _recordedConsumers) { if (pair.Value.Queue == queue) { diff --git a/projects/RabbitMQ.Client/client/impl/ChannelBase.cs b/projects/RabbitMQ.Client/client/impl/ChannelBase.cs index fd6f4d4e71..fa4f4d2f50 100644 --- a/projects/RabbitMQ.Client/client/impl/ChannelBase.cs +++ b/projects/RabbitMQ.Client/client/impl/ChannelBase.cs @@ -488,7 +488,7 @@ private void OnChannelShutdown(ShutdownEventArgs reason) if (_confirmsTaskCompletionSources?.Count > 0) { var exception = new AlreadyClosedException(reason); - foreach (var confirmsTaskCompletionSource in _confirmsTaskCompletionSources) + foreach (TaskCompletionSource confirmsTaskCompletionSource in _confirmsTaskCompletionSources) { confirmsTaskCompletionSource.TrySetException(exception); } @@ -635,7 +635,7 @@ protected void HandleAckNack(ulong deliveryTag, bool multiple, bool isNack) if (_pendingDeliveryTags.Count == 0 && _confirmsTaskCompletionSources.Count > 0) { // Done, mark tasks - foreach (var confirmsTaskCompletionSource in _confirmsTaskCompletionSources) + foreach (TaskCompletionSource confirmsTaskCompletionSource in _confirmsTaskCompletionSources) { confirmsTaskCompletionSource.TrySetResult(_onlyAcksReceived); } @@ -754,7 +754,7 @@ protected async Task HandleChannelCloseOkAsync(IncomingCommand cmd, Cancel */ FinishClose(); - if (_continuationQueue.TryPeek(out var k)) + if (_continuationQueue.TryPeek(out ChannelCloseAsyncRpcContinuation k)) { _continuationQueue.Next(); await k.HandleCommandAsync(cmd) @@ -1905,7 +1905,7 @@ await tokenRegistration.DisposeAsync() props = new BasicProperties(); } - var headers = props.Headers ?? new Dictionary(); + IDictionary headers = props.Headers ?? new Dictionary(); // Inject the ActivityContext into the message headers to propagate trace context to the receiving service. DistributedContextPropagator.Current.Inject(sendActivity, headers, InjectTraceContextIntoBasicProperties); diff --git a/projects/RabbitMQ.Client/client/impl/Connection.Commands.cs b/projects/RabbitMQ.Client/client/impl/Connection.Commands.cs index ef1c9d03c1..c0ab30ac65 100644 --- a/projects/RabbitMQ.Client/client/impl/Connection.Commands.cs +++ b/projects/RabbitMQ.Client/client/impl/Connection.Commands.cs @@ -212,9 +212,13 @@ await UpdateSecretAsync(_config.CredentialsProvider.Password, "Token refresh", c private IAuthMechanismFactory GetAuthMechanismFactory(string supportedMechanismNames) { // Our list is in order of preference, the server one is not. - foreach (var factory in _config.AuthMechanisms) + foreach (IAuthMechanismFactory factory in _config.AuthMechanisms) { +#if NET6_0_OR_GREATER + if (supportedMechanismNames.Contains(factory.Name, StringComparison.OrdinalIgnoreCase)) +#else if (supportedMechanismNames.IndexOf(factory.Name, StringComparison.OrdinalIgnoreCase) >= 0) +#endif { return factory; } diff --git a/projects/RabbitMQ.Client/client/impl/ConsumerDispatching/ConsumerDispatcherBase.cs b/projects/RabbitMQ.Client/client/impl/ConsumerDispatching/ConsumerDispatcherBase.cs index eb85a04e08..88072f4f2f 100644 --- a/projects/RabbitMQ.Client/client/impl/ConsumerDispatching/ConsumerDispatcherBase.cs +++ b/projects/RabbitMQ.Client/client/impl/ConsumerDispatching/ConsumerDispatcherBase.cs @@ -28,7 +28,7 @@ protected IBasicConsumer GetConsumerOrDefault(string tag) { lock (_consumers) { - return _consumers.TryGetValue(tag, out var consumer) ? consumer : GetDefaultOrFallbackConsumer(); + return _consumers.TryGetValue(tag, out IBasicConsumer? consumer) ? consumer : GetDefaultOrFallbackConsumer(); } } @@ -36,7 +36,7 @@ public IBasicConsumer GetAndRemoveConsumer(string tag) { lock (_consumers) { - return _consumers.Remove(tag, out var consumer) ? consumer : GetDefaultOrFallbackConsumer(); + return _consumers.Remove(tag, out IBasicConsumer? consumer) ? consumer : GetDefaultOrFallbackConsumer(); } } diff --git a/projects/RabbitMQ.Client/client/impl/WireFormatting.Read.cs b/projects/RabbitMQ.Client/client/impl/WireFormatting.Read.cs index cd5aef51d4..1149b97b97 100644 --- a/projects/RabbitMQ.Client/client/impl/WireFormatting.Read.cs +++ b/projects/RabbitMQ.Client/client/impl/WireFormatting.Read.cs @@ -78,7 +78,7 @@ public static object ReadFieldValue(ReadOnlySpan span, out int bytesRead) switch ((char)span[0]) { case 'S': - bytesRead = 1 + ReadLongstr(span.Slice(1), out var bytes); + bytesRead = 1 + ReadLongstr(span.Slice(1), out byte[] bytes); return bytes; case 't': bytesRead = 2; @@ -96,11 +96,11 @@ public static object ReadFieldValue(ReadOnlySpan span, out int bytesRead) // Moved out of outer switch to have a shorter main method (improves performance) static object ReadFieldValueSlow(ReadOnlySpan span, out int bytesRead) { - var slice = span.Slice(1); + ReadOnlySpan slice = span.Slice(1); switch ((char)span[0]) { case 'F': - bytesRead = 1 + ReadDictionary(slice, out var dictionary); + bytesRead = 1 + ReadDictionary(slice, out Dictionary dictionary); return dictionary; case 'A': IList arrayResult = ReadArray(slice, out int arrayBytesRead); @@ -134,10 +134,10 @@ static object ReadFieldValueSlow(ReadOnlySpan span, out int bytesRead) bytesRead = 3; return NetworkOrderDeserializer.ReadUInt16(slice); case 'T': - bytesRead = 1 + ReadTimestamp(slice, out var timestamp); + bytesRead = 1 + ReadTimestamp(slice, out AmqpTimestamp timestamp); return timestamp; case 'x': - bytesRead = 1 + ReadLongstr(slice, out var binaryTableResult); + bytesRead = 1 + ReadLongstr(slice, out byte[] binaryTableResult); return new BinaryTableValue(binaryTableResult); default: bytesRead = 0; diff --git a/projects/RabbitMQ.Client/client/impl/WireFormatting.Write.cs b/projects/RabbitMQ.Client/client/impl/WireFormatting.Write.cs index ab27c08049..d9a92f252b 100644 --- a/projects/RabbitMQ.Client/client/impl/WireFormatting.Write.cs +++ b/projects/RabbitMQ.Client/client/impl/WireFormatting.Write.cs @@ -326,7 +326,7 @@ public static int WriteShort(ref byte destination, ushort val) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int WriteShortstr(ref byte destination, ReadOnlySpan value) { - var length = value.Length; + int length = value.Length; if (length <= byte.MaxValue) { destination = (byte)length;