How to hide Visual Studio squiggly lines from analyzers suppressed by a DiagnosticSuppressor

82 Views Asked by At

Context

I have implemented a DiagnosticsSuppressor that suppresses the CS1591 rule on all constructs except interfaces.

The suppressor works fine when compiling code:

  • I see warnings in the build output for interfaces that lack xml documentation.
  • I don't see warnings in the build output for things other than interfaces that lack xml documentation.

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).

Visual Studio screenshot showing the squiggly lines

Question

What additional work needs to be done in order for the DiagnosticSuppressor to also remove the squiggly lines from the IDE?

Code

This is the code of the DiagnosticSuppressor:

[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 Details

I'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. However, we see that this doesn't match what is reported in the Error List view, which contains warnings that should have been suppressed.

Visual Studio screenshot showing the both the Error List and build output

Edit: Related Rosylyn GitHub Discussion: https://github.com/dotnet/roslyn/discussions/70598

1

There are 1 best solutions below

1
On BEST ANSWER

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.