如何从诊断抑制器抑制的分析器中隐藏 Visual Studio 波浪线

问题描述 投票:0回答:1

背景

我已经实现了一个

DiagnosticsSuppressor
,它抑制了除接口之外的所有构造上的
CS1591
规则。

抑制器工作正常编译代码时

  • 我在构建输出中看到缺少 xml 文档的接口的警告。
  • 除了缺少 xml 文档的接口之外,我在构建输出中没有看到任何警告。

但是,在 Visual Studio IDE 中,我在没有 xml 文档的类成员中看到波浪线,即使抑制器应该抑制该诊断(参见屏幕截图)。

问题

还需要完成哪些额外工作才能使

DiagnosticSuppressor
也从 IDE 中删除波浪线?

代码

这是

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));
    }
}

更多详情

我使用的是 Visual Studio 2022,版本 17.6.4。

在下面的屏幕截图中,我们看到构建的结果是正确的,这意味着报告的警告只是不应该被抑制的警告。 但是,我们看到这与错误列表视图中报告的内容不匹配,其中包含应该被抑制的警告。

c# visual-studio roslyn-code-analysis
1个回答
0
投票

我认为分析器在单独的进程中运行。如果您刚刚创建了分析器(或 DiagnosticSuppressor),则可能必须重新启动 Visual Studio(可能是所有实例),以便终止所有相关进程。为了安全起见,您也可以重新启动计算机。

© www.soinside.com 2019 - 2024. All rights reserved.