Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add CS8602 suppressor #24151
Add CS8602 suppressor #24151
Changes from 3 commits
1a9bedd
0d472d5
dd5bcc2
5159844
b7cf639
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I wrote in #21663 (comment), this won't take into account the common case of queryable operators, which aren't on EntityFrameworkQueryableExtensions (e.g. Where). To handle those reliably, rather than checking the queryable operators themselves, you'd need to go up the tree to make sure the tree is rooted on an EF Core DbSet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like it checks only for operations directly within an expression lambda. Consider the following:
p.Title.StartsWith
may generate a nullability warning, but the Any it's in is on Posts, which is an enumerable, not a queryable. To catch this you need to go further up the tree; this may not be trivial to do correctly, while keeping analyzer perf in mind.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@roji I haven't used ASP.NET and EF for long, so I might be wrong, but I think
Where
in the above snippet is from System.Linq and there is no expression tree involved, is that correct?If that's correct, how it's guaranteed there will be no exception thrown if expression trees aren't involved?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Where above is indeed Queryable.Where, which means it accepts an expression tree as a parameter. However, note that
b.Posts
(the navigation property) is not a DbSet/IQueryable, but rather an ordinary List (or similar); this means that Any is Enumerable.Any, not Queryable.Any.The main point here is that EF Core ensures that anything within the Expression tree would be null-safe; for example, if there's any Post with a null title, the above wouldn't throw an NRE when executed with EF Core (although it would definitely throw if executed in Linq to Objects). This suppressor would ideally identify this by walking all the way up from the warning; if it finds an EF Core DbSet, it knows that the warning can be suppressed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: empty lines
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.