dotnet/roslyn-sdk

Analyzer for comments producing false positives

jezzsantos opened this issue · 2 comments

I've built an analyzer (net7) for making sure certain code elements (in C#) have appropriate comments on them:

For example, this code would raise a Warning:

public class Class1
{
}

where as this code would not raise a Warning:

/// <summary>
/// This class is designed to do something very special....
/// </summary>
public class Class1
{
}

I registered my analyzer like this:

 public override void Initialize(AnalysisContext context)
    {
        context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
        context.EnableConcurrentExecution();
        context.RegisterSyntaxNodeAction(AnalyzeType, SyntaxKind.StructDeclaration, SyntaxKind.ClassDeclaration,
            SyntaxKind.InterfaceDeclaration, SyntaxKind.RecordDeclaration, SyntaxKind.DelegateDeclaration,
            SyntaxKind.EnumDeclaration);
        context.RegisterSyntaxNodeAction(AnalyzeMethod, SyntaxKind.MethodDeclaration);
    }

This all works great, in the IDE editor (and in unit testing).
I can see the warnings on classes without comments, and I can fix them up, and the warning goes away.

However, when building the solution in the IDE (Jetbrains Rider), I get a build Warning for every place in the code that the rule checks, regardless of the fact that an appropriate comment exists at that place!
My build log is full of the warnings. Clicking on them to navigate to the code, shows me code where you can see the comment, and no warning squiggles underlining the code.

I am wondering if the reason for that is because comments are removed from the compiled code, and the analyzer is inspecting the compiled code?

If that is the case, should I be using a different setup, or strategy?

/// comments only exist when <GenerateDocumentationFile> is set to true. In a project where this is set to false, the comments will come through as single-line comments without any semantic content. See also https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/05489698155b609dfc081c06ad21771ddcf26f6c/documentation/SA0001.md.

Thanks,

Makes sense, thank you. Not had to think about generating those files in a long time. cheers