diff --git a/MoreLinq.Test/AggregateTest.cs b/MoreLinq.Test/AggregateTest.cs index 04ec29154..bd2bafe7c 100644 --- a/MoreLinq.Test/AggregateTest.cs +++ b/MoreLinq.Test/AggregateTest.cs @@ -97,7 +97,9 @@ from pair in pairs into t select new TestCaseData(t.Method, t.Args).SetName(t.Name).Returns(t.Expectation); - [TestCaseSource(nameof(AccumulatorsTestSource), new object[] { nameof(Accumulators), 10 })] +#pragma warning disable NUnit1018 // Parameter count does not match (false negative) + [TestCaseSource(nameof(AccumulatorsTestSource), [nameof(Accumulators), 10])] +#pragma warning restore NUnit1018 // Parameter count does not match public object? Accumulators(MethodInfo method, object[] args) => method.Invoke(null, args); diff --git a/MoreLinq.Test/BacksertTest.cs b/MoreLinq.Test/BacksertTest.cs index dbc985903..1a093754f 100644 --- a/MoreLinq.Test/BacksertTest.cs +++ b/MoreLinq.Test/BacksertTest.cs @@ -32,7 +32,7 @@ public void BacksertIsLazy() [Test] public void BacksertWithNegativeIndex() { - Assert.That(() => Enumerable.Range(1, 10).Backsert(new[] { 97, 98, 99 }, -1), + Assert.That(() => Enumerable.Range(1, 10).Backsert([97, 98, 99], -1), Throws.ArgumentOutOfRangeException("index")); } diff --git a/MoreLinq.Test/CountDownTest.cs b/MoreLinq.Test/CountDownTest.cs index e852a3e0c..06b0b8434 100644 --- a/MoreLinq.Test/CountDownTest.cs +++ b/MoreLinq.Test/CountDownTest.cs @@ -150,12 +150,9 @@ public static IReadOnlyCollection /// for another. /// - abstract class Sequence : IEnumerable + abstract class Sequence(Func, IEnumerator>? em) : IEnumerable { - readonly Func, IEnumerator> em; - - protected Sequence(Func, IEnumerator>? em) => - this.em = em ?? (e => e); + readonly Func, IEnumerator> em = em ?? (e => e); public IEnumerator GetEnumerator() => this.em(Items.GetEnumerator()); diff --git a/MoreLinq.Test/EndsWithTest.cs b/MoreLinq.Test/EndsWithTest.cs index 11add633c..9ebce0a62 100644 --- a/MoreLinq.Test/EndsWithTest.cs +++ b/MoreLinq.Test/EndsWithTest.cs @@ -52,13 +52,13 @@ public bool EndsWithWithStrings(string first, string second) [Test] public void EndsWithReturnsTrueIfBothEmpty() { - Assert.That(new int[0].EndsWith(new int[0]), Is.True); + Assert.That(new int[0].EndsWith([]), Is.True); } [Test] public void EndsWithReturnsFalseIfOnlyFirstIsEmpty() { - Assert.That(new int[0].EndsWith(new[] { 1, 2, 3 }), Is.False); + Assert.That(new int[0].EndsWith([1, 2, 3]), Is.False); } [TestCase("", "", ExpectedResult = true)] diff --git a/MoreLinq.Test/Enumerable.cs b/MoreLinq.Test/Enumerable.cs index f7795af4e..a63220274 100644 --- a/MoreLinq.Test/Enumerable.cs +++ b/MoreLinq.Test/Enumerable.cs @@ -145,7 +145,9 @@ public static TSource ElementAt(this IEnumerable source, int i LinqEnumerable.ElementAtOrDefault(source, index); public static IEnumerable Empty() => +#pragma warning disable IDE0301 // Simplify collection initialization LinqEnumerable.Empty(); +#pragma warning restore IDE0301 // Simplify collection initialization public static IEnumerable Except(this IEnumerable first, IEnumerable second) => LinqEnumerable.Except(first, second); diff --git a/MoreLinq.Test/InsertTest.cs b/MoreLinq.Test/InsertTest.cs index 1dd148bd0..b7cb97ede 100644 --- a/MoreLinq.Test/InsertTest.cs +++ b/MoreLinq.Test/InsertTest.cs @@ -25,7 +25,7 @@ public class InsertTest [Test] public void InsertWithNegativeIndex() { - Assert.That(() => Enumerable.Range(1, 10).Insert(new[] { 97, 98, 99 }, -1), + Assert.That(() => Enumerable.Range(1, 10).Insert([97, 98, 99], -1), Throws.ArgumentOutOfRangeException("index")); } diff --git a/MoreLinq.Test/ReturnTest.cs b/MoreLinq.Test/ReturnTest.cs index 769d7168d..cf8494047 100644 --- a/MoreLinq.Test/ReturnTest.cs +++ b/MoreLinq.Test/ReturnTest.cs @@ -148,7 +148,9 @@ static IEnumerable UnsupportedActions(string testName) => } select new TestCaseData(ma.Action).SetName($"{testName}({ma.MethodName})"); - [TestCaseSource(nameof(UnsupportedActions), new object[] { nameof(TestUnsupportedMethodShouldThrow) })] +#pragma warning disable NUnit1018 // Parameter count does not match (false negative) + [TestCaseSource(nameof(UnsupportedActions), [nameof(TestUnsupportedMethodShouldThrow)])] +#pragma warning restore NUnit1018 // Parameter count does not match public void TestUnsupportedMethodShouldThrow(Action unsupportedAction) { Assert.That(() => unsupportedAction(), Throws.InstanceOf()); diff --git a/MoreLinq.Test/SampleData.cs b/MoreLinq.Test/SampleData.cs index 6e257d060..a7861226d 100644 --- a/MoreLinq.Test/SampleData.cs +++ b/MoreLinq.Test/SampleData.cs @@ -25,15 +25,11 @@ namespace MoreLinq.Test /// static class SampleData { - internal static readonly ReadOnlyCollection Strings = new(new[] - { - "ax", "hello", "world", "aa", "ab", "ay", "az" - }); + internal static readonly ReadOnlyCollection + Strings = new(["ax", "hello", "world", "aa", "ab", "ay", "az"]); - internal static readonly ReadOnlyCollection Values = new(new[] - { - 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 - }); + internal static readonly ReadOnlyCollection + Values = new([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); internal static readonly Func Plus = (a, b) => a + b; internal static readonly Func Mul = (a, b) => a * b; diff --git a/MoreLinq.Test/Scope.cs b/MoreLinq.Test/Scope.cs index b770bc14f..9d847681f 100644 --- a/MoreLinq.Test/Scope.cs +++ b/MoreLinq.Test/Scope.cs @@ -19,12 +19,9 @@ namespace MoreLinq.Test { using System; - abstract class Scope : IDisposable + abstract class Scope(T current) : IDisposable { - readonly T old; - - protected Scope(T current) => this.old = current; - public virtual void Dispose() => Restore(this.old); + public virtual void Dispose() => Restore(current); protected abstract void Restore(T old); } } diff --git a/MoreLinq.Test/StartsWithTest.cs b/MoreLinq.Test/StartsWithTest.cs index cb4148b52..a252072d1 100644 --- a/MoreLinq.Test/StartsWithTest.cs +++ b/MoreLinq.Test/StartsWithTest.cs @@ -52,13 +52,13 @@ public bool StartsWithWithStrings(string first, string second) [Test] public void StartsWithReturnsTrueIfBothEmpty() { - Assert.That(new int[0].StartsWith(new int[0]), Is.True); + Assert.That(new int[0].StartsWith([]), Is.True); } [Test] public void StartsWithReturnsFalseIfOnlyFirstIsEmpty() { - Assert.That(new int[0].StartsWith(new[] { 1, 2, 3 }), Is.False); + Assert.That(new int[0].StartsWith([1, 2, 3]), Is.False); } [TestCase("", "", ExpectedResult = true)] diff --git a/MoreLinq.Test/TestExtensions.cs b/MoreLinq.Test/TestExtensions.cs index 706641f1b..76d6869c6 100644 --- a/MoreLinq.Test/TestExtensions.cs +++ b/MoreLinq.Test/TestExtensions.cs @@ -33,22 +33,19 @@ public enum SourceKind static class SourceKinds { - public static readonly IEnumerable Sequence = new[] - { - SourceKind.Sequence, - }; + public static readonly IEnumerable Sequence = [ + SourceKind.Sequence + ]; - public static readonly IEnumerable Collection = new[] - { + public static readonly IEnumerable Collection = [ SourceKind.BreakingCollection, SourceKind.BreakingReadOnlyCollection - }; + ]; - public static readonly IEnumerable List = new[] - { + public static readonly IEnumerable List = [ SourceKind.BreakingList, SourceKind.BreakingReadOnlyList - }; + ]; } static partial class TestExtensions diff --git a/MoreLinq.Test/TraverseTest.cs b/MoreLinq.Test/TraverseTest.cs index 89a0b2277..7a02f264f 100644 --- a/MoreLinq.Test/TraverseTest.cs +++ b/MoreLinq.Test/TraverseTest.cs @@ -38,7 +38,7 @@ public void TraverseBreadthFirstIsStreaming() [Test] public void TraverseDepthFirstPreservesChildrenOrder() { - var res = MoreEnumerable.TraverseDepthFirst(0, i => i == 0 ? Enumerable.Range(1, 10) : Enumerable.Empty()); + var res = MoreEnumerable.TraverseDepthFirst(0, i => i == 0 ? Enumerable.Range(1, 10) : []); res.AssertSequenceEqual(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10); } @@ -46,7 +46,7 @@ public void TraverseDepthFirstPreservesChildrenOrder() [Test] public void TraverseBreadthFirstPreservesChildrenOrder() { - var res = MoreEnumerable.TraverseBreadthFirst(0, i => i == 0 ? Enumerable.Range(1, 10) : Enumerable.Empty()); + var res = MoreEnumerable.TraverseBreadthFirst(0, i => i == 0 ? Enumerable.Range(1, 10) : []); res.AssertSequenceEqual(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10); } diff --git a/MoreLinq/FullGroupJoin.cs b/MoreLinq/FullGroupJoin.cs index 7ce9b687f..2f6fb56c8 100644 --- a/MoreLinq/FullGroupJoin.cs +++ b/MoreLinq/FullGroupJoin.cs @@ -19,7 +19,6 @@ namespace MoreLinq { using System; using System.Collections.Generic; - using System.Linq; // Inspiration & credit: http://stackoverflow.com/a/13503860/6682 static partial class MoreEnumerable @@ -162,7 +161,7 @@ IEnumerable _(IEqualityComparer comparer) if (alookup.Contains(b.Key)) continue; // We can skip the lookup because we are iterating over keys not found in the first sequence - yield return resultSelector(b.Key, Enumerable.Empty(), b); + yield return resultSelector(b.Key, [], b); } } } diff --git a/MoreLinq/Maxima.cs b/MoreLinq/Maxima.cs index 5292dbcf8..645ea374d 100644 --- a/MoreLinq/Maxima.cs +++ b/MoreLinq/Maxima.cs @@ -232,7 +232,7 @@ IEnumerator IEnumerable.GetEnumerator() => public IEnumerable Take(int count) => count switch { - 0 => Enumerable.Empty(), + 0 => [], 1 => ExtremaBy(source, Extremum.First, 1 , selector, comparer), _ => ExtremaBy(source, Extrema.First , count, selector, comparer) }; @@ -240,7 +240,7 @@ public IEnumerable Take(int count) => public IEnumerable TakeLast(int count) => count switch { - 0 => Enumerable.Empty(), + 0 => [], 1 => ExtremaBy(source, Extremum.Last, 1 , selector, comparer), _ => ExtremaBy(source, Extrema.Last , count, selector, comparer) }; @@ -290,7 +290,7 @@ sealed class Extremum : Extrema<(bool, T), T> public override void Restart(ref (bool, T) store) => store = default; public override IEnumerable GetEnumerable((bool, T) store) => - store is (true, var item) ? Enumerable.Repeat(item, 1) : Enumerable.Empty(); + store is (true, var item) ? [item] : []; public override void Add(ref (bool, T) store, int? limit, T item) { @@ -320,7 +320,7 @@ IEnumerable Extrema() using var e = source.GetEnumerator(); if (!e.MoveNext()) - return new List(); + return []; var store = extrema.New(); extrema.Add(ref store, limit, e.Current); diff --git a/MoreLinq/Partition.cs b/MoreLinq/Partition.cs index 9660186fc..5c99b997f 100644 --- a/MoreLinq/Partition.cs +++ b/MoreLinq/Partition.cs @@ -351,12 +351,7 @@ static TResult PartitionImpl(IEnumerable>? etc = null; - var groups = new[] - { - Enumerable.Empty(), - Enumerable.Empty(), - Enumerable.Empty(), - }; + var groups = new IEnumerable[] { [], [], [] }; foreach (var e in source) { diff --git a/MoreLinq/TakeLast.cs b/MoreLinq/TakeLast.cs index 88eac7a8e..c20d40f47 100644 --- a/MoreLinq/TakeLast.cs +++ b/MoreLinq/TakeLast.cs @@ -54,7 +54,7 @@ public static IEnumerable TakeLast(this IEnumerable s { if (source == null) throw new ArgumentNullException(nameof(source)); - return count < 1 ? Enumerable.Empty() + return count < 1 ? [] : source.CountDown(count, (e, cd) => (Element: e, Countdown: cd)) .SkipWhile(e => e.Countdown == null) .Select(e => e.Element); diff --git a/bld/ExtensionsGenerator/Program.cs b/bld/ExtensionsGenerator/Program.cs index 35dae759d..ec466df30 100644 --- a/bld/ExtensionsGenerator/Program.cs +++ b/bld/ExtensionsGenerator/Program.cs @@ -357,11 +357,9 @@ select Walk(te.Type))), // - Each type parameter (recursively) // -abstract class TypeKey : IComparable +abstract class TypeKey(string name) : IComparable { - protected TypeKey(string name) => Name = name; - - public string Name { get; } + public string Name { get; } = name; public abstract ImmutableList Parameters { get; } public virtual int CompareTo(TypeKey? other) @@ -383,18 +381,15 @@ protected static int Compare(IEnumerable a, IEnumerable b) => sealed class SimpleTypeKey(string name) : TypeKey(name) { public override string ToString() => Name; - public override ImmutableList Parameters => ImmutableList.Empty; + public override ImmutableList Parameters => []; } -abstract class ParameterizedTypeKey : TypeKey +abstract class ParameterizedTypeKey(string name, ImmutableList parameters) : TypeKey(name) { protected ParameterizedTypeKey(string name, TypeKey parameter) : this(name, [parameter]) { } - protected ParameterizedTypeKey(string name, ImmutableList parameters) : - base(name) => Parameters = parameters; - - public override ImmutableList Parameters { get; } + public override ImmutableList Parameters { get; } = parameters; } sealed class GenericTypeKey(string name, ImmutableList parameters) :