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

Code before:
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;

namespace Microsoft.EntityFrameworkCore;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class UninitializedDbSetDiagnosticSuppressor : DiagnosticSuppressor
{
    private static readonly SuppressionDescriptor _suppressUninitializedDbSetRule = new(
        id: "SPR1001",
        suppressedDiagnosticId: "CS8618",
        justification: "DbSet properties on DbContext subclasses are automatically populated by the DbContext constructor");

    /// <inheritdoc />
    public override ImmutableArray<SuppressionDescriptor> SupportedSuppressions => ImmutableArray.Create(_suppressUninitializedDbSetRule);

    /// <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)
            {

Review comment (suggestion):
The id will always be CS8618.

```suggestion
            if (!diagnostic.Location.IsInSource)
```

Code after:
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;

namespace Microsoft.EntityFrameworkCore;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class UninitializedDbSetDiagnosticSuppressor : DiagnosticSuppressor
{
    private static readonly SuppressionDescriptor _suppressUninitializedDbSetRule = new(
        id: "SPR1001",
        suppressedDiagnosticId: "CS8618",
        justification: AnalyzerStrings.UninitializedDbSetWarningSuppressionJustification);

    /// <inheritdoc />
    public override ImmutableArray<SuppressionDescriptor> SupportedSuppressions { get; }
        = ImmutableArray.Create(_suppressUninitializedDbSetRule);

    /// <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;