dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.98k stars 4.03k forks source link

"Dereference of a possibly null reference" on EF Core's ThenInclude #41812

Open qrjo opened 4 years ago

qrjo commented 4 years ago

Version Used: .NET Core 3.1 with Microsoft.EntityFrameworkCore 3.1.1, nullable reference types enabled.

Steps to Reproduce:

using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query;

namespace TestApplication
{
    public static class Test
    {
        public static void Run()
        {
            IQueryable<A> query = new A[0].AsQueryable();
            IIncludableQueryable<A, C?> result = query.Include(a => a.NavProperty).ThenInclude(b => b.NavProperty);
        }
    }

    public sealed class A
    {
        public B? NavProperty { get; set; }
    }

    public sealed class B
    {
        public C? NavProperty { get; set; }
    }

    public sealed class C
    {
    }
}

This code will result in a Dereference of a possibly null reference warning on the b => b.NavProperty expression. This means ThenInclude requires either suppressing this warning globally (which takes away the useful occurrences of the warning), suppressing it locally (which will turn the code into a mess) or living with way too many warnings in an otherwise warning free solution (not workable). The null conditional operator that would be used in regular code is not available in expressions.

Expected Behavior:

Actual Behavior: Dereference of a possibly null reference warning.

sharwell commented 4 years ago

Related to dotnet/csharplang#2545