Skip to content

Create DiagnosticSuppressor for CS8602 in Linq expressions and Includes #21663

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.

Activity

  1. Dreamescaper commented on Jul 17, 2020

    @Dreamescaper
    Author

    Related to #21608

  2. smitpatel commented on Jul 17, 2020

    @smitpatel
    Contributor

    This may not be a good idea. While I understand the logic behind it, it is hard to tell if the LINQ query is being run over EF here null safety won't matter vs objects where code which is null unsafe will certainly throw an error if evaluated.

  3. Dreamescaper commented on Jul 17, 2020

    @Dreamescaper
    Author

    @smitpatel
    I assume it relates to Linq case only?
    WDYT about Includes? It should always be safe to suppress warnings there, right?

  4. roji commented on Jul 17, 2020

    @roji
    Member

    I'm guessing/hoping there are some cases where we can unambiguously determine we're in an EF-evaluated expression (at least in Includes as @Dreamescaper suggested, possibly even other cases)... We can look into this.

  5. Dreamescaper commented on Jul 17, 2020

    @Dreamescaper
    Author

    For Linq case - we can try suppression only when expression starts with DbSet.
    E.g.

    await dbContext.Users
        .Where(u => u => u.FirstName.Contains(searchTerm)
              || u.MiddleName.Contains(searchTerm) // <---------- query starts from DbSet<User>, suppress warning
              || u.LastName.Contains(searchTerm))
        .ToListAsync();

    but

    IQueryable<UserProfile> SearchQuery(IQueryable<User> query, string searchTerm)
    {
         return query.Where(up => up.FirstName.Contains(searchTerm)
            || up.MiddleName.Contains(searchTerm)  // <------------ can't know if IQueryable is EF Core specific, so do not suppress warning
            || up.LastName.Contains(searchTerm);
    }
  6. ajcvickers commented on Jul 17, 2020

    @ajcvickers
    Contributor

    Note from triage: we're not going to put any DiagnosticSuppressors in for 5.0. We should discuss post-5.0 whether this is something that we should invest in.

  7. added this to the Backlog milestone on Jul 17, 2020
  8. Youssef1313 commented on Feb 13, 2021

    @Youssef1313
    Member

    @ajcvikers Are there any plans for this suppressor? I'm interested to implement it and submit a PR.

  9. 4 remaining items

  10. roji commented on Feb 13, 2021

    @roji
    Member

    I agree with that - any correct suppressions we do are useful (even if we don't cover all cases), but we shouldn't incorrectly suppress any diagnostic.

  11. Youssef1313 commented on Feb 14, 2021

    @Youssef1313
    Member

    @roji Does working with the solution require the current preview version of Visual Studio or any special requirements?

    I'm getting several warnings with VS 16.8:

    image

  12. ErikEJ commented on Feb 14, 2021

    @ErikEJ
    Contributor
  13. roji commented on Feb 17, 2021

    @roji
    Member

    Design discussion:

    • If we suppress NRE warnings in EF-rooted expression trees, we risk suppressing real warnings for code that will get client-evaluated
    • It is not possible to reliably identify client-evaluated code; the top-level select is only one instance, but it is possible for earlier operators to contain bits that will end up being client-evaluated as well.
    • As such, we will make no attempt to identify client-evaluation, and apply the suppression to the entire EF-rooted tree.
    • Since we'd be suppressing potentially valuable warnings, we want to explore opt-in/opt-out possibilities here.
    • Ideally, we'd be able to suppress the warning, but emit a compilation suggestion with a clear, EF-specific text describing the situation, and directing users what to do.
    • An editorconfig knob could allow removing the suggestion entirely, or removing the suppression, making the original warning show (as today).
    • If it's not possible to downgrade the warning to a suggestion, then we need to reach a decision on whether the complete suppression should be the default or opt-in.
  14. Youssef1313 commented on Feb 17, 2021

    @Youssef1313
    Member

    @roji With the current Roslyn APIs, I can't see a way to have a suppression disabled by default to be opt-in. But that sounds like a reasonable feature request for Roslyn. Opened dotnet/roslyn#51278 for that in Roslyn.

  15. roji commented on Feb 17, 2021

    @roji
    Member

    @Youssef1313 the suppressor can simply decide not to call ReportSuppression based on some flag in .editorconfig or elsewhere, no?

  16. roji commented on Feb 17, 2021

    @roji
    Member

    Ideally, we'd be able to suppress the warning, but emit a compilation suggestion with a clear, EF-specific text describing the situation, and directing users what to do.

    A quick look seems to show that a suppressor cannot report new diagnostics, nor mutate existing ones - only suppress.

  17. roji commented on Feb 24, 2021

    @roji
    Member

    Design decision: as there's no way for a suppressor to report new diagnostics, we'll suppress these warnings in all DbSet-rooted expression trees by default. An .editorconfig opt-in should allow opting back in to the warnings, effectively disabling the suppressor.

    @Youssef1313 I think we know what we want to do here - you can continue work on this.

  18. roji commented on Feb 24, 2021

    @roji
    Member
  19. added theissue type on Jun 15, 2025
  20. johnwc commented on Jul 8, 2025

    @johnwc
  21. roji commented on Jul 9, 2025

    @roji
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions