Search code examples
c#recordstylecop

Does StyleCop.Analyzers support C# records?


Problem description:

When adding a C# record in a project with StyleCop.Analyzers, it shows the warning:

Warning AD0001  Analyzer 'StyleCop.Analyzers.DocumentationRules.SA1649FileNameMustMatchTypeName' threw an exception of type 'System.ArgumentException' with message 'Unhandled declaration kind: RecordDeclaration'.    

System.ArgumentException: Unhandled declaration kind: RecordDeclaration
       at StyleCop.Analyzers.Helpers.NamedTypeHelpers.GetNameOrIdentifier(MemberDeclarationSyntax member)
       at StyleCop.Analyzers.Helpers.FileNameHelpers.GetConventionalFileName(MemberDeclarationSyntax declaration, FileNamingConvention convention)
       at StyleCop.Analyzers.DocumentationRules.SA1649FileNameMustMatchTypeName.Analyzer.HandleSyntaxTree(SyntaxTreeAnalysisContext context, StyleCopSettings settings)
       at StyleCop.Analyzers.AnalyzerExtensions.<>c__DisplayClass0_0.<RegisterSyntaxTreeAction>b__0(SyntaxTreeAnalysisContext c)
       at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c.<ExecuteSyntaxTreeActionsCore>b__59_1(ValueTuple`2 data)
       at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info)

Is it a bug in StyleCop.Analyzers?

Steps to reproduce:

  • Create a new .Net6 console project (or even .Net5).

  • Add a C# record in it: public record Member(string FirstName, string LastName);

  • Install the package StyleCop.Analyzers v1.1.118

  • Look in the Visual Studio Error List, and you will see the warning.


Solution

  • Thank you @stuartd you pointed me to the right direction.

    It is indeed a bug in Stylecop 1.1.X

    It was corrected in the StyleCop.Analyzers.Unstable v1.2.0-beta.161. but unfortunately, it is not included in the stable version yet. So the workaround I see for now is to use the unstable version