Skip to content

Commit 1768960

Browse files
authored
Merge pull request #1162 from drewnoakes/nullable-annotations-100-percent
Complete the nullable annotations
2 parents cbe150e + 5ee2b20 commit 1768960

17 files changed

Lines changed: 237 additions & 232 deletions

File tree

src/Directory.Build.props

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
<Project>
22

33
<PropertyGroup>
4-
<LangVersion>13</LangVersion>
4+
<LangVersion>14</LangVersion>
55
<Nullable>enable</Nullable>
66
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
77
<AccelerateBuildsInVisualStudio>true</AccelerateBuildsInVisualStudio>
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="JetBrains.Annotations" Version="2022.3.1" PrivateAssets="All" />
1211
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
1312
</ItemGroup>
1413

src/NetMQ.Tests/CleanupAfterFixture.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using System;
2-
using JetBrains.Annotations;
32

43
namespace NetMQ.Tests
54
{
6-
[UsedImplicitly]
75
public sealed class CleanupAfterFixture : IDisposable
86
{
97
public void Dispose() => NetMQConfig.Cleanup();

src/NetMQ.Tests/NetMQ.Tests.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
</ItemGroup>
5555

5656
<ItemGroup>
57-
<PackageReference Update="JetBrains.Annotations" Version="2025.2.4" />
5857
<PackageReference Update="Microsoft.SourceLink.GitHub" Version="10.0.202" />
5958
</ItemGroup>
6059

src/NetMQ/Annotations.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,56 @@ internal sealed class DoesNotReturnIfAttribute : Attribute
8787
}
8888
}
8989

90+
#endif
91+
92+
#if !NET5_0_OR_GREATER
93+
94+
namespace System.Diagnostics.CodeAnalysis
95+
{
96+
/// <summary>Specifies that the method or property will ensure that the listed field and property members have not-null values.</summary>
97+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
98+
internal sealed class MemberNotNullAttribute : Attribute
99+
{
100+
/// <summary>Initializes the attribute with a field or property member.</summary>
101+
/// <param name="member">The field or property member that is promised to be not-null.</param>
102+
public MemberNotNullAttribute(string member) => Members = [member];
103+
104+
/// <summary>Initializes the attribute with the list of field and property members.</summary>
105+
/// <param name="members">The list of field and property members that are promised to be not-null.</param>
106+
public MemberNotNullAttribute(params string[] members) => Members = members;
107+
108+
/// <summary>Gets field or property member names.</summary>
109+
public string[] Members { get; }
110+
}
111+
112+
/// <summary>Specifies that the method or property will ensure that the listed field and property members have not-null values when returning with the specified return value condition.</summary>
113+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
114+
internal sealed class MemberNotNullWhenAttribute : Attribute
115+
{
116+
/// <summary>Initializes the attribute with the specified return value condition and a field or property member.</summary>
117+
/// <param name="returnValue">The return value condition. If the method returns this value, the associated parameter will not be null.</param>
118+
/// <param name="member">The field or property member that is promised to be not-null.</param>
119+
public MemberNotNullWhenAttribute(bool returnValue, string member)
120+
{
121+
ReturnValue = returnValue;
122+
Members = [member];
123+
}
124+
125+
/// <summary>Initializes the attribute with the specified return value condition and list of field and property members.</summary>
126+
/// <param name="returnValue">The return value condition. If the method returns this value, the associated parameter will not be null.</param>
127+
/// <param name="members">The list of field and property members that are promised to be not-null.</param>
128+
public MemberNotNullWhenAttribute(bool returnValue, params string[] members)
129+
{
130+
ReturnValue = returnValue;
131+
Members = members;
132+
}
133+
134+
/// <summary>Gets the return value condition.</summary>
135+
public bool ReturnValue { get; }
136+
137+
/// <summary>Gets field or property member names.</summary>
138+
public string[] Members { get; }
139+
}
140+
}
141+
90142
#endif

src/NetMQ/Core/Ctx.cs

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,11 @@ You should have received a copy of the GNU Lesser General Public License
1919
along with this program. If not, see <http://www.gnu.org/licenses/>.
2020
*/
2121

22-
#nullable disable
23-
2422
using System;
2523
using System.Collections.Generic;
2624
using System.Diagnostics;
2725
using System.Linq;
2826
using System.Threading;
29-
using JetBrains.Annotations;
30-
3127
namespace NetMQ.Core
3228
{
3329
/// <summary>
@@ -54,7 +50,7 @@ public class Endpoint
5450
/// </summary>
5551
/// <param name="socket">the socket for this new Endpoint</param>
5652
/// <param name="options">the Options to assign to this new Endpoint</param>
57-
public Endpoint([NotNull] SocketBase socket, [NotNull] Options options)
53+
public Endpoint(SocketBase socket, Options options)
5854
{
5955
Socket = socket;
6056
Options = options;
@@ -63,13 +59,11 @@ public Endpoint([NotNull] SocketBase socket, [NotNull] Options options)
6359
/// <summary>
6460
/// Get the socket associated with this Endpoint.
6561
/// </summary>
66-
[NotNull]
6762
public SocketBase Socket { get; }
6863

6964
/// <summary>
7065
/// Get the Options of this Endpoint.
7166
/// </summary>
72-
[NotNull]
7367
public Options Options { get; }
7468
}
7569

@@ -109,7 +103,7 @@ public Endpoint([NotNull] SocketBase socket, [NotNull] Options options)
109103
/// <summary>
110104
/// The reaper thread.
111105
/// </summary>
112-
[CanBeNull] private Reaper m_reaper;
106+
private Reaper? m_reaper;
113107

114108
/// <summary>
115109
/// List of I/O threads.
@@ -124,7 +118,7 @@ public Endpoint([NotNull] SocketBase socket, [NotNull] Options options)
124118
/// <summary>
125119
/// Array of pointers to mailboxes for both application and I/O threads.
126120
/// </summary>
127-
[CanBeNull] private IMailbox[] m_slots;
121+
private IMailbox?[]? m_slots;
128122

129123
/// <summary>
130124
/// Mailbox for zmq_term thread.
@@ -202,9 +196,9 @@ public void Terminate(bool block)
202196
socket.Stop();
203197

204198
if (!block)
205-
m_reaper.ForceStop();
199+
m_reaper!.ForceStop();
206200
else if (m_sockets.Count == 0)
207-
m_reaper.Stop();
201+
m_reaper!.Stop();
208202
}
209203
finally
210204
{
@@ -265,7 +259,6 @@ public int MaxSockets
265259
/// <exception cref="TerminatingException">Cannot create new socket while terminating.</exception>
266260
/// <exception cref="NetMQException">Maximum number of sockets reached.</exception>
267261
/// <exception cref="TerminatingException">The context (Ctx) must not be already terminating.</exception>
268-
[NotNull]
269262
public SocketBase CreateSocket(ZmqSocketType type)
270263
{
271264
lock (m_slotSync)
@@ -343,7 +336,7 @@ public SocketBase CreateSocket(ZmqSocketType type)
343336
SocketBase s = SocketBase.Create(type, this, slot, socketId);
344337

345338
m_sockets.Add(s);
346-
m_slots[slot] = s.Mailbox;
339+
m_slots![slot] = s.Mailbox;
347340

348341
//LOG.debug("NEW Slot [" + slot + "] " + s);
349342

@@ -359,14 +352,14 @@ public SocketBase CreateSocket(ZmqSocketType type)
359352
/// <remarks>
360353
/// If this was the last socket, then stop the reaper.
361354
/// </remarks>
362-
public void DestroySocket([NotNull] SocketBase socket)
355+
public void DestroySocket(SocketBase socket)
363356
{
364357
// Free the associated thread slot.
365358
lock (m_slotSync)
366359
{
367360
int threadId = socket.ThreadId;
368361
m_emptySlots.Push(threadId);
369-
m_slots[threadId].Close();
362+
m_slots![threadId]!.Close();
370363
m_slots[threadId] = null;
371364

372365
// Remove the socket from the list of sockets.
@@ -375,7 +368,7 @@ public void DestroySocket([NotNull] SocketBase socket)
375368
// If zmq_term() was already called and there are no more socket
376369
// we can ask reaper thread to terminate.
377370
if (m_terminating && m_sockets.Count == 0)
378-
m_reaper.Stop();
371+
m_reaper!.Stop();
379372
}
380373

381374
//LOG.debug("Released Slot [" + socket_ + "] ");
@@ -386,31 +379,30 @@ public void DestroySocket([NotNull] SocketBase socket)
386379
/// </summary>
387380
public ZObject GetReaper()
388381
{
389-
return m_reaper;
382+
return m_reaper!;
390383
}
391384

392385
/// <summary>
393386
/// Send a command to the given destination thread.
394387
/// </summary>
395-
public void SendCommand(int threadId, [NotNull] Command command)
388+
public void SendCommand(int threadId, Command command)
396389
{
397-
m_slots[threadId].Send(command);
390+
m_slots![threadId]!.Send(command);
398391
}
399392

400393
/// <summary>
401394
/// Returns the <see cref="IOThread"/> that is the least busy at the moment.
402395
/// </summary>
403396
/// <paramref name="affinity">Which threads are eligible (0 = all).</paramref>
404397
/// <returns>The least busy thread, or <c>null</c> if none is available.</returns>
405-
[CanBeNull]
406-
public IOThread ChooseIOThread(long affinity)
398+
public IOThread? ChooseIOThread(long affinity)
407399
{
408400
if (m_ioThreads.Count == 0)
409401
return null;
410402

411403
// Find the I/O thread with minimum load.
412404
int minLoad = -1;
413-
IOThread selectedIOThread = null;
405+
IOThread? selectedIOThread = null;
414406

415407
for (int i = 0; i != m_ioThreads.Count; i++)
416408
{
@@ -435,7 +427,7 @@ public IOThread ChooseIOThread(long affinity)
435427
/// <param name="address">the textual name to give this endpoint</param>
436428
/// <param name="endpoint">the Endpoint to remember</param>
437429
/// <returns>true if the given address was NOT already registered</returns>
438-
public bool RegisterEndpoint([NotNull] string address, [NotNull] Endpoint endpoint)
430+
public bool RegisterEndpoint(string address, Endpoint endpoint)
439431
{
440432
lock (m_endpointsSync)
441433
{
@@ -453,12 +445,12 @@ public bool RegisterEndpoint([NotNull] string address, [NotNull] Endpoint endpoi
453445
/// <param name="address">the (string) address denoting the endpoint to unregister</param>
454446
/// <param name="socket">the socket associated with that endpoint</param>
455447
/// <returns>true if the endpoint having this address and socket is found, false otherwise</returns>
456-
public bool UnregisterEndpoint([NotNull] string address, [NotNull] SocketBase socket)
448+
public bool UnregisterEndpoint(string address, SocketBase socket)
457449
{
458450
lock (m_endpointsSync)
459451
{
460452

461-
if (!m_endpoints.TryGetValue(address, out Endpoint endpoint))
453+
if (!m_endpoints.TryGetValue(address, out Endpoint? endpoint))
462454
return false;
463455

464456
if (socket != endpoint.Socket)
@@ -473,7 +465,7 @@ public bool UnregisterEndpoint([NotNull] string address, [NotNull] SocketBase so
473465
/// Remove from the list of endpoints, all endpoints that reference the given socket.
474466
/// </summary>
475467
/// <param name="socket">the socket to remove all references to</param>
476-
public void UnregisterEndpoints([NotNull] SocketBase socket)
468+
public void UnregisterEndpoints(SocketBase socket)
477469
{
478470
lock (m_endpointsSync)
479471
{
@@ -494,11 +486,8 @@ public void UnregisterEndpoints([NotNull] SocketBase socket)
494486
/// By calling this method, the socket associated with that returned EndPoint has it's Seqnum incremented,
495487
/// in order to prevent it from being de-allocated before a command can be sent to it.
496488
/// </remarks>
497-
[NotNull]
498-
public Endpoint FindEndpoint([NotNull] string addr)
489+
public Endpoint FindEndpoint(string addr)
499490
{
500-
Debug.Assert(addr != null);
501-
502491
lock (m_endpointsSync)
503492
{
504493
if (!m_endpoints.ContainsKey(addr))

0 commit comments

Comments
 (0)