Skip to content

Create DiagnosticSuppressor for CS8602 in Linq expressions and Includes #21663

@Dreamescaper

Description

@Dreamescaper

Currently CS8602 (Dereference of possibly null reference) is triggered for nullable properties in linq expressions, even though no NRE possible when query is executed on DB side.

Example 1 (Linq):

class User
{
    public string FirstName {get;set;}
    public string? MiddleName {get;set;}
    public string LastName {get;set;} 
}
...
string searchTerm = "...";

await dbContext.Users
    .Where(u => u => u.FirstName.Contains(searchTerm)
          || u.MiddleName.Contains(searchTerm)       <---------- CS8602 is reported, as 'MiddleName' is nullable
          || u.LastName.Contains(searchTerm))
    .ToListAsync();

Example 2 (Includes):

class SomeModel
{
    public class NavigationModel Navigation? {get;set;}
}

class NavigationModel 
{
    public class AnotherNavigationModel DeeperNavigation {get;set;}
}

dbContext.Models
   .Include(m => m.Navigation)
        .ThenInclude(n => n.DeeperNavigation)   <----- CS8602 is reported, as 'n' is nullable

Suggestion:
Create DiagnosticSuppressor for CS8602 for IQueryable expressions in Linq methods and for Include/ThenInclude methods.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions