Repository: dotnet/roslyn
File: src/Compilers/CSharp/Test/Emit3/Diagnostics/GetDiagnosticsTests.cs (C#)

Code before:
                //     [property: Test]
                Diagnostic(ErrorCode.WRN_AttributeLocationOnBadDeclaration, "property").WithArguments("property", "field").WithLocation(5, 6));

            // Verify that CS0657 can be suppressed with a DiagnosticSuppressor
            var diagnosticAnalyzer = new CSharpCompilerDiagnosticAnalyzer();
            var suppressor = new DiagnosticSuppressorForCS0657();

            var options = new CompilationWithAnalyzersOptions(
                AnalyzerOptions.Empty,
                onAnalyzerException: null,
                concurrentAnalysis: false,
                logAnalyzerExecutionTime: false,
                reportSuppressedDiagnostics: true,
                analyzerExceptionFilter: null,
                getAnalyzerConfigOptionsProvider: _ => new CompilerAnalyzerConfigOptionsProvider(
                    ImmutableDictionary<object, AnalyzerConfigOptions>.Empty,
                    new DictionaryAnalyzerConfigOptions(ImmutableDictionary<string, string>.Empty)));

            var compilationWithAnalyzers = compilation.WithAnalyzers([diagnosticAnalyzer, suppressor], options);

            // Calling in with a single analyzer should still properly setup internal maps so that suppressors run properly.
            var analysisResult1 = await compilationWithAnalyzers.GetAnalysisResultAsync([diagnosticAnalyzer], CancellationToken.None);
            var analysisResult2 = await compilationWithAnalyzers.GetAnalysisResultAsync([diagnosticAnalyzer, suppressor], CancellationToken.None);

            var diagnostic1 = analysisResult1.SemanticDiagnostics.Single().Value.Single().Value.Single();
            var diagnostic2 = analysisResult1.SemanticDiagnostics.Single().Value.Single().Value.Single();

            Assert.True(diagnostic1.IsSuppressed);
            Assert.True(diagnostic2.IsSuppressed);

            Assert.Equal("CS0657", diagnostic1.Id);
            Assert.Equal("CS0657", diagnostic2.Id);
        }

        [DiagnosticAnalyzer(LanguageNames.CSharp)]
        private sealed class DiagnosticSuppressorForCS0657 : DiagnosticSuppressor
        {
            internal const string SuppressionId = "SPR0001";
            private readonly SuppressionDescriptor _descriptor = new(SuppressionId, "CS0657", "Justification");
            public override ImmutableArray<SuppressionDescriptor> SupportedSuppressions => ImmutableArray.Create(_descriptor);

            public override void ReportSuppressions(SuppressionAnalysisContext context)
            {
                foreach (var diagnostic in context.ReportedDiagnostics)
                {
                    context.ReportSuppression(Suppression.Create(_descriptor, diagnostic));
                }
            }
        }

        [Theory, CombinatorialData, WorkItem(66968, "https://github.com/dotnet/roslyn/issues/66968")]

Review comment (suggestion):
Both `diagnostic1` and `diagnostic2` are the same value. Did you mean to do this instead?

```suggestion
            var diagnostic2 = analysisResult2.SemanticDiagnostics.Single().Value.Single().Value.Single();
```

Code after:
                //     [property: Test]
                Diagnostic(ErrorCode.WRN_AttributeLocationOnBadDeclaration, "property").WithArguments("property", "field").WithLocation(5, 6));

            // Verify that CS0657 can be suppressed with a DiagnosticSuppressor
            var diagnosticAnalyzer = new CSharpCompilerDiagnosticAnalyzer();
            var suppressor = new DiagnosticSuppressorForCS0657();

            // Create options with our own getAnalyzerConfigOptionsProvider.  That way we do initialize the map from
            // analyzer to options within the AnalyzerDriver.  This map needs to contain all analyzers and suppressors,
            // not just the one we're calling into with GetAnalysisResultAsync.
            var options = new CompilationWithAnalyzersOptions(
                AnalyzerOptions.Empty,
                onAnalyzerException: null,
                concurrentAnalysis: false,
                logAnalyzerExecutionTime: false,
                reportSuppressedDiagnostics: true,
                analyzerExceptionFilter: null,
                getAnalyzerConfigOptionsProvider: _ => new CompilerAnalyzerConfigOptionsProvider(
                    ImmutableDictionary<object, AnalyzerConfigOptions>.Empty,
                    new DictionaryAnalyzerConfigOptions(ImmutableDictionary<string, string>.Empty)));

            var compilationWithAnalyzers = compilation.WithAnalyzers([diagnosticAnalyzer, suppressor], options);

            // Calling in with a single analyzer should still properly setup internal maps so that suppressors run properly.
            var analysisResult1 = await compilationWithAnalyzers.GetAnalysisResultAsync([diagnosticAnalyzer], CancellationToken.None);
            var analysisResult2 = await compilationWithAnalyzers.GetAnalysisResultAsync([diagnosticAnalyzer, suppressor], CancellationToken.None);

            var diagnostic1 = analysisResult1.SemanticDiagnostics.Single().Value.Single().Value.Single();
            var diagnostic2 = analysisResult2.SemanticDiagnostics.Single().Value.Single().Value.Single();

            Assert.True(diagnostic1.IsSuppressed);
            Assert.True(diagnostic2.IsSuppressed);

            Assert.Equal("CS0657", diagnostic1.Id);
            Assert.Equal("CS0657", diagnostic2.Id);
        }

        [DiagnosticAnalyzer(LanguageNames.CSharp)]
        private sealed class DiagnosticSuppressorForCS0657 : DiagnosticSuppressor
        {
            internal const string SuppressionId = "SPR0001";
            private readonly SuppressionDescriptor _descriptor = new(SuppressionId, "CS0657", "Justification");
            public override ImmutableArray<SuppressionDescriptor> SupportedSuppressions => ImmutableArray.Create(_descriptor);

            public override void ReportSuppressions(SuppressionAnalysisContext context)
            {
                foreach (var diagnostic in context.ReportedDiagnostics)
                {
                    context.ReportSuppression(Suppression.Create(_descriptor, diagnostic));
                }
            }