Repository: dotnet/efcore
File: src/EFCore.Analyzers/UninitializedDbSetDiagnosticSuppressor.cs (C#)

Code before:
    /// <inheritdoc />
    public override void ReportSuppressions(SuppressionAnalysisContext context)
    {
        INamedTypeSymbol? dbSetTypeSymbol = null;
        INamedTypeSymbol? dbContextTypeSymbol = null;

        foreach (var diagnostic in context.ReportedDiagnostics)
        {
            if (diagnostic.Id != "CS8618" || !diagnostic.Location.IsInSource)
            {
                continue;
            }

            // We have an warning about an uninitialized non-nullable property.
            // Get the node, and make sure it's a property who's type syntactically contains DbSet (fast check before getting the
            // semantic model, which is heavier).
            var sourceTree = diagnostic.Location.SourceTree;
            var node = sourceTree?.GetRoot().FindNode(diagnostic.Location.SourceSpan);
            if (node is not PropertyDeclarationSyntax propertyDeclarationSyntax
                || !propertyDeclarationSyntax.Type.ToString().Contains("DbSet"))
            {
                continue;
            }

            // Get the semantic symbol and do some basic checks
            if (context.GetSemanticModel(sourceTree!).GetDeclaredSymbol(node) is not IPropertySymbol propertySymbol
                || propertySymbol.IsStatic
                || propertySymbol.IsReadOnly)
            {
                continue;
            }

            if (dbSetTypeSymbol is null || dbContextTypeSymbol is null)
            {
                dbSetTypeSymbol = context.Compilation.GetTypeByMetadataName("Microsoft.EntityFrameworkCore.DbSet`1");
                dbContextTypeSymbol = context.Compilation.GetTypeByMetadataName("Microsoft.EntityFrameworkCore.DbContext");

                if (dbSetTypeSymbol is null || dbContextTypeSymbol is null)
                {
                    return;
                }
            }

            // Check that the property is actually a DbSet<T>, and that its containing type inherits from DbContext
            if (propertySymbol.Type.OriginalDefinition.Equals(dbSetTypeSymbol, SymbolEqualityComparer.Default)
                && InheritsFrom(propertySymbol.ContainingType, dbContextTypeSymbol))
            {
                context.ReportSuppression(Suppression.Create(SupportedSuppressions[0], diagnostic));
            }

            static bool InheritsFrom(ITypeSymbol typeSymbol, ITypeSymbol baseTypeSymbol)

Review comment (suggestion):
With the suggestion in https://github.com/dotnet/efcore/pull/26795/files#r754816528, the null check will no longer be needed.

Code after:
    /// <inheritdoc />
    public override void ReportSuppressions(SuppressionAnalysisContext context)
    {
        INamedTypeSymbol? dbSetTypeSymbol = null;
        INamedTypeSymbol? dbContextTypeSymbol = null;

        foreach (var diagnostic in context.ReportedDiagnostics)
        {
            // We have an warning about an uninitialized non-nullable property.
            // Get the node, and make sure it's a property who's type syntactically contains DbSet (fast check before getting the
            // semantic model, which is heavier).
            if (diagnostic.Location.SourceTree is not { } sourceTree
                || sourceTree.GetRoot().FindNode(diagnostic.Location.SourceSpan) is not PropertyDeclarationSyntax propertyDeclarationSyntax
                || !propertyDeclarationSyntax.Type.ToString().Contains("DbSet"))
            {
                continue;
            }

            // Get the semantic symbol and do some basic checks
            if (context.GetSemanticModel(sourceTree).GetDeclaredSymbol(propertyDeclarationSyntax) is not IPropertySymbol propertySymbol
                || propertySymbol.IsStatic
                || propertySymbol.IsReadOnly)
            {
                continue;
            }

            if (dbSetTypeSymbol is null || dbContextTypeSymbol is null)
            {
                dbSetTypeSymbol = context.Compilation.GetTypeByMetadataName("Microsoft.EntityFrameworkCore.DbSet`1");
                dbContextTypeSymbol = context.Compilation.GetTypeByMetadataName("Microsoft.EntityFrameworkCore.DbContext");

                if (dbSetTypeSymbol is null || dbContextTypeSymbol is null)
                {
                    return;
                }
            }

            // Check that the property is actually a DbSet<T>, and that its containing type inherits from DbContext
            if (propertySymbol.Type.OriginalDefinition.Equals(dbSetTypeSymbol, SymbolEqualityComparer.Default)
                && InheritsFrom(propertySymbol.ContainingType, dbContextTypeSymbol))
            {
                context.ReportSuppression(Suppression.Create(SupportedSuppressions[0], diagnostic));
            }

            static bool InheritsFrom(ITypeSymbol typeSymbol, ITypeSymbol baseTypeSymbol)
            {
                var baseType = typeSymbol.BaseType;
                while (baseType is not null)
                {
                    if (baseType.Equals(baseTypeSymbol, SymbolEqualityComparer.Default))
                    {