You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Create DiagnosticSuppressor for CS8602 in Linq expressions and Includes #21663
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):
classUser{publicstringFirstName{get;set;}publicstring?MiddleName{get;set;}publicstringLastName{get;set;}}...
string searchTerm="...";awaitdbContext.Users.Where(u => u =>u.FirstName.Contains(searchTerm)||u.MiddleName.Contains(searchTerm)<----------CS8602isreported,as 'MiddleName' isnullable||u.LastName.Contains(searchTerm)).ToListAsync();
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.
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.
For Linq case - we can try suppression only when expression starts with DbSet.
E.g.
awaitdbContext.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,stringsearchTerm){returnquery.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);}
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.
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.
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.
@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.
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.
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.
Activity
Dreamescaper commentedon Jul 17, 2020
Related to #21608
smitpatel commentedon Jul 17, 2020
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.
Dreamescaper commentedon Jul 17, 2020
@smitpatel
I assume it relates to Linq case only?
WDYT about Includes? It should always be safe to suppress warnings there, right?
roji commentedon Jul 17, 2020
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.
Dreamescaper commentedon Jul 17, 2020
For Linq case - we can try suppression only when expression starts with DbSet.
E.g.
but
ajcvickers commentedon Jul 17, 2020
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.
Youssef1313 commentedon Feb 13, 2021
@ajcvikers Are there any plans for this suppressor? I'm interested to implement it and submit a PR.
4 remaining items
roji commentedon Feb 13, 2021
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.
Youssef1313 commentedon Feb 14, 2021
@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:
ErikEJ commentedon Feb 14, 2021
Did you follow this guide? https://www.github.com/dotnet/efcore/tree/main/docs%2Fgetting-and-building-the-code.md
roji commentedon Feb 17, 2021
Design discussion:
Youssef1313 commentedon Feb 17, 2021
@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.
roji commentedon Feb 17, 2021
@Youssef1313 the suppressor can simply decide not to call ReportSuppression based on some flag in .editorconfig or elsewhere, no?
roji commentedon Feb 17, 2021
A quick look seems to show that a suppressor cannot report new diagnostics, nor mutate existing ones - only suppress.
roji commentedon Feb 24, 2021
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.
roji commentedon Feb 24, 2021
Actually, it seems like standard rules (editorconfig or XML) can be used to disable suppressions: https://github.com/nunit/nunit.analyzers/pull/325/files#diff-47f25205db28dc2d9aebf924f1be9ba85b2d18fbcedb7cad85710a371ec3468aR80
johnwc commentedon Jul 8, 2025
roji commentedon Jul 9, 2025