Skip to content
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

Analyzer suggestion: mark DbSet's nullability suppression property initializer as unnecessary #29988

Open
Tracked by #22086
DoctorKrolic opened this issue Jan 5, 2023 · 1 comment

Comments

@DoctorKrolic
Copy link
Contributor

Consider the following basic EF Core setup:

using Microsoft.EntityFrameworkCore;

class MyClass
{
    
}

class MyDbContext : DbContext
{
    public DbSet<MyClass> MyDbSet { get; set; } = null!;
}

Prior to EF Core 7 this was a normal example of Db context declaration. However, since EF Core 7 introduced a nullability warning suppressor for DbSet properties, the = null!; is no longer needed and can be considered as unnecessary noise. It would be great to have an analyzer + codefix, that would suggest to remove the = null!; part.

Notes:

  1. By playing with diagnostic tags you can actually make = null!; fade out the way VS fades out all unnecessary code parts (usings, parentheses etc.). This should also work in Rider, but I am not sure
  2. The codefix should have proper fix-all support, so if a user updates his codebase from EF Core 6, he can just fix all such cases in several clicks
  3. The diagnostic should only trigger for actual nullability suppressions like = null!; or = default!;. It should not be reported for things like = null;, because without nullability suppression operator it is no longer a EF-specific case
@roji
Copy link
Member

roji commented Jan 5, 2023

Really nice idea, I like it; added to #22086 for tracking along with other analyzer ideas.

Unfortunately there's a compiler bug which makes the suppressor fail when warnings-as-errors is enabled (#29582), so this wouldn't be a great idea until that's fixed...

@ajcvickers ajcvickers added this to the Backlog milestone Jan 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants