Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert null checks to modern ThrowIfNull checks #2089

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions Samples/Server/Server_Retained_Messages_Samples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,7 @@ sealed class MqttRetainedMessageModel

public static MqttRetainedMessageModel Create(MqttApplicationMessage message)
{
if (message == null)
{
throw new ArgumentNullException(nameof(message));
}
ArgumentNullException.ThrowIfNull(message);

return new MqttRetainedMessageModel
{
Expand Down
5 changes: 1 addition & 4 deletions Source/MQTTnet.AspnetCore/EndpointRouterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ public static class EndpointRouterExtensions
{
public static void MapMqtt(this IEndpointRouteBuilder endpoints, string pattern)
{
if (endpoints == null)
{
throw new ArgumentNullException(nameof(endpoints));
}
ArgumentNullException.ThrowIfNull(endpoints);

endpoints.MapConnectionHandler<MqttConnectionHandler>(pattern, options =>
{
Expand Down
10 changes: 2 additions & 8 deletions Source/MQTTnet.AspnetCore/MqttSubProtocolSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ public static class MqttSubProtocolSelector
{
public static string SelectSubProtocol(HttpRequest request)
{
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
ArgumentNullException.ThrowIfNull(request);

string subProtocol = null;
if (request.Headers.TryGetValue("Sec-WebSocket-Protocol", out var requestedSubProtocolValues))
Expand All @@ -29,10 +26,7 @@ public static string SelectSubProtocol(HttpRequest request)

public static string SelectSubProtocol(IList<string> requestedSubProtocolValues)
{
if (requestedSubProtocolValues == null)
{
throw new ArgumentNullException(nameof(requestedSubProtocolValues));
}
ArgumentNullException.ThrowIfNull(requestedSubProtocolValues);

// Order the protocols to also match "mqtt", "mqttv-3.1", "mqttv-3.11" etc.
return requestedSubProtocolValues.OrderByDescending(p => p.Length).FirstOrDefault(p => p.ToLower().StartsWith("mqtt"));
Expand Down
5 changes: 1 addition & 4 deletions Source/MQTTnet.AspnetCore/MqttWebSocketServerAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ public void Dispose()

public async Task RunWebSocketConnectionAsync(WebSocket webSocket, HttpContext httpContext)
{
if (webSocket == null)
{
throw new ArgumentNullException(nameof(webSocket));
}
ArgumentNullException.ThrowIfNull(webSocket);

var endpoint = $"{httpContext.Connection.RemoteIpAddress}:{httpContext.Connection.RemotePort}";

Expand Down
5 changes: 1 addition & 4 deletions Source/MQTTnet.AspnetCore/ReaderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ public static bool TryDecode(
out SequencePosition observed,
out int bytesRead)
{
if (formatter == null)
{
throw new ArgumentNullException(nameof(formatter));
}
ArgumentNullException.ThrowIfNull(formatter);

packet = null;
consumed = input.Start;
Expand Down
31 changes: 6 additions & 25 deletions Source/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,8 @@ public static class ServiceCollectionExtensions
{
public static IServiceCollection AddHostedMqttServer(this IServiceCollection services, MqttServerOptions options)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}

if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
ArgumentNullException.ThrowIfNull(services);
ArgumentNullException.ThrowIfNull(options);

services.AddSingleton(options);
services.AddHostedMqttServer();
Expand All @@ -34,10 +27,7 @@ public static IServiceCollection AddHostedMqttServer(this IServiceCollection ser

public static IServiceCollection AddHostedMqttServer(this IServiceCollection services, Action<MqttServerOptionsBuilder> configure)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
ArgumentNullException.ThrowIfNull(services);

var serverOptionsBuilder = new MqttServerOptionsBuilder();

Expand All @@ -61,10 +51,7 @@ public static void AddHostedMqttServer(this IServiceCollection services)

public static IServiceCollection AddHostedMqttServerWithServices(this IServiceCollection services, Action<AspNetMqttServerOptionsBuilder> configure)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
ArgumentNullException.ThrowIfNull(services);

services.AddSingleton(
s =>
Expand All @@ -89,20 +76,14 @@ public static IServiceCollection AddMqttConnectionHandler(this IServiceCollectio

public static void AddMqttLogger(this IServiceCollection services, IMqttNetLogger logger)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
ArgumentNullException.ThrowIfNull(services);

services.AddSingleton(logger);
}

public static IServiceCollection AddMqttServer(this IServiceCollection serviceCollection, Action<MqttServerOptionsBuilder> configure = null)
{
if (serviceCollection is null)
{
throw new ArgumentNullException(nameof(serviceCollection));
}
ArgumentNullException.ThrowIfNull(serviceCollection);

serviceCollection.AddMqttConnectionHandler();
serviceCollection.AddHostedMqttServer(configure);
Expand Down
2 changes: 1 addition & 1 deletion Source/MQTTnet.Benchmarks/ReaderExtensionsBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public static bool TryDecode(MqttPacketFormatterAdapter formatter,
out SequencePosition observed,
out int bytesRead)
{
if (formatter == null) throw new ArgumentNullException(nameof(formatter));
ArgumentNullException.ThrowIfNull(formatter);

packet = null;
consumed = input.Start;
Expand Down
11 changes: 2 additions & 9 deletions Source/MQTTnet.Benchmarks/TopicFilterComparerBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,8 @@ public void Setup()

static bool LegacyMethodByStringSplit(string topic, string filter)
{
if (topic == null)
{
throw new ArgumentNullException(nameof(topic));
}

if (filter == null)
{
throw new ArgumentNullException(nameof(filter));
}
ArgumentNullException.ThrowIfNull(topic);
ArgumentNullException.ThrowIfNull(filter);

if (string.Equals(topic, filter, StringComparison.Ordinal))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@ public sealed class DefaultMqttRpcClientTopicGenerationStrategy : IMqttRpcClient
{
public MqttRpcTopicPair CreateRpcTopics(TopicGenerationContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
ArgumentNullException.ThrowIfNull(context);

if (context.MethodName.Contains("/") || context.MethodName.Contains("+") || context.MethodName.Contains("#"))
{
throw new ArgumentException("The method name cannot contain /, + or #.");
}

var requestTopic = $"MQTTnet.RPC/{Guid.NewGuid():N}/{context.MethodName}";
var responseTopic = requestTopic + "/response";

Expand Down
11 changes: 2 additions & 9 deletions Source/MQTTnet.Extensions.Rpc/MqttFactoryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,8 @@ public static IMqttRpcClient CreateMqttRpcClient(this MqttClientFactory clientFa

public static IMqttRpcClient CreateMqttRpcClient(this MqttClientFactory _, IMqttClient mqttClient, MqttRpcClientOptions rpcClientOptions)
{
if (mqttClient == null)
{
throw new ArgumentNullException(nameof(mqttClient));
}

if (rpcClientOptions == null)
{
throw new ArgumentNullException(nameof(rpcClientOptions));
}
ArgumentNullException.ThrowIfNull(mqttClient);
ArgumentNullException.ThrowIfNull(rpcClientOptions);

return new MqttRpcClient(mqttClient, rpcClientOptions);
}
Expand Down
5 changes: 1 addition & 4 deletions Source/MQTTnet.Extensions.Rpc/MqttRpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,7 @@ public async Task<byte[]> ExecuteAsync(TimeSpan timeout, string methodName, byte

public async Task<byte[]> ExecuteAsync(string methodName, byte[] payload, MqttQualityOfServiceLevel qualityOfServiceLevel, IDictionary<string, object> parameters = null, CancellationToken cancellationToken = default)
{
if (methodName == null)
{
throw new ArgumentNullException(nameof(methodName));
}
ArgumentNullException.ThrowIfNull(methodName);

var context = new TopicGenerationContext(_mqttClient, _options, methodName, parameters, qualityOfServiceLevel);
var topicNames = _options.TopicGenerationStrategy.CreateRpcTopics(context);
Expand Down
5 changes: 1 addition & 4 deletions Source/MQTTnet.Extensions.TopicTemplate/MqttTopicTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ public sealed class MqttTopicTemplate : IEquatable<MqttTopicTemplate>
/// </exception>
public MqttTopicTemplate(string topicTemplate)
{
if (topicTemplate == null)
{
throw new ArgumentNullException(nameof(topicTemplate));
}
ArgumentNullException.ThrowIfNull(topicTemplate);

MqttTopicValidator.ThrowIfInvalidSubscribe(topicTemplate);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ public static class MqttConnAckPacketFactory
{
public static MqttConnAckPacket Create(ValidatingConnectionEventArgs validatingConnectionEventArgs)
{
if (validatingConnectionEventArgs == null)
{
throw new ArgumentNullException(nameof(validatingConnectionEventArgs));
}
ArgumentNullException.ThrowIfNull(validatingConnectionEventArgs);

var connAckPacket = new MqttConnAckPacket
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,8 @@ public static class MqttPubAckPacketFactory
{
public static MqttPubAckPacket Create(MqttPublishPacket publishPacket, DispatchApplicationMessageResult dispatchApplicationMessageResult)
{
if (publishPacket == null)
{
throw new ArgumentNullException(nameof(publishPacket));
}

if (dispatchApplicationMessageResult == null)
{
throw new ArgumentNullException(nameof(dispatchApplicationMessageResult));
}
ArgumentNullException.ThrowIfNull(publishPacket);
ArgumentNullException.ThrowIfNull(dispatchApplicationMessageResult);

var pubAckPacket = new MqttPubAckPacket
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ public static class MqttPubCompPacketFactory
{
public static MqttPubCompPacket Create(MqttPubRelPacket pubRelPacket, MqttApplicationMessageReceivedReasonCode reasonCode)
{
if (pubRelPacket == null)
{
throw new ArgumentNullException(nameof(pubRelPacket));
}
ArgumentNullException.ThrowIfNull(pubRelPacket);

return new MqttPubCompPacket
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ public static class MqttPubRecPacketFactory
{
public static MqttPacket Create(MqttPublishPacket publishPacket, DispatchApplicationMessageResult dispatchApplicationMessageResult)
{
if (publishPacket == null)
{
throw new ArgumentNullException(nameof(publishPacket));
}
ArgumentNullException.ThrowIfNull(publishPacket);

var pubRecPacket = new MqttPubRecPacket
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ public static class MqttPubRelPacketFactory
{
public static MqttPubRelPacket Create(MqttPubRecPacket pubRecPacket, MqttApplicationMessageReceivedReasonCode reasonCode)
{
if (pubRecPacket == null)
{
throw new ArgumentNullException(nameof(pubRecPacket));
}
ArgumentNullException.ThrowIfNull(pubRecPacket);

return new MqttPubRelPacket
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ public static class MqttPublishPacketFactory
{
public static MqttPublishPacket Create(MqttConnectPacket connectPacket)
{
if (connectPacket == null)
{
throw new ArgumentNullException(nameof(connectPacket));
}
ArgumentNullException.ThrowIfNull(connectPacket);

if (!connectPacket.WillFlag)
{
Expand Down Expand Up @@ -46,10 +43,7 @@ public static MqttPublishPacket Create(MqttConnectPacket connectPacket)

public static MqttPublishPacket Create(MqttApplicationMessage applicationMessage)
{
if (applicationMessage == null)
{
throw new ArgumentNullException(nameof(applicationMessage));
}
ArgumentNullException.ThrowIfNull(applicationMessage);

// Copy all values to their matching counterparts.
// The not supported values in MQTT 3.1.1 are not serialized (excluded) later.
Expand All @@ -75,10 +69,7 @@ public static MqttPublishPacket Create(MqttApplicationMessage applicationMessage

public static MqttPublishPacket Create(MqttRetainedMessageMatch retainedMessage)
{
if (retainedMessage == null)
{
throw new ArgumentNullException(nameof(retainedMessage));
}
ArgumentNullException.ThrowIfNull(retainedMessage);

var publishPacket = Create(retainedMessage.ApplicationMessage);
publishPacket.QualityOfServiceLevel = retainedMessage.SubscriptionQualityOfServiceLevel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,8 @@ public static class MqttSubAckPacketFactory
{
public static MqttSubAckPacket Create(MqttSubscribePacket subscribePacket, SubscribeResult subscribeResult)
{
if (subscribePacket == null)
{
throw new ArgumentNullException(nameof(subscribePacket));
}

if (subscribeResult == null)
{
throw new ArgumentNullException(nameof(subscribeResult));
}
ArgumentNullException.ThrowIfNull(subscribePacket);
ArgumentNullException.ThrowIfNull(subscribeResult);

var subAckPacket = new MqttSubAckPacket
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,8 @@ public static class MqttUnsubAckPacketFactory
{
public static MqttUnsubAckPacket Create(MqttUnsubscribePacket unsubscribePacket, UnsubscribeResult unsubscribeResult)
{
if (unsubscribePacket == null)
{
throw new ArgumentNullException(nameof(unsubscribePacket));
}

if (unsubscribeResult == null)
{
throw new ArgumentNullException(nameof(unsubscribeResult));
}
ArgumentNullException.ThrowIfNull(unsubscribePacket);
ArgumentNullException.ThrowIfNull(unsubscribeResult);

var unsubAckPacket = new MqttUnsubAckPacket
{
Expand Down
Loading