morelinq / MoreLINQ

Extensions to LINQ to Objects
https://morelinq.github.io/
Apache License 2.0
3.67k stars 412 forks source link

Fix nullability of `ToDataTable` expressions argument #914

Closed atifaziz closed 1 year ago

atifaziz commented 1 year ago

This fixes the nullability of ToDataTable expressions argument, adding to #803. The actual and possibly boxed return type of each expression (object) is not really used; the actual type of the accessed property is. As a result, there is no requirement that the property type not be nullable and in fact can be limiting. This PR allows expressions to return object?.

Suppose the following record:

sealed record Person
{
    public required string FirstName { get; init; }
    public string? MiddleName { get; init; }
    public required string LastName { get; init; }
}

Before this PR, the following will issue a nullability warning (CS8603) at the line marked with the comment:

var people = new[]
{
    new Person { FirstName = "John", LastName = "Doe" }
};

var dt = people.ToDataTable(p => p.FirstName,
                            p => p.MiddleName, // CS8603 - Possible null reference return
                            p => p.LastName);

After this PR, the warning disappears.

codecov[bot] commented 1 year ago

Codecov Report

Merging #914 (69cc931) into master (79a2794) will not change coverage. The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master     #914   +/-   ##
=======================================
  Coverage   92.39%   92.39%           
=======================================
  Files         112      112           
  Lines        3444     3444           
  Branches     1021     1021           
=======================================
  Hits         3182     3182           
  Misses        200      200           
  Partials       62       62           
Impacted Files Coverage Δ
MoreLinq/ToDataTable.cs 96.87% <100.00%> (ø)

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more