Exclude file from StyleCop analysis: "auto-generated" tag is ignored
Asked Answered
S

3

27

At the beginning of a C# file, I have added:

//-----------------------------------------------------------------------
// <copyright company="SomeCompany" file="MyFile.cs">
// Copyright © Some Company, 2011
// </copyright>
// <auto-generated />
//-----------------------------------------------------------------------

I want StyleCop to skip checking this file, so I used the auto-generated trick explained in other answers.

However, after cleaning and rebuilding my solution, StyleCop keeps generating warnings for this file. Why does this happen? How can it be fixed?

I am using Microsoft Visual Studio 2008 Professional Edition and StyleCop v4.3.

Sturgeon answered 17/2, 2011 at 10:46 Comment(2)
Did you verify that the "Analyze generated files" option is unchecked in the StyleCop Project Settings?Bessie
@Frédéric: Thank you for your suggestion. Yes, this option is unchecked.Sturgeon
N
26

@Frédéric - unfortunately, Analyze generated files option is not somehow connected with skipping files with <auto-generated /> tag.

Files containing <auto-generated /> text will always be skipped regardless the value of the setting.

@Daniel - I believe that you deal with a bug in version 4.3 which was released more than a year ago and is definitely obsolete now. The only reason to use 4.3 is only if you use Visual Studio 2005, which is not supported by StyleCop 4.4.

I strongly recommend you upgrading to 4.4 - I've just checked your example and it works fine.

N answered 17/2, 2011 at 11:25 Comment(2)
doesn't work for me. I have version 4.4.3. What else could be wrong about it?Anthrax
@Sören, Note if you have at least one empty not starting with // in the header marked as <auto-generated>, Stylecop does not consider it as auto generated. And Microsoft includes bunch of empty lines in their auto-generated filesUbiety
E
3

You can set file exclusions within the Settings.StyleCop file. The file is located in to your solution / project or in your StyleCop install directory.

You can then use regex within the Parser settings to define files you want to ignore

<Parsers>
  <Parser ParserId="Microsoft.StyleCop.CSharp.CsParser">
    <ParserSettings>
      <BooleanProperty Name="AnalyzeDesignerFiles">False</BooleanProperty>
      <CollectionProperty Name="GeneratedFileFilters">
        <Value>\.g\.cs$</Value>
        <Value>\.generated\.cs$</Value>
        <Value>\.g\.i\.cs$</Value>
        <Value>codegen.*\.cs$</Value>
      </CollectionProperty>
    </ParserSettings>
  </Parser>
</Parsers>

In this case I want to ignore codegen.whatever.cs

Engen answered 6/3, 2017 at 14:19 Comment(0)
B
1

Check StyleCop documentation. My favourite is <auto-generated /> tag on the top of the document or you can use #region directive or other options mentioned in the docs.

Bureaucratize answered 14/8, 2015 at 11:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.