dotnet / dotnet/efcore

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

Open
#29,988 2 comments 0 reactions 0 assignees View on GitHub
area-analyzer customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

Consider the following basic EF Core setup:
```cs
using Microsoft.EntityFrameworkCore;

class MyClass
{

}

class MyDbContext : DbContext
{
public DbSet 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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.