16

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

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?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Sergey Fadeev
  • 333
  • 1
  • 4
  • 10
  • 4
    "A syntax error has been discovered in file." What a misleading error message. It ought to make clear the error is from Style Cop rather than the compiler. Your code is fine, but Style Cop can't cope with [C# 6](https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C%23-6). – Colonel Panic Apr 18 '16 at 10:21

2 Answers2

12

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.

sashoalm
  • 75,001
  • 122
  • 434
  • 781
AlexH
  • 2,650
  • 1
  • 27
  • 35
  • 2
    Yes, suppressing with [SuppressMessage] attribute is not working - parse error happened little earlier. But this post helps me to find the way to disable StyleCop for only one file (right click on it) and not for the whole project. Thank you. – Sergey Fadeev Dec 14 '13 at 10:07
  • 1
    How is this an answer? It doesn't fix the OP's problem, rather invites an opportunity for the Style Cop team to fix their analyzer. – The Sharp Ninja Sep 10 '20 at 15:14
5

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>
user3942922
  • 83
  • 1
  • 1
  • 1
    Yes, it is the same as right click on file in Solution Explorer and select Exclude from StyleCop in file context menu. Thank you. – Sergey Fadeev Aug 20 '14 at 12:45
  • 8
    This is not a solution as it would also suppress all other checks. This is not what the OP is trying to achieve. – Imre Pühvel Apr 19 '17 at 09:05