DiagnosticSuppressor vs Visual Studio's Error List and squiggly lines #70598
-
Original question from StackOverflow ContextI have implemented a The suppressor works fine when compiling code:
However, in the Visual Studio IDE, I see squiggly lines in class members that don't have xml doc, even though the suppressor should be suppressing that diagnostic (see screenshot). QuestionWhat additional work needs to be done in order for the CodeThis is the code of the [DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class XmlDocumentationRequiredSuppressor : DiagnosticSuppressor
{
public static readonly string[] SuppressedDiagnosticIds = { "CS1591", "SA1600" };
public static readonly IReadOnlyDictionary<string, SuppressionDescriptor> SuppressionDescriptorByDiagnosticId = SuppressedDiagnosticIds.ToDictionary(
id => id,
id => new SuppressionDescriptor("GOOSE001", id, "XML documentation is most important on interface members, but not on everything else."));
public override ImmutableArray<SuppressionDescriptor> SupportedSuppressions { get; } = ImmutableArray.CreateRange(SuppressionDescriptorByDiagnosticId.Values);
public override void ReportSuppressions(SuppressionAnalysisContext context)
{
foreach (var diagnostic in context.ReportedDiagnostics)
{
if (!context.CancellationToken.IsCancellationRequested)
{
Location location = diagnostic.Location;
SyntaxNode? node = location.SourceTree?.GetRoot(context.CancellationToken).FindNode(location.SourceSpan);
if (!(node is InterfaceDeclarationSyntax || HasParentOfType<InterfaceDeclarationSyntax>(node)))
{
context.ReportSuppression(Suppression.Create(SuppressionDescriptorByDiagnosticId[diagnostic.Id], diagnostic));
}
}
}
}
public bool HasParentOfType<TSearched>(SyntaxNode? syntaxNode) where TSearched : SyntaxNode
{
return syntaxNode != null && (syntaxNode.Parent is TSearched || HasParentOfType<TSearched>(syntaxNode.Parent));
}
} More DetailsI'm using Visual Studio 2022, version 17.6.4. In the following screenshot, we see that the result from the build is correct, meaning that the reported warnings are only the ones that shouldn't be suppressed. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I think analyzers run in separate processes. If you just created the analyzer (or DiagnosticSuppressor), you might have to restart Visual Studio (perhaps all instances) so all related processes are terminated. You can also restart your computer just to be safe. |
Beta Was this translation helpful? Give feedback.
I think analyzers run in separate processes. If you just created the analyzer (or DiagnosticSuppressor), you might have to restart Visual Studio (perhaps all instances) so all related processes are terminated. You can also restart your computer just to be safe.