简体   繁体   English

StyleCop.Analyzers 是否支持 C# 记录?

[英]Does StyleCop.Analyzers support C# records?

Problem description:问题描述:

When adding a C# record in a project with StyleCop.Analyzers , it shows the warning:在带有StyleCop.Analyzers的项目中添加C#记录时,它会显示警告:

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 ?它是StyleCop.Analyzers中的错误吗?

Steps to reproduce:重现步骤:

  • Create a new .Net6 console project (or even .Net5 ).创建一个新的.Net6控制台项目(甚至是.Net5 )。

  • Add a C# record in it: public record Member(string FirstName, string LastName);在其中添加一条C#记录: public record Member(string FirstName, string LastName);

  • Install the package StyleCop.Analyzers v1.1.118安装 package StyleCop.Analyzers v1.1.118

  • Look in the Visual Studio Error List, and you will see the warning.查看 Visual Studio 错误列表,您将看到警告。

Thank you @stuartd you pointed me to the right direction.谢谢@stuartd,您为我指明了正确的方向。

It is indeed a bug in Stylecop 1.1.X这确实是 Stylecop 1.1.X 中的一个错误

It was corrected in the StyleCop.Analyzers.Unstable v1.2.0-beta.161 .它在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所以我现在看到的解决方法是使用不稳定的版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM