Skip to content

Commit 63d8518

Browse files
authored
Convert null checks to modern ThrowIfNull checks (#2089)
Convert null check to modern ThrowIfNull checks
1 parent 0506857 commit 63d8518

File tree

85 files changed

+209
-783
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+209
-783
lines changed

Samples/Server/Server_Retained_Messages_Samples.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,7 @@ sealed class MqttRetainedMessageModel
102102

103103
public static MqttRetainedMessageModel Create(MqttApplicationMessage message)
104104
{
105-
if (message == null)
106-
{
107-
throw new ArgumentNullException(nameof(message));
108-
}
105+
ArgumentNullException.ThrowIfNull(message);
109106

110107
return new MqttRetainedMessageModel
111108
{

Source/MQTTnet.AspnetCore/EndpointRouterExtensions.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ public static class EndpointRouterExtensions
1212
{
1313
public static void MapMqtt(this IEndpointRouteBuilder endpoints, string pattern)
1414
{
15-
if (endpoints == null)
16-
{
17-
throw new ArgumentNullException(nameof(endpoints));
18-
}
15+
ArgumentNullException.ThrowIfNull(endpoints);
1916

2017
endpoints.MapConnectionHandler<MqttConnectionHandler>(pattern, options =>
2118
{

Source/MQTTnet.AspnetCore/MqttSubProtocolSelector.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ public static class MqttSubProtocolSelector
1313
{
1414
public static string SelectSubProtocol(HttpRequest request)
1515
{
16-
if (request == null)
17-
{
18-
throw new ArgumentNullException(nameof(request));
19-
}
16+
ArgumentNullException.ThrowIfNull(request);
2017

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

3027
public static string SelectSubProtocol(IList<string> requestedSubProtocolValues)
3128
{
32-
if (requestedSubProtocolValues == null)
33-
{
34-
throw new ArgumentNullException(nameof(requestedSubProtocolValues));
35-
}
29+
ArgumentNullException.ThrowIfNull(requestedSubProtocolValues);
3630

3731
// Order the protocols to also match "mqtt", "mqttv-3.1", "mqttv-3.11" etc.
3832
return requestedSubProtocolValues.OrderByDescending(p => p.Length).FirstOrDefault(p => p.ToLower().StartsWith("mqtt"));

Source/MQTTnet.AspnetCore/MqttWebSocketServerAdapter.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ public void Dispose()
2626

2727
public async Task RunWebSocketConnectionAsync(WebSocket webSocket, HttpContext httpContext)
2828
{
29-
if (webSocket == null)
30-
{
31-
throw new ArgumentNullException(nameof(webSocket));
32-
}
29+
ArgumentNullException.ThrowIfNull(webSocket);
3330

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

Source/MQTTnet.AspnetCore/ReaderExtensions.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ public static bool TryDecode(
2222
out SequencePosition observed,
2323
out int bytesRead)
2424
{
25-
if (formatter == null)
26-
{
27-
throw new ArgumentNullException(nameof(formatter));
28-
}
25+
ArgumentNullException.ThrowIfNull(formatter);
2926

3027
packet = null;
3128
consumed = input.Start;

Source/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,8 @@ public static class ServiceCollectionExtensions
1616
{
1717
public static IServiceCollection AddHostedMqttServer(this IServiceCollection services, MqttServerOptions options)
1818
{
19-
if (services == null)
20-
{
21-
throw new ArgumentNullException(nameof(services));
22-
}
23-
24-
if (options == null)
25-
{
26-
throw new ArgumentNullException(nameof(options));
27-
}
19+
ArgumentNullException.ThrowIfNull(services);
20+
ArgumentNullException.ThrowIfNull(options);
2821

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

3528
public static IServiceCollection AddHostedMqttServer(this IServiceCollection services, Action<MqttServerOptionsBuilder> configure)
3629
{
37-
if (services == null)
38-
{
39-
throw new ArgumentNullException(nameof(services));
40-
}
30+
ArgumentNullException.ThrowIfNull(services);
4131

4232
var serverOptionsBuilder = new MqttServerOptionsBuilder();
4333

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

6252
public static IServiceCollection AddHostedMqttServerWithServices(this IServiceCollection services, Action<AspNetMqttServerOptionsBuilder> configure)
6353
{
64-
if (services == null)
65-
{
66-
throw new ArgumentNullException(nameof(services));
67-
}
54+
ArgumentNullException.ThrowIfNull(services);
6855

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

9077
public static void AddMqttLogger(this IServiceCollection services, IMqttNetLogger logger)
9178
{
92-
if (services == null)
93-
{
94-
throw new ArgumentNullException(nameof(services));
95-
}
79+
ArgumentNullException.ThrowIfNull(services);
9680

9781
services.AddSingleton(logger);
9882
}
9983

10084
public static IServiceCollection AddMqttServer(this IServiceCollection serviceCollection, Action<MqttServerOptionsBuilder> configure = null)
10185
{
102-
if (serviceCollection is null)
103-
{
104-
throw new ArgumentNullException(nameof(serviceCollection));
105-
}
86+
ArgumentNullException.ThrowIfNull(serviceCollection);
10687

10788
serviceCollection.AddMqttConnectionHandler();
10889
serviceCollection.AddHostedMqttServer(configure);

Source/MQTTnet.Benchmarks/ReaderExtensionsBenchmark.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public static bool TryDecode(MqttPacketFormatterAdapter formatter,
145145
out SequencePosition observed,
146146
out int bytesRead)
147147
{
148-
if (formatter == null) throw new ArgumentNullException(nameof(formatter));
148+
ArgumentNullException.ThrowIfNull(formatter);
149149

150150
packet = null;
151151
consumed = input.Start;

Source/MQTTnet.Benchmarks/TopicFilterComparerBenchmark.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,8 @@ public void Setup()
7373

7474
static bool LegacyMethodByStringSplit(string topic, string filter)
7575
{
76-
if (topic == null)
77-
{
78-
throw new ArgumentNullException(nameof(topic));
79-
}
80-
81-
if (filter == null)
82-
{
83-
throw new ArgumentNullException(nameof(filter));
84-
}
76+
ArgumentNullException.ThrowIfNull(topic);
77+
ArgumentNullException.ThrowIfNull(filter);
8578

8679
if (string.Equals(topic, filter, StringComparison.Ordinal))
8780
{

Source/MQTTnet.Extensions.Rpc/DefaultMqttRpcClientTopicGenerationStrategy.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,13 @@ public sealed class DefaultMqttRpcClientTopicGenerationStrategy : IMqttRpcClient
1010
{
1111
public MqttRpcTopicPair CreateRpcTopics(TopicGenerationContext context)
1212
{
13-
if (context == null)
14-
{
15-
throw new ArgumentNullException(nameof(context));
16-
}
13+
ArgumentNullException.ThrowIfNull(context);
1714

1815
if (context.MethodName.Contains("/") || context.MethodName.Contains("+") || context.MethodName.Contains("#"))
1916
{
2017
throw new ArgumentException("The method name cannot contain /, + or #.");
2118
}
22-
19+
2320
var requestTopic = $"MQTTnet.RPC/{Guid.NewGuid():N}/{context.MethodName}";
2421
var responseTopic = requestTopic + "/response";
2522

Source/MQTTnet.Extensions.Rpc/MqttFactoryExtensions.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,8 @@ public static IMqttRpcClient CreateMqttRpcClient(this MqttClientFactory clientFa
2020

2121
public static IMqttRpcClient CreateMqttRpcClient(this MqttClientFactory _, IMqttClient mqttClient, MqttRpcClientOptions rpcClientOptions)
2222
{
23-
if (mqttClient == null)
24-
{
25-
throw new ArgumentNullException(nameof(mqttClient));
26-
}
27-
28-
if (rpcClientOptions == null)
29-
{
30-
throw new ArgumentNullException(nameof(rpcClientOptions));
31-
}
23+
ArgumentNullException.ThrowIfNull(mqttClient);
24+
ArgumentNullException.ThrowIfNull(rpcClientOptions);
3225

3326
return new MqttRpcClient(mqttClient, rpcClientOptions);
3427
}

0 commit comments

Comments
 (0)