Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to suppress StyleCop error SA0102 : CSharp.CsParser : A syntax error has been discovered in file when using generic type parameters attributes

Having the following C# code with generic type parameter attribute:

[System.AttributeUsage(System.AttributeTargets.GenericParameter)]
public class GenericParameterAttribute : System.Attribute
{
}
public class GenericClass<[GenericParameter] T>
{
}

With turned on StyleCop integration (StyleCop.targets imported in .csproj file) StyleCop returns error and compilation fails:

Error 1 SA0102 : CSharp.CsParser : A syntax error has been discovered in file ...

Without StyleCop.targets imported in .csproj file compiled ok.

Environment

  • StyleCop version 4.7.47.0 (latest available from http://stylecop.codeplex.com/releases/view/79972)
  • .NET Framework 4.0

I can't find code SA0102 on StyleCop documentation site http://www.stylecop.com/docs/StyleCop%20Rules.html - it seems SA0102 is not a StyleCop rule, possible it is code of internal StyleCop error.

So question: How to suppress StyleCop error SA0102?

like image 284
Sergey Fadeev Avatar asked Dec 13 '13 09:12

Sergey Fadeev


2 Answers

It seems impossible to suppress this error with this kind of attribute :

[SuppressMessage("StyleCopNameSpace", "SA0102:RuleNameHere")] 

As related in this Post, this message is displayed when StyleCop encounters an internal error:

jasonall May 18, 2010 at 10:00 PM: It is actually not possible to suppress SA0101 or SA0102. These are special case “rules” which are thrown whenever StyleCop encounters an internal error. The only workarounds for you would be to disable this file from analysis completely, or stop using optional parameters until upgrading to StyleCop 4.4.

like image 66
AlexH Avatar answered Nov 10 '22 22:11

AlexH


Look for the file that you want to exclude from StyleCop in .csproj file and add the ExcludeFromStyleCop element as below

<Compile Include="<filename>.cs">
  <ExcludeFromStyleCop>True</ExcludeFromStyleCop>
</Compile>
like image 5
user3942922 Avatar answered Nov 10 '22 21:11

user3942922