Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable nullable context for entire solution #915

Merged
merged 3 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project>
<PropertyGroup>
<LangVersion>11</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
</Project>
12 changes: 6 additions & 6 deletions MoreLinq.Test/AcquireTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public class AcquireTest
[Test]
public void AcquireAll()
{
Disposable a = null;
Disposable b = null;
Disposable c = null;
Disposable? a = null;
Disposable? b = null;
Disposable? c = null;

var allocators = MoreEnumerable.From(() => a = new Disposable(),
() => b = new Disposable(),
Expand All @@ -48,9 +48,9 @@ public void AcquireAll()
[Test]
public void AcquireSome()
{
Disposable a = null;
Disposable b = null;
Disposable c = null;
Disposable? a = null;
Disposable? b = null;
Disposable? c = null;

var allocators = MoreEnumerable.From(() => a = new Disposable(),
() => b = new Disposable(),
Expand Down
11 changes: 6 additions & 5 deletions MoreLinq.Test/AggregateTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ into m
AccumulatorCount = (m.Instantiation.GetParameters().Length - 2 /* source + resultSelector */) / 2 /* seed + accumulator */,
ResultSelectorType = rst,
Parameters =
rst.GetMethod("Invoke")
.GetParameters()
.Select(p => Expression.Parameter(p.ParameterType))
.ToArray(),
rst.GetMethod("Invoke") is { } invoke
? invoke.GetParameters()
.Select(p => Expression.Parameter(p.ParameterType))
.ToArray()
: throw new Exception("""Method "Invoke" not found."""),
}
into m
let resultSelector =
Expand Down Expand Up @@ -96,7 +97,7 @@ into t
select new TestCaseData(t.Method, t.Args).SetName(t.Name).Returns(t.Expectation);

[TestCaseSource(nameof(AccumulatorsTestSource), new object[] { nameof(Accumulators), 10 })]
public object Accumulators(MethodInfo method, object[] args) =>
public object? Accumulators(MethodInfo method, object[] args) =>
method.Invoke(null, args);

[Test]
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq.Test/AppendTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void AppendWithEmptyHeadSequence()
public void AppendWithNullTail()
{
var head = new[] { "first", "second" };
string tail = null;
string? tail = null;
var whole = head.Append(tail);
whole.AssertSequenceEqual("first", "second", null);
}
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq.Test/AssertCountTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void AssertCountIsLazy()
[Test]
public void AssertCountWithCollectionIsLazy()
{
new BreakingCollection<object>(5).AssertCount(0);
new BreakingCollection<int>(new int[5]).AssertCount(0);
}

[Test]
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq.Test/AssertTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void AssertSequenceWithValidSomeInvalidElements()
public void AssertSequenceWithInvalidElementsAndCustomErrorReturningNull()
{
var source = new[] { 2, 4, 6, 7, 8, 9 };
Assert.That(() => source.Assert(n => n % 2 == 0, _ => null).Consume(),
Assert.That(() => source.Assert(n => n % 2 == 0, _ => null!).Consume(),
Throws.InvalidOperationException);
}

Expand Down
26 changes: 15 additions & 11 deletions MoreLinq.Test/Async/AsyncEnumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public static IAsyncEnumerable<TSource> Distinct<TSource>(this IAsyncEnumerable<
public static ValueTask<TSource> ElementAtAsync<TSource>(this IAsyncEnumerable<TSource> source, int index) =>
LinqEnumerable.ElementAtAsync(source, index);

public static ValueTask<TSource> ElementAtOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, int index) =>
public static ValueTask<TSource?> ElementAtOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, int index) =>
LinqEnumerable.ElementAtOrDefaultAsync(source, index);

public static IAsyncEnumerable<TResult> Empty<TResult>() =>
Expand All @@ -159,10 +159,10 @@ public static ValueTask<TSource> FirstAsync<TSource>(this IAsyncEnumerable<TSour
public static ValueTask<TSource> FirstAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate) =>
LinqEnumerable.FirstAsync(source, predicate);

public static ValueTask<TSource> FirstOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source) =>
public static ValueTask<TSource?> FirstOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source) =>
LinqEnumerable.FirstOrDefaultAsync(source);

public static ValueTask<TSource> FirstOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate) =>
public static ValueTask<TSource?> FirstOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate) =>
LinqEnumerable.FirstOrDefaultAsync(source, predicate);

public static IAsyncEnumerable<TResult> GroupBy<TSource, TKey, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, IAsyncEnumerable<TSource>, TResult> resultSelector, IEqualityComparer<TKey> comparer) =>
Expand Down Expand Up @@ -213,10 +213,10 @@ public static ValueTask<TSource> LastAsync<TSource>(this IAsyncEnumerable<TSourc
public static ValueTask<TSource> LastAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate) =>
LinqEnumerable.LastAsync(source, predicate);

public static ValueTask<TSource> LastOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source) =>
public static ValueTask<TSource?> LastOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source) =>
LinqEnumerable.LastOrDefaultAsync(source);

public static ValueTask<TSource> LastOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate) =>
public static ValueTask<TSource?> LastOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate) =>
LinqEnumerable.LastOrDefaultAsync(source, predicate);

public static ValueTask<long> LongCountAsync<TSource>(this IAsyncEnumerable<TSource> source) =>
Expand Down Expand Up @@ -411,10 +411,10 @@ public static ValueTask<TSource> SingleAsync<TSource>(this IAsyncEnumerable<TSou
public static ValueTask<TSource> SingleAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate) =>
LinqEnumerable.SingleAsync(source, predicate);

public static ValueTask<TSource> SingleOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source) =>
public static ValueTask<TSource?> SingleOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source) =>
LinqEnumerable.SingleOrDefaultAsync(source);

public static ValueTask<TSource> SingleOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate) =>
public static ValueTask<TSource?> SingleOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate) =>
LinqEnumerable.SingleOrDefaultAsync(source, predicate);

public static IAsyncEnumerable<TSource> Skip<TSource>(this IAsyncEnumerable<TSource> source, int count) =>
Expand Down Expand Up @@ -510,16 +510,20 @@ public static IOrderedAsyncEnumerable<TSource> ThenByDescending<TSource, TKey>(t
public static ValueTask<TSource[]> ToArrayAsync<TSource>(this IAsyncEnumerable<TSource> source) =>
LinqEnumerable.ToArrayAsync(source);

public static ValueTask<Dictionary<TKey, TSource>> ToDictionaryAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector) =>
public static ValueTask<Dictionary<TKey, TSource>> ToDictionaryAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
where TKey: notnull =>
LinqEnumerable.ToDictionaryAsync(source, keySelector);

public static ValueTask<Dictionary<TKey, TSource>> ToDictionaryAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer) =>
public static ValueTask<Dictionary<TKey, TSource>> ToDictionaryAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
where TKey: notnull =>
LinqEnumerable.ToDictionaryAsync(source, keySelector, comparer);

public static ValueTask<Dictionary<TKey, TElement>> ToDictionaryAsync<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector) =>
public static ValueTask<Dictionary<TKey, TElement>> ToDictionaryAsync<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
where TKey: notnull =>
LinqEnumerable.ToDictionaryAsync(source, keySelector, elementSelector);

public static ValueTask<Dictionary<TKey, TElement>> ToDictionaryAsync<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer) =>
public static ValueTask<Dictionary<TKey, TElement>> ToDictionaryAsync<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)
where TKey: notnull =>
LinqEnumerable.ToDictionaryAsync(source, keySelector, elementSelector, comparer);

public static ValueTask<List<TSource>> ToListAsync<TSource>(this IAsyncEnumerable<TSource> source) =>
Expand Down
12 changes: 5 additions & 7 deletions MoreLinq.Test/BatchTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ public void BatchBucketSelectorCurrentList()
{
var input = TestingSequence.Of(1, 2, 3, 4, 5, 6, 7, 8, 9);
using var pool = new TestArrayPool<int>();
int[] bucketSelectorItems = null;
int[]? bucketSelectorItems = null;

var result = input.Batch(4, pool, current => bucketSelectorItems = current.ToArray(), _ => 0);

Expand All @@ -356,18 +356,16 @@ public void BatchBucketSelectorCurrentList()

sealed class TestArrayPool<T> : ArrayPool<T>, IDisposable
{
T[] _pooledArray;
T[] _rentedArray;
T[]? _pooledArray;
T[]? _rentedArray;

public override T[] Rent(int minimumLength)
{
if (_pooledArray is null && _rentedArray is null)
_pooledArray = new T[minimumLength * 2];

if (_pooledArray is null)
throw new InvalidOperationException("The pool is exhausted.");

(_pooledArray, _rentedArray) = (null, _pooledArray);
(_pooledArray, _rentedArray) =
(null, _pooledArray ?? throw new InvalidOperationException("The pool is exhausted."));

return _rentedArray;
}
Expand Down
2 changes: 0 additions & 2 deletions MoreLinq.Test/BreakingCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ class BreakingCollection<T> : BreakingSequence<T>, ICollection<T>

public BreakingCollection(params T[] values) : this ((IList<T>) values) {}
public BreakingCollection(IList<T> list) => List = list;
public BreakingCollection(int count) :
this(Enumerable.Repeat(default(T), count).ToList()) {}

public int Count => List.Count;

Expand Down
9 changes: 8 additions & 1 deletion MoreLinq.Test/BreakingSequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ namespace MoreLinq.Test
/// </summary>
class BreakingSequence<T> : IEnumerable<T>
{
public IEnumerator<T> GetEnumerator() => throw new InvalidOperationException();
public IEnumerator<T> GetEnumerator() => throw new BreakException();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

sealed class BreakException : Exception
{
public BreakException() { }
public BreakException(string message) : base(message) { }
public BreakException(string message, Exception inner) : base(message, inner) { }
}
}
2 changes: 1 addition & 1 deletion MoreLinq.Test/ChooseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ static class Option

static class Option<T>
{
public static readonly (bool IsSome, T Value) None = (false, default);
public static readonly (bool IsSome, T Value) None = default;
}

[Test]
Expand Down
8 changes: 4 additions & 4 deletions MoreLinq.Test/CompareCountTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void CompareCountWithCollectionAndSequence(int collectionCount,
int expectedCompareCount,
int expectedMoveNextCallCount)
{
var collection = new BreakingCollection<int>(collectionCount);
var collection = new BreakingCollection<int>(new int[collectionCount]);

using var seq = Enumerable.Range(0, sequenceCount).AsTestingSequence();

Expand All @@ -70,7 +70,7 @@ public void CompareCountWithSequenceAndCollection(int sequenceCount,
int expectedCompareCount,
int expectedMoveNextCallCount)
{
var collection = new BreakingCollection<int>(collectionCount);
var collection = new BreakingCollection<int>(new int[collectionCount]);

using var seq = Enumerable.Range(0, sequenceCount).AsTestingSequence();

Expand Down Expand Up @@ -107,7 +107,7 @@ public void CompareCountDisposesSequenceEnumerators()
[Test]
public void CompareCountDisposesFirstEnumerator()
{
var collection = new BreakingCollection<int>(0);
var collection = new BreakingCollection<int>();

using var seq = TestingSequence.Of<int>();

Expand All @@ -117,7 +117,7 @@ public void CompareCountDisposesFirstEnumerator()
[Test]
public void CompareCountDisposesSecondEnumerator()
{
var collection = new BreakingCollection<int>(0);
var collection = new BreakingCollection<int>();

using var seq = TestingSequence.Of<int>();

Expand Down
8 changes: 4 additions & 4 deletions MoreLinq.Test/Comparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ sealed class Comparer
/// <see cref="Func{T,T,Int32}"/>.
/// </summary>

public static IComparer<T> Create<T>(Func<T, T, int> compare) =>
public static IComparer<T> Create<T>(Func<T?, T?, int> compare) =>
new DelegatingComparer<T>(compare);

sealed class DelegatingComparer<T> : IComparer<T>
{
readonly Func<T, T, int> _comparer;
readonly Func<T?, T?, int> _comparer;

public DelegatingComparer(Func<T, T, int> comparer)
public DelegatingComparer(Func<T?, T?, int> comparer)
{
_comparer = comparer ?? throw new ArgumentNullException(nameof(comparer));
}

public int Compare(T x, T y) => _comparer(x, y);
public int Compare(T? x, T? y) => _comparer(x, y);
}
}
}
16 changes: 8 additions & 8 deletions MoreLinq.Test/CountByTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ public void CountByWithSomeNullKeys()
var result = ss.CountBy(s => s);

result.AssertSequenceEqual(
KeyValuePair.Create("foo", 2),
KeyValuePair.Create((string) null, 4),
KeyValuePair.Create("bar", 2),
KeyValuePair.Create("baz", 2));
KeyValuePair.Create((string?)"foo", 2),
KeyValuePair.Create((string?)null, 4),
KeyValuePair.Create((string?)"bar", 2),
KeyValuePair.Create((string?)"baz", 2));
}

[Test]
Expand All @@ -110,10 +110,10 @@ public void CountByWithSomeNullKeysAndEqualityComparer()
var result = new[] { "a", "B", null, "c", "A", null, "b", "A" }.CountBy(c => c, StringComparer.OrdinalIgnoreCase);

result.AssertSequenceEqual(
KeyValuePair.Create("a", 3),
KeyValuePair.Create("B", 2),
KeyValuePair.Create((string)null, 2),
KeyValuePair.Create("c", 1));
KeyValuePair.Create((string?)"a", 3),
KeyValuePair.Create((string?)"B", 2),
KeyValuePair.Create((string?)null, 2),
KeyValuePair.Create((string?)"c", 1));
}
}
}
10 changes: 5 additions & 5 deletions MoreLinq.Test/CountDownTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,14 @@ static class TestCollection
{
public static ICollection<T>
Create<T>(ICollection<T> collection,
Func<IEnumerator<T>, IEnumerator<T>> em = null)
Func<IEnumerator<T>, IEnumerator<T>>? em = null)
{
return new Collection<T>(collection, em);
}

public static IReadOnlyCollection<T>
CreateReadOnly<T>(ICollection<T> collection,
Func<IEnumerator<T>, IEnumerator<T>> em = null)
Func<IEnumerator<T>, IEnumerator<T>>? em = null)
{
return new ReadOnlyCollection<T>(collection, em);
}
Expand All @@ -154,7 +154,7 @@ abstract class Sequence<T> : IEnumerable<T>
{
readonly Func<IEnumerator<T>, IEnumerator<T>> _em;

protected Sequence(Func<IEnumerator<T>, IEnumerator<T>> em) =>
protected Sequence(Func<IEnumerator<T>, IEnumerator<T>>? em) =>
_em = em ?? (e => e);

public IEnumerator<T> GetEnumerator() =>
Expand All @@ -175,7 +175,7 @@ sealed class Collection<T> : Sequence<T>, ICollection<T>
readonly ICollection<T> _collection;

public Collection(ICollection<T> collection,
Func<IEnumerator<T>, IEnumerator<T>> em = null) :
Func<IEnumerator<T>, IEnumerator<T>>? em = null) :
base(em) =>
_collection = collection ?? throw new ArgumentNullException(nameof(collection));

Expand All @@ -202,7 +202,7 @@ sealed class ReadOnlyCollection<T> : Sequence<T>, IReadOnlyCollection<T>
readonly ICollection<T> _collection;

public ReadOnlyCollection(ICollection<T> collection,
Func<IEnumerator<T>, IEnumerator<T>> em = null) :
Func<IEnumerator<T>, IEnumerator<T>>? em = null) :
base(em) =>
_collection = collection ?? throw new ArgumentNullException(nameof(collection));

Expand Down
Loading