Search code examples
c#.netvisual-studiostylecop

Add custom rule to a C# project with StyleCop in VS 2022


I can't seem to find a good tutorial to add some custom styling rules that are not already customizable in StyleCop StyleCop Package

For example, creating a rule to check for N levels of nested blocks in loops or control flow statements, e.g nested code with 3 levels of depth: Nested code with 3 levels of depth.

I have found an old tutorial using VS 2008 https://subscription.packtpub.com/book/programming/9781782169543/1/ch01lvl1sec16/creating+custom+rules+(intermediate) but I believe it is not possible to do it in a similar fashion nowadays, and I cannot find anything relevant in the docs https://github.com/DotNetAnalyzers/StyleCopAnalyzers. I'm using Visual Studio 2022 in a .NET Core 6 C# project with the above-mentioned package. I would appreciate if you could share some insights about this. Thanks, in advance!

I've tried referencing these tutorials but I believe they don't apply for current VS 2022 setup with .Net 6

https://subscription.packtpub.com/book/programming/9781782169543/1/ch01lvl1sec16/creating+custom+rules+(intermediate)

https://www.planetgeek.ch/2009/07/19/custom-stylecop-rules-2/


Solution

  • I can't seem to find a good tutorial to add some custom styling rules that are not already customizable in StyleCop

    That's becuse the StyleCop.Analyzers package isn't extensible (i.e. you can't):

    https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2265

    @RodionovDmitry: Is there a way to create custom rule in C#?

    @sharwell: StyleCop Analyzers does not provide any infrastructure to aid in developing custom rules. However, it doesn't really need to because the compiler already does that. Custom rules have the form of additional independent analyzer packages. You can install any number of Roslyn-based analyzer packages into your project to get the complete analysis you want.

    But don't get disheartened: you're having trouble finding relevant info because you're searching specifically for "StyleCop", but StyleCop (as in, the original name for the original tooling from 2005-2012) is from before Roslyn was introduced and is entirely obsolete today. Instead search for "roslyn" instead of "stylecop" and you'll get relevant results.


    So what you actually want to do is write a Roslyn syntax analyzer that inspects the structure of the syntax-tree of a .cs file, which is documented here - and here is a quickstart/sample/template project for creating a custom syntax analyzer (permalink). And here's a random guide I found that's still relevant today.


    • Unless you want to use any of the rules in the StyleCop.Analyzers package, you should remove that package reference.
    • Instead, add a NuGet reference to Microsoft.CodeAnalysis.CSharp.Workspaces (which in-turn will transitively reference the other Microsoft.CodeAnalysis.* packages you'll need).
    • Load yer .cs file with SyntaxTree tree = CSharpSyntaxTree.ParseText( await File.ReadAllTextAsync( "FizzBuzz.cs" ) ); CompilationUnitSyntax root = tree.GetCompilationUnitRoot();
    • Inspect root.DescendantNodes for Syntax subtypes you're interested in, such as if statements or specific keywords and tokens.
    • Reporting warnings back to the compiler by using [DiagnosticAnalyzerAttribute]
    • ...though I'm not yet sure how you can configure analyzers via .editorconfig. If I ever find out I'll edit this answer.