public void TestSuppression_MultipleSuppressors_DifferentDiagnostic() { string source = @"class C1 { private readonly int f; }"; var compilation = CreateCompilation(source); compilation.VerifyDiagnostics( // (1,33): warning CS0169: The field 'C1.f' is never used // class C1 { private readonly int f; } Diagnostic(ErrorCode.WRN_UnreferencedField, "f").WithArguments("C1.f").WithLocation(1, 33)); var analyzer = new CompilationAnalyzerWithSeverity(DiagnosticSeverity.Warning, configurable: true); VerifyAnalyzerDiagnostics(compilation, new DiagnosticAnalyzer[] { analyzer }, Diagnostic(analyzer.Descriptor.Id, source)); var suppressor1 = new DiagnosticSuppressorForId("CS0169"); var suppressor2 = new DiagnosticSuppressorForId(analyzer.Descriptor.Id); var analyzersAndSuppressors = new DiagnosticAnalyzer[] { analyzer, suppressor1, suppressor2 }; VerifySuppressedDiagnostics(compilation, analyzersAndSuppressors, Diagnostic("CS0169", "f", isSuppressed: true).WithArguments("C1.f").WithLocation(1, 33), Diagnostic(analyzer.Descriptor.Id, source, isSuppressed: true)); VerifySuppressedAndFilteredDiagnostics(compilation, analyzersAndSuppressors); }
public void TestSuppression_AnalyzerDiagnostics_SeveritiesAndConfigurableMatrix() { string source = @" class C { }"; var compilation = CreateCompilation(source); compilation.VerifyDiagnostics(); var configurations = new[] { false, true }; var severities = Enum.GetValues(typeof(DiagnosticSeverity)); var originalSpecificDiagnosticOptions = compilation.Options.SpecificDiagnosticOptions; foreach (var configurable in configurations) { foreach (DiagnosticSeverity defaultSeverity in severities) { foreach (DiagnosticSeverity effectiveSeverity in severities) { var diagnostic = Diagnostic("ID1000", "class C { }") .WithLocation(2, 1) .WithDefaultSeverity(defaultSeverity) .WithEffectiveSeverity(configurable ? effectiveSeverity : defaultSeverity); if (defaultSeverity == DiagnosticSeverity.Warning && effectiveSeverity == DiagnosticSeverity.Error && configurable) { diagnostic = diagnostic.WithWarningAsError(true); } var analyzer = new CompilationAnalyzerWithSeverity(defaultSeverity, configurable); var suppressor = new DiagnosticSuppressorForId(analyzer.Descriptor.Id); var analyzersWithoutSuppressor = new DiagnosticAnalyzer[] { analyzer }; var analyzersWithSuppressor = new DiagnosticAnalyzer[] { analyzer, suppressor }; var specificDiagnosticOptions = originalSpecificDiagnosticOptions.Add( key: analyzer.Descriptor.Id, value: DiagnosticDescriptor.MapSeverityToReport(effectiveSeverity)); compilation = compilation.WithOptions(compilation.Options.WithSpecificDiagnosticOptions(specificDiagnosticOptions)); // Verify analyzer diagnostic without suppressor, also verify no suppressions. VerifyAnalyzerDiagnostics(compilation, analyzersWithoutSuppressor, diagnostic); VerifySuppressedDiagnostics(compilation, analyzersWithoutSuppressor); // Verify suppressed analyzer diagnostic, except when default severity is Error or diagnostic is not-configurable. if (defaultSeverity == DiagnosticSeverity.Error || !configurable) { VerifySuppressedDiagnostics(compilation, analyzersWithSuppressor); } else { VerifySuppressedDiagnostics(compilation, analyzersWithSuppressor, diagnostic); } } } } }
public void TestProgrammaticSuppressionInfo_DiagnosticSuppressor() { string source = @"class C1 { }"; var compilation = CreateCompilation(source); compilation.VerifyDiagnostics(); var analyzer = new CompilationAnalyzerWithSeverity(DiagnosticSeverity.Warning, configurable: true); var expectedDiagnostic = Diagnostic(analyzer.Descriptor.Id, source); VerifyAnalyzerDiagnostics(compilation, new DiagnosticAnalyzer[] { analyzer }, expectedDiagnostic); const string suppressionId = "SPR1001"; var suppressor = new DiagnosticSuppressorForId(analyzer.Descriptor.Id, suppressionId); var analyzersAndSuppressors = new DiagnosticAnalyzer[] { analyzer, suppressor }; var diagnostics = compilation.GetAnalyzerDiagnostics(analyzersAndSuppressors, reportSuppressedDiagnostics: true); Assert.Single(diagnostics); var suppressionInfo = diagnostics.Select(d => d.ProgrammaticSuppressionInfo).Single().Suppressions.Single(); Assert.Equal(suppressionId, suppressionInfo.Id); Assert.Equal(suppressor.SuppressionDescriptor.Justification, suppressionInfo.Justification); const string suppressionId2 = "SPR1002"; var suppressor2 = new DiagnosticSuppressorForId(analyzer.Descriptor.Id, suppressionId2); analyzersAndSuppressors = new DiagnosticAnalyzer[] { analyzer, suppressor, suppressor2 }; diagnostics = compilation.GetAnalyzerDiagnostics(analyzersAndSuppressors, reportSuppressedDiagnostics: true); Assert.Single(diagnostics); var programmaticSuppression = diagnostics.Select(d => d.ProgrammaticSuppressionInfo).Single(); Assert.Equal(2, programmaticSuppression.Suppressions.Count); var orderedSuppressions = programmaticSuppression.Suppressions.Order().ToImmutableArrayOrEmpty(); Assert.Equal(suppressionId, orderedSuppressions[0].Id); Assert.Equal(suppressor.SuppressionDescriptor.Justification, orderedSuppressions[0].Justification); Assert.Equal(suppressionId2, orderedSuppressions[1].Id); Assert.Equal(suppressor2.SuppressionDescriptor.Justification, orderedSuppressions[1].Justification); }