From 2d7fdd052f9891e3f6219ce5b550d81f10869be7 Mon Sep 17 00:00:00 2001 From: Alex Zaytsev Date: Fri, 22 Mar 2024 18:12:10 +1000 Subject: [PATCH] feat: enable nullable annotations (#36) --- src/Directory.Build.props | 2 ++ .../Generic/GenericSetFixture.cs | 6 ++-- src/Iesi.Collections/Generic/LinkedHashSet.cs | 32 ++++++++----------- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 8c46db9..3d774ea 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -1,6 +1,8 @@ + true + enable 12 diff --git a/src/Iesi.Collections.Test/Generic/GenericSetFixture.cs b/src/Iesi.Collections.Test/Generic/GenericSetFixture.cs index cd2856a..045330d 100644 --- a/src/Iesi.Collections.Test/Generic/GenericSetFixture.cs +++ b/src/Iesi.Collections.Test/Generic/GenericSetFixture.cs @@ -9,9 +9,9 @@ namespace Iesi.Collections.Test.Generic; /// public abstract class GenericSetFixture { - IList _aInitValues; - IList _bInitValues; - protected ISet _set; + IList _aInitValues = default!; + IList _bInitValues = default!; + protected ISet _set = default!; public static string one = "one"; public static string two = "two"; diff --git a/src/Iesi.Collections/Generic/LinkedHashSet.cs b/src/Iesi.Collections/Generic/LinkedHashSet.cs index e4ce3a7..502f0f0 100644 --- a/src/Iesi.Collections/Generic/LinkedHashSet.cs +++ b/src/Iesi.Collections/Generic/LinkedHashSet.cs @@ -17,11 +17,11 @@ namespace Iesi.Collections.Generic; #endif public class LinkedHashSet : ISet #if !NET40 - , IReadOnlyCollection + , IReadOnlyCollection where T : notnull #endif { - readonly Dictionary> _elements = new Dictionary>(); - LinkedHashNode _first, _last; + readonly Dictionary> _elements = new(); + LinkedHashNode? _first, _last; /// /// Initializes a new instance of the class. @@ -151,8 +151,7 @@ public void CopyTo(T[] array, int arrayIndex) /// The object to remove from the .The is read-only. public bool Remove(T item) { - LinkedHashNode node; - if (_elements.TryGetValue(item, out node)) + if (_elements.TryGetValue(item, out var node)) { _elements.Remove(item); Unlink(node); @@ -216,8 +215,7 @@ public void SymmetricExceptWith(IEnumerable other) { foreach (var item in other) { - LinkedHashNode node; - if (_elements.TryGetValue(item, out node)) + if (_elements.TryGetValue(item, out var node)) { _elements.Remove(item); Unlink(node); @@ -254,8 +252,7 @@ public bool IsSubsetOf(IEnumerable other) /// The collection to compare to the current set. is null. public bool IsSupersetOf(IEnumerable other) { - int numberOfOthersPresent; - var numberOfOthers = CountOthers(other, out numberOfOthersPresent); + var numberOfOthers = CountOthers(other, out var numberOfOthersPresent); // All others must be present. return numberOfOthersPresent == numberOfOthers; @@ -270,8 +267,7 @@ public bool IsSupersetOf(IEnumerable other) /// The collection to compare to the current set. is null. public bool IsProperSupersetOf(IEnumerable other) { - int numberOfOthersPresent; - var numberOfOthers = CountOthers(other, out numberOfOthersPresent); + var numberOfOthers = CountOthers(other, out var numberOfOthersPresent); // All others must be present, plus we need to have at least one additional item. return numberOfOthersPresent == numberOfOthers && numberOfOthers < Count; @@ -423,8 +419,8 @@ public LinkedHashNode(TElement value) } public readonly TElement Value; - public LinkedHashNode Next; - public LinkedHashNode Previous; + public LinkedHashNode? Next; + public LinkedHashNode? Previous; } /// @@ -435,12 +431,12 @@ public LinkedHashNode(TElement value) #endif public struct Enumerator : IEnumerator { - LinkedHashNode _node; - T _current; + LinkedHashNode? _node; + T? _current; internal Enumerator(LinkedHashSet set) { - _current = default(T); + _current = default; _node = set._first; } @@ -456,10 +452,10 @@ public bool MoveNext() } /// - public T Current => _current; + public T Current => _current!; /// - object IEnumerator.Current => Current; + object IEnumerator.Current => Current!; /// void IEnumerator.Reset() => throw new NotSupportedException();