Skip to content

Commit 7751318

Browse files
authored
Merge pull request #878 from drewnoakes/nullable-reference-types
Nullable reference types
2 parents 86af49c + f74c15a commit 7751318

Some content is hidden

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

69 files changed

+734
-535
lines changed

src/NetMQ/Annotations.cs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE.md file in the project root for more information.
2+
3+
#if !NETSTANDARD2_1
4+
5+
namespace System.Diagnostics.CodeAnalysis
6+
{
7+
/// <summary>Specifies that null is allowed as an input even if the corresponding type disallows it.</summary>
8+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)]
9+
internal sealed class AllowNullAttribute : Attribute
10+
{ }
11+
12+
/// <summary>Specifies that null is disallowed as an input even if the corresponding type allows it.</summary>
13+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)]
14+
internal sealed class DisallowNullAttribute : Attribute
15+
{ }
16+
17+
/// <summary>Specifies that an output may be null even if the corresponding type disallows it.</summary>
18+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)]
19+
internal sealed class MaybeNullAttribute : Attribute
20+
{ }
21+
22+
/// <summary>Specifies that an output will not be null even if the corresponding type allows it.</summary>
23+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)]
24+
internal sealed class NotNullAttribute : Attribute
25+
{ }
26+
27+
/// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter may be null even if the corresponding type disallows it.</summary>
28+
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
29+
internal sealed class MaybeNullWhenAttribute : Attribute
30+
{
31+
/// <summary>Initializes the attribute with the specified return value condition.</summary>
32+
/// <param name="returnValue">
33+
/// The return value condition. If the method returns this value, the associated parameter may be null.
34+
/// </param>
35+
public MaybeNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
36+
37+
/// <summary>Gets the return value condition.</summary>
38+
public bool ReturnValue { get; }
39+
}
40+
41+
/// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter will not be null even if the corresponding type allows it.</summary>
42+
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
43+
internal sealed class NotNullWhenAttribute : Attribute
44+
{
45+
/// <summary>Initializes the attribute with the specified return value condition.</summary>
46+
/// <param name="returnValue">
47+
/// The return value condition. If the method returns this value, the associated parameter will not be null.
48+
/// </param>
49+
public NotNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
50+
51+
/// <summary>Gets the return value condition.</summary>
52+
public bool ReturnValue { get; }
53+
}
54+
55+
/// <summary>Specifies that the output will be non-null if the named parameter is non-null.</summary>
56+
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, AllowMultiple = true, Inherited = false)]
57+
internal sealed class NotNullIfNotNullAttribute : Attribute
58+
{
59+
/// <summary>Initializes the attribute with the associated parameter name.</summary>
60+
/// <param name="parameterName">
61+
/// The associated parameter name. The output will be non-null if the argument to the parameter specified is non-null.
62+
/// </param>
63+
public NotNullIfNotNullAttribute(string parameterName) => ParameterName = parameterName;
64+
65+
/// <summary>Gets the associated parameter name.</summary>
66+
public string ParameterName { get; }
67+
}
68+
69+
/// <summary>Applied to a method that will never return under any circumstance.</summary>
70+
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
71+
internal sealed class DoesNotReturnAttribute : Attribute
72+
{ }
73+
74+
/// <summary>Specifies that the method will not return if the associated Boolean parameter is passed the specified value.</summary>
75+
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
76+
internal sealed class DoesNotReturnIfAttribute : Attribute
77+
{
78+
/// <summary>Initializes the attribute with the specified parameter value.</summary>
79+
/// <param name="parameterValue">
80+
/// The condition parameter value. Code after the method will be considered unreachable by diagnostics if the argument to
81+
/// the associated parameter matches this value.
82+
/// </param>
83+
public DoesNotReturnIfAttribute(bool parameterValue) => ParameterValue = parameterValue;
84+
85+
/// <summary>Gets the condition parameter value.</summary>
86+
public bool ParameterValue { get; }
87+
}
88+
}
89+
90+
#endif

src/NetMQ/AsyncReceiveExtensions.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
#if NETSTANDARD2_0 || NETSTANDARD2_1 || NET47
1+
#nullable enable
2+
#if NETSTANDARD2_0 || NETSTANDARD2_1 || NET47
23

34
using System;
45
using System.Collections.Generic;
56
using System.Text;
67
using System.Threading;
78
using System.Threading.Tasks;
8-
using JetBrains.Annotations;
99

1010
namespace NetMQ
1111
{
@@ -33,7 +33,7 @@ public static class AsyncReceiveExtensions
3333
/// <param name="cancellationToken">The token used to propagate notification that this operation should be canceled.</param>
3434
/// <returns>The content of the received message.</returns>
3535
public static async Task<NetMQMessage> ReceiveMultipartMessageAsync(
36-
[NotNull] this NetMQSocket socket,
36+
this NetMQSocket socket,
3737
int expectedFrameCount = 4,
3838
CancellationToken cancellationToken = default(CancellationToken))
3939
{
@@ -63,9 +63,8 @@ public static async Task<NetMQMessage> ReceiveMultipartMessageAsync(
6363
/// <param name="socket">The socket to receive from.</param>
6464
/// <param name="cancellationToken">The token used to propagate notification that this operation should be canceled.</param>
6565
/// <returns>The content of the received message frame and boolean indicate if another frame of the same message follows.</returns>
66-
[NotNull]
6766
public static Task<(byte[], bool)> ReceiveFrameBytesAsync(
68-
[NotNull] this NetMQSocket socket,
67+
this NetMQSocket socket,
6968
CancellationToken cancellationToken = default(CancellationToken)
7069
)
7170
{
@@ -117,9 +116,8 @@ void Listener(object sender, NetMQSocketEventArgs args)
117116
/// <param name="socket">The socket to receive from.</param>
118117
/// <param name="cancellationToken">The token used to propagate notification that this operation should be canceled.</param>
119118
/// <returns>The content of the received message frame as a string and a boolean indicate if another frame of the same message follows.</returns>
120-
[NotNull]
121119
public static Task<(string, bool)> ReceiveFrameStringAsync(
122-
[NotNull] this NetMQSocket socket,
120+
this NetMQSocket socket,
123121
CancellationToken cancellationToken = default(CancellationToken)
124122
)
125123
{
@@ -134,10 +132,9 @@ void Listener(object sender, NetMQSocketEventArgs args)
134132
/// <param name="encoding">The encoding used to convert the frame's data to a string.</param>
135133
/// <param name="cancellationToken">The token used to propagate notification that this operation should be canceled.</param>
136134
/// <returns>The content of the received message frame as a string and boolean indicate if another frame of the same message follows..</returns>
137-
[NotNull]
138135
public static Task<(string, bool)> ReceiveFrameStringAsync(
139-
[NotNull] this NetMQSocket socket,
140-
[NotNull] Encoding encoding,
136+
this NetMQSocket socket,
137+
Encoding encoding,
141138
CancellationToken cancellationToken = default(CancellationToken))
142139
{
143140
if (NetMQRuntime.Current == null)
@@ -190,7 +187,7 @@ void Listener(object sender, NetMQSocketEventArgs args)
190187
/// </summary>
191188
/// <param name="socket">The socket to receive from.</param>
192189
/// <returns>Boolean indicate if another frame of the same message follows</returns>
193-
public static Task<bool> SkipFrameAsync([NotNull] this NetMQSocket socket)
190+
public static Task<bool> SkipFrameAsync(this NetMQSocket socket)
194191
{
195192
if (NetMQRuntime.Current == null)
196193
throw new InvalidOperationException("NetMQRuntime must be created before calling async functions");
@@ -235,7 +232,7 @@ void Listener(object sender, NetMQSocketEventArgs args)
235232
/// Receive all frames of the next message from <paramref name="socket"/>, asynchronously, then ignore their contents.
236233
/// </summary>
237234
/// <param name="socket">The socket to receive from.</param>
238-
public static async Task SkipMultipartMessageAsync([NotNull] this NetMQSocket socket)
235+
public static async Task SkipMultipartMessageAsync(this NetMQSocket socket)
239236
{
240237
while (true)
241238
{
@@ -256,7 +253,7 @@ public static async Task SkipMultipartMessageAsync([NotNull] this NetMQSocket so
256253
/// <param name="socket">The socket to receive from.</param>
257254
/// <returns>The routing key and a boolean indicate if another frame of the same message follows.</returns>
258255

259-
public static async Task<(RoutingKey, bool)> ReceiveRoutingKeyAsync([NotNull] this NetMQSocket socket)
256+
public static async Task<(RoutingKey, bool)> ReceiveRoutingKeyAsync(this NetMQSocket socket)
260257
{
261258
var (bytes, more) = await socket.ReceiveFrameBytesAsync();
262259

src/NetMQ/AtomicCounterPool.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using JetBrains.Annotations;
1+
#nullable enable
2+
23
using NetMQ.Core.Utils;
34
using System;
45
using System.Threading;
@@ -15,14 +16,13 @@ public interface IAtomicCounterPool : IDisposable
1516
/// Take an AtomicCounter from the pool.
1617
/// </summary>
1718
/// <returns>an atomic counter</returns>
18-
[NotNull]
1919
AtomicCounter Take();
2020

2121
/// <summary>
2222
/// Return the given atomic counter to the common pool.
2323
/// </summary>
2424
/// <param name="counter">the atomic counter to return to the buffer-pool</param>
25-
void Return([NotNull] AtomicCounter counter);
25+
void Return(AtomicCounter counter);
2626
}
2727

2828
/// <summary>
@@ -99,7 +99,7 @@ public static void SetGCCounterPool()
9999
/// Set BufferPool to use the specified IAtomicCounterPool implementation to manage the pool.
100100
/// </summary>
101101
/// <param name="counterPool">the implementation of <see cref="IAtomicCounterPool"/> to use</param>
102-
public static void SetCustomCounterPool([NotNull] IAtomicCounterPool counterPool)
102+
public static void SetCustomCounterPool(IAtomicCounterPool counterPool)
103103
{
104104
var prior = Interlocked.Exchange(ref s_counterPool, counterPool);
105105

@@ -110,7 +110,6 @@ public static void SetCustomCounterPool([NotNull] IAtomicCounterPool counterPool
110110
/// Allocate an atomic counter from the current <see cref="IAtomicCounterPool"/>.
111111
/// </summary>
112112
/// <returns>an atomic counter</returns>
113-
[NotNull]
114113
public static AtomicCounter Take()
115114
{
116115
return s_counterPool.Take();
@@ -120,7 +119,7 @@ public static AtomicCounter Take()
120119
/// Returns <paramref name="counter"/> to the <see cref="IAtomicCounterPool"/>.
121120
/// </summary>
122121
/// <param name="counter">The atomic counter to be returned to the pool.</param>
123-
public static void Return([NotNull] AtomicCounter counter)
122+
public static void Return(AtomicCounter counter)
124123
{
125124
s_counterPool.Return(counter);
126125
}

src/NetMQ/BufferPool.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
#nullable enable
2+
13
using System;
24
using System.ServiceModel.Channels;
35
using System.Threading;
4-
using JetBrains.Annotations;
56

67
namespace NetMQ
78
{
@@ -16,14 +17,13 @@ public interface IBufferPool : IDisposable
1617
/// </summary>
1718
/// <param name="size">the number of bytes to take</param>
1819
/// <returns>a byte-array that comes from the buffer-pool</returns>
19-
[NotNull]
2020
byte[] Take(int size);
2121

2222
/// <summary>
2323
/// Return the given byte-array buffer to the common buffer-pool.
2424
/// </summary>
2525
/// <param name="buffer">the byte-array to return to the buffer-pool</param>
26-
void Return([NotNull] byte[] buffer);
26+
void Return(byte[] buffer);
2727
}
2828

2929
/// <summary>
@@ -180,7 +180,7 @@ public static void SetBufferManagerBufferPool(long maxBufferPoolSize, int maxBuf
180180
/// Set BufferPool to use the specified IBufferPool implementation to manage the buffer-pool.
181181
/// </summary>
182182
/// <param name="bufferPool">the implementation of <see cref="IBufferPool"/> to use</param>
183-
public static void SetCustomBufferPool([NotNull] IBufferPool bufferPool)
183+
public static void SetCustomBufferPool(IBufferPool bufferPool)
184184
{
185185
var prior = Interlocked.Exchange(ref s_bufferPool, bufferPool);
186186

@@ -192,7 +192,6 @@ public static void SetCustomBufferPool([NotNull] IBufferPool bufferPool)
192192
/// </summary>
193193
/// <param name="size">The minimum size required, in bytes.</param>
194194
/// <returns>A byte array having at least <paramref name="size"/> bytes.</returns>
195-
[NotNull]
196195
public static byte[] Take(int size)
197196
{
198197
return s_bufferPool.Take(size);
@@ -202,7 +201,7 @@ public static byte[] Take(int size)
202201
/// Returns <paramref name="buffer"/> to the <see cref="IBufferPool"/>.
203202
/// </summary>
204203
/// <param name="buffer">The byte array to be returned to the pool.</param>
205-
public static void Return([NotNull] byte[] buffer)
204+
public static void Return(byte[] buffer)
206205
{
207206
s_bufferPool.Return(buffer);
208207
}

src/NetMQ/EmptyArray.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#nullable enable
2+
13
namespace NetMQ
24
{
35
internal static class EmptyArray<T>

src/NetMQ/Endianness.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace NetMQ
1+
#nullable enable
2+
3+
namespace NetMQ
24
{
35
/// <summary>
46
/// This enum-type specifies either big-endian (Big) or little-endian (Little),

src/NetMQ/ErrorCode.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#nullable enable
2+
13
namespace NetMQ
24
{
35
/// <summary>

src/NetMQ/EventDelegator.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
using System;
1+
#nullable enable
2+
3+
using System;
24
using System.Threading;
3-
using JetBrains.Annotations;
45

56
namespace NetMQ
67
{
@@ -16,15 +17,15 @@ internal class EventDelegator<T> : IDisposable where T : EventArgs
1617
{
1718
private readonly Action m_registerToEvent;
1819
private readonly Action m_unregisterFromEvent;
19-
private EventHandler<T> m_event;
20+
private EventHandler<T>? m_event;
2021
private int m_counter;
2122

2223
/// <summary>
2324
/// Initialises a new instance.
2425
/// </summary>
2526
/// <param name="registerToEvent">an Action to perform when the first handler is registered for the event</param>
2627
/// <param name="unregisterFromEvent">an Action to perform when the last handler is unregistered from the event</param>
27-
public EventDelegator([NotNull] Action registerToEvent, [NotNull] Action unregisterFromEvent)
28+
public EventDelegator(Action registerToEvent, Action unregisterFromEvent)
2829
{
2930
m_registerToEvent = registerToEvent;
3031
m_unregisterFromEvent = unregisterFromEvent;
@@ -53,7 +54,7 @@ public event EventHandler<T> Event
5354
/// </summary>
5455
/// <param name="sender">the sender that the event-handler that gets notified of this event will receive</param>
5556
/// <param name="args">the subclass of EventArgs that the event-handler will receive</param>
56-
public void Fire([NotNull] object sender, [NotNull] T args)
57+
public void Fire(object sender, T args)
5758
{
5859
m_event?.Invoke(sender, args);
5960
}

src/NetMQ/INetMQPoller.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#nullable enable
2+
13
using System;
24

35
namespace NetMQ

0 commit comments

Comments
 (0)