Skip to content

Commit 47ab74a

Browse files
tolzy88Copilot
andauthored
Enable Nullable (#4)
* Enable nullable reference types and annotate codebase Enabled C# nullable reference types and updated all relevant code to use nullable annotations and null-forgiving operators where appropriate. Constructors, method signatures, and interface implementations now accept nullable parameters as needed. Updated default assignments and enumerator implementations to avoid nullable warnings. Added [DoesNotReturn] to ThrowHelper methods and improved exception handling for nullability. These changes enhance code safety and compatibility with modern C# nullability analysis. * Bump version to 2.4 in version.json Updated the version number in version.json from 2.3 to 2.4 to reflect the new release. No other changes were made. * Fix nullable annotation issues flagged in code review (#5) * Initial plan * Apply nullable review feedback: fix ThrowHelper, SyncRoot, comparer signatures Co-authored-by: tolzy88 <229818339+tolzy88@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: tolzy88 <229818339+tolzy88@users.noreply.github.com> --------- Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
1 parent 98f4a09 commit 47ab74a

13 files changed

Lines changed: 386 additions & 342 deletions

Collections.Pooled/Collections.Pooled.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
<IncludeContentInPack>true</IncludeContentInPack>
3838
<GenerateDocumentationFile>true</GenerateDocumentationFile>
3939
<IncludeSymbols>false</IncludeSymbols>
40+
<Nullable>enable</Nullable>
4041
</PropertyGroup>
4142

4243
</Project>

Collections.Pooled/HashHelpers.SerializationInfoTable.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
// Used by Hashtable and Dictionary's SeralizationInfo .ctor's to store the SeralizationInfo
66
// object until OnDeserialization is called.
77

8-
using System.Threading;
98
using System.Runtime.CompilerServices;
109
using System.Runtime.Serialization;
10+
using System.Threading;
1111

1212
namespace Collections.Pooled
1313
{
1414
internal static partial class HashHelpers
15-
{
16-
private static ConditionalWeakTable<object, SerializationInfo> s_serializationInfoTable;
15+
{
16+
private static ConditionalWeakTable<object, SerializationInfo>? s_serializationInfoTable;
1717

1818
public static ConditionalWeakTable<object, SerializationInfo> SerializationInfoTable
1919
{

Collections.Pooled/NonRandomizedStringEqualityComparer.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Diagnostics;
8-
using System.Runtime.CompilerServices;
98
using System.Runtime.Serialization;
109

1110
namespace Collections.Pooled
@@ -28,9 +27,9 @@ private NonRandomizedStringEqualityComparer() { }
2827
// This is used by the serialization engine.
2928
private NonRandomizedStringEqualityComparer(SerializationInfo information, StreamingContext context) { }
3029

31-
public sealed override bool Equals(string x, string y) => string.Equals(x, y);
30+
public sealed override bool Equals(string? x, string? y) => string.Equals(x, y);
3231

33-
public sealed override int GetHashCode(string str)
32+
public sealed override int GetHashCode(string? str)
3433
=> str is null ? 0 : str.Length == 0 ? s_empyStringHashCode : GetNonRandomizedHashCode(str);
3534

3635
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)

Collections.Pooled/PooledDictionary.cs

Lines changed: 112 additions & 112 deletions
Large diffs are not rendered by default.

Collections.Pooled/PooledExtensions.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static PooledList<T> ToPooledList<T>(this Memory<T> memory)
4242
public static PooledDictionary<TKey, TValue> ToPooledDictionary<TSource, TKey, TValue>(
4343
this IEnumerable<TSource> source,
4444
Func<TSource, TKey> keySelector, Func<TSource, TValue> valueSelector,
45-
IEqualityComparer<TKey> comparer = null)
45+
IEqualityComparer<TKey>? comparer = null)
4646
{
4747
if (source == null)
4848
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
@@ -62,7 +62,7 @@ public static PooledDictionary<TKey, TValue> ToPooledDictionary<TSource, TKey, T
6262
public static PooledDictionary<TKey, TValue> ToPooledDictionary<TSource, TKey, TValue>(
6363
this ReadOnlySpan<TSource> source,
6464
Func<TSource, TKey> keySelector, Func<TSource, TValue> valueSelector,
65-
IEqualityComparer<TKey> comparer = null)
65+
IEqualityComparer<TKey>? comparer = null)
6666
{
6767
var dict = new PooledDictionary<TKey, TValue>(source.Length, comparer);
6868
foreach (var item in source)
@@ -112,7 +112,7 @@ public static PooledDictionary<TKey, TValue> ToPooledDictionary<TSource, TKey, T
112112
/// </summary>
113113
public static PooledDictionary<TKey, TSource> ToPooledDictionary<TSource, TKey>(
114114
this IEnumerable<TSource> source,
115-
Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer = null)
115+
Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer = null)
116116
{
117117
if (source == null)
118118
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
@@ -131,7 +131,7 @@ public static PooledDictionary<TKey, TSource> ToPooledDictionary<TSource, TKey>(
131131
/// </summary>
132132
public static PooledDictionary<TKey, TSource> ToPooledDictionary<TSource, TKey>(
133133
this ReadOnlySpan<TSource> source,
134-
Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer = null)
134+
Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer = null)
135135
{
136136
var dict = new PooledDictionary<TKey, TSource>(source.Length, comparer);
137137
foreach (var item in source)
@@ -147,7 +147,7 @@ public static PooledDictionary<TKey, TSource> ToPooledDictionary<TSource, TKey>(
147147
/// key selector and comparer.
148148
/// </summary>
149149
public static PooledDictionary<TKey, TSource> ToPooledDictionary<TSource, TKey>(this Span<TSource> source,
150-
Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer = null)
150+
Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer = null)
151151
{
152152
return ToPooledDictionary((ReadOnlySpan<TSource>)source, keySelector, comparer);
153153
}
@@ -158,7 +158,7 @@ public static PooledDictionary<TKey, TSource> ToPooledDictionary<TSource, TKey>(
158158
/// </summary>
159159
public static PooledDictionary<TKey, TSource> ToPooledDictionary<TSource, TKey>(
160160
this ReadOnlyMemory<TSource> source,
161-
Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer = null)
161+
Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer = null)
162162
{
163163
return ToPooledDictionary(source.Span, keySelector, comparer);
164164
}
@@ -168,7 +168,7 @@ public static PooledDictionary<TKey, TSource> ToPooledDictionary<TSource, TKey>(
168168
/// key selector and comparer.
169169
/// </summary>
170170
public static PooledDictionary<TKey, TSource> ToPooledDictionary<TSource, TKey>(this Memory<TSource> source,
171-
Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer = null)
171+
Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer = null)
172172
{
173173
return ToPooledDictionary(source.Span, keySelector, comparer);
174174
}
@@ -178,7 +178,7 @@ public static PooledDictionary<TKey, TSource> ToPooledDictionary<TSource, TKey>(
178178
/// </summary>
179179
public static PooledDictionary<TKey, TValue> ToPooledDictionary<TKey, TValue>(
180180
this IEnumerable<(TKey, TValue)> source,
181-
IEqualityComparer<TKey> comparer = null)
181+
IEqualityComparer<TKey>? comparer = null)
182182
{
183183
return new PooledDictionary<TKey, TValue>(source, comparer);
184184
}
@@ -188,7 +188,7 @@ public static PooledDictionary<TKey, TValue> ToPooledDictionary<TKey, TValue>(
188188
/// </summary>
189189
public static PooledDictionary<TKey, TValue> ToPooledDictionary<TKey, TValue>(
190190
this IEnumerable<KeyValuePair<TKey, TValue>> source,
191-
IEqualityComparer<TKey> comparer = null)
191+
IEqualityComparer<TKey>? comparer = null)
192192
{
193193
return new PooledDictionary<TKey, TValue>(source, comparer);
194194
}
@@ -198,7 +198,7 @@ public static PooledDictionary<TKey, TValue> ToPooledDictionary<TKey, TValue>(
198198
/// </summary>
199199
public static PooledDictionary<TKey, TValue> ToPooledDictionary<TKey, TValue>(
200200
this IEnumerable<Tuple<TKey, TValue>> source,
201-
IEqualityComparer<TKey> comparer = null)
201+
IEqualityComparer<TKey>? comparer = null)
202202
{
203203
if (source == null)
204204
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
@@ -217,7 +217,7 @@ public static PooledDictionary<TKey, TValue> ToPooledDictionary<TKey, TValue>(
217217
/// </summary>
218218
public static PooledDictionary<TKey, TValue> ToPooledDictionary<TKey, TValue>(
219219
this ReadOnlySpan<(TKey, TValue)> source,
220-
IEqualityComparer<TKey> comparer = null)
220+
IEqualityComparer<TKey>? comparer = null)
221221
{
222222
return new PooledDictionary<TKey, TValue>(source, comparer);
223223
}
@@ -226,7 +226,7 @@ public static PooledDictionary<TKey, TValue> ToPooledDictionary<TKey, TValue>(
226226
/// Creates a <see cref="PooledDictionary{TKey,TValue}"/> from a span of key/value tuples.
227227
/// </summary>
228228
public static PooledDictionary<TKey, TValue> ToPooledDictionary<TKey, TValue>(this Span<(TKey, TValue)> source,
229-
IEqualityComparer<TKey> comparer = null)
229+
IEqualityComparer<TKey>? comparer = null)
230230
{
231231
return new PooledDictionary<TKey, TValue>(source, comparer);
232232
}
@@ -235,19 +235,19 @@ public static PooledDictionary<TKey, TValue> ToPooledDictionary<TKey, TValue>(th
235235

236236
#region PooledSet
237237

238-
public static PooledSet<T> ToPooledSet<T>(this IEnumerable<T> source, IEqualityComparer<T> comparer = null)
238+
public static PooledSet<T> ToPooledSet<T>(this IEnumerable<T> source, IEqualityComparer<T>? comparer = null)
239239
=> new PooledSet<T>(source, comparer);
240240

241-
public static PooledSet<T> ToPooledSet<T>(this Span<T> source, IEqualityComparer<T> comparer = null)
241+
public static PooledSet<T> ToPooledSet<T>(this Span<T> source, IEqualityComparer<T>? comparer = null)
242242
=> new PooledSet<T>(source, comparer);
243243

244-
public static PooledSet<T> ToPooledSet<T>(this ReadOnlySpan<T> source, IEqualityComparer<T> comparer = null)
244+
public static PooledSet<T> ToPooledSet<T>(this ReadOnlySpan<T> source, IEqualityComparer<T>? comparer = null)
245245
=> new PooledSet<T>(source, comparer);
246246

247-
public static PooledSet<T> ToPooledSet<T>(this Memory<T> source, IEqualityComparer<T> comparer = null)
247+
public static PooledSet<T> ToPooledSet<T>(this Memory<T> source, IEqualityComparer<T>? comparer = null)
248248
=> new PooledSet<T>(source.Span, comparer);
249249

250-
public static PooledSet<T> ToPooledSet<T>(this ReadOnlyMemory<T> source, IEqualityComparer<T> comparer = null)
250+
public static PooledSet<T> ToPooledSet<T>(this ReadOnlyMemory<T> source, IEqualityComparer<T>? comparer = null)
251251
=> new PooledSet<T>(source.Span, comparer);
252252

253253
#endregion

0 commit comments

Comments
 (0)