From f7668273de76bcbdbecc95b0cb8f616a90479646 Mon Sep 17 00:00:00 2001 From: Atif Aziz Date: Sat, 6 May 2023 10:42:52 +0200 Subject: [PATCH] Fix bug in "FillForward" when filler is null --- MoreLinq.Test/FillForwardTest.cs | 13 +++++++++++++ MoreLinq/FillForward.cs | 6 +++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/MoreLinq.Test/FillForwardTest.cs b/MoreLinq.Test/FillForwardTest.cs index bd156f096..97c68f563 100644 --- a/MoreLinq.Test/FillForwardTest.cs +++ b/MoreLinq.Test/FillForwardTest.cs @@ -83,5 +83,18 @@ select line.Trim() into line new { Continent = "Africa", Country = "Kenya", City = "Nairobi", Value = 901 }, })); } + + /// + /// Exercises bug reported in issue #999. + /// + + [Test] + public void FillForwardWithNullFiller() + { + var input = new int?[] { null, 1, null, 2, null, 3, null }; + var result = input.FillForward(x => x is not null); + result.AssertSequenceEqual(Enumerable.Repeat((int?)null, input.Length)); + } } } diff --git a/MoreLinq/FillForward.cs b/MoreLinq/FillForward.cs index 6d304a366..a38faae49 100644 --- a/MoreLinq/FillForward.cs +++ b/MoreLinq/FillForward.cs @@ -112,10 +112,10 @@ static IEnumerable FillForwardImpl(IEnumerable source, Func pr { if (predicate(item)) { - yield return seed is (true, { } someSeed) + yield return seed is (true, var theSeed) ? fillSelector != null - ? fillSelector(item, someSeed) - : someSeed + ? fillSelector(item, theSeed) + : theSeed : item; } else