static bool isEnabledWithAnalyzerConfigOptions( DiagnosticDescriptor descriptor, SeverityFilter severityFilter, Compilation? compilation, AnalyzerOptions? analyzerOptions, CancellationToken cancellationToken) { if (compilation != null && compilation.Options.SyntaxTreeOptionsProvider is { } treeOptions) { foreach (var tree in compilation.SyntaxTrees) { // Check if diagnostic is enabled by SyntaxTree.DiagnosticOptions or Bulk configuration from AnalyzerConfigOptions. if (treeOptions.TryGetDiagnosticValue(tree, descriptor.Id, cancellationToken, out var configuredValue) || analyzerOptions.TryGetSeverityFromBulkConfiguration(tree, compilation, descriptor, cancellationToken, out configuredValue)) { if (configuredValue != ReportDiagnostic.Suppress && !severityFilter.Contains(configuredValue)) { return true; } } } } return false; }
internal bool IsDiagnosticAnalyzerSuppressed( DiagnosticAnalyzer analyzer, CompilationOptions options, Func<DiagnosticAnalyzer, bool> isCompilerAnalyzer, AnalyzerExecutor analyzerExecutor, SeverityFilter severityFilter) { if (isCompilerAnalyzer(analyzer)) { // Compiler analyzer must always be executed for compiler errors, which cannot be suppressed or filtered. return false; } var supportedDiagnostics = GetSupportedDiagnosticDescriptors(analyzer, analyzerExecutor); var diagnosticOptions = options.SpecificDiagnosticOptions; analyzerExecutor.TryGetCompilationAndAnalyzerOptions(out var compilation, out var analyzerOptions); foreach (var diag in supportedDiagnostics) { if (HasNotConfigurableTag(diag.CustomTags)) { if (diag.IsEnabledByDefault) { // Diagnostic descriptor is not configurable, so the diagnostics created through it cannot be suppressed. return false; } else { // NotConfigurable disabled diagnostic can be ignored as it is never reported. continue; } } // Is this diagnostic suppressed by default (as written by the rule author) var isSuppressed = !diag.IsEnabledByDefault; // Compilation wide user settings from ruleset/nowarn/warnaserror overrides the analyzer author. // Note that "/warnaserror-:DiagnosticId" adds a diagnostic option with value 'ReportDiagnostic.Default', // which should not alter 'isSuppressed'. if (diagnosticOptions.TryGetValue(diag.Id, out var severity) && severity != ReportDiagnostic.Default) { isSuppressed = severity == ReportDiagnostic.Suppress; } else { severity = isSuppressed ? ReportDiagnostic.Suppress : DiagnosticDescriptor.MapSeverityToReport(diag.DefaultSeverity); } // Is this diagnostic suppressed due to its severity if (severityFilter.Contains(severity)) { isSuppressed = true; } // Editorconfig user settings override compilation wide settings. if (isSuppressed && isEnabledWithAnalyzerConfigOptions(diag, severityFilter, compilation, analyzerOptions, analyzerExecutor.CancellationToken)) { isSuppressed = false; } if (!isSuppressed) { return false; } } if (analyzer is DiagnosticSuppressor suppressor) { foreach (var suppressionDescriptor in GetSupportedSuppressionDescriptors(suppressor, analyzerExecutor)) { if (!suppressionDescriptor.IsDisabled(options)) { return false; } } } return true;