How to suppress a StyleCop warning?

后端 未结 12 1335
心在旅途
心在旅途 2020-11-27 18:23

I\'m using StyleCop and want to suppress some warning which does not suit my style. I prefer to have solution for

1) in-line code suppressing
2) global setting

相关标签:
12条回答
  • 2020-11-27 18:57

    You can disable the rules you don't want in Settings.StyleCop file, which is in the project root folder. You will need the namespace that contains the rule, which can be found here: http://stylecop.soyuz5.com/StyleCop%20Rules.html

    Settings.stylecop file code for your reference:

    <StyleCopSettings Version="105">
      <Analyzers>
        <Analyzer AnalyzerId="StyleCop.CSharp.LayoutRules">
          <Rules>
            <Rule Name="ElementsMustBeSeparatedByBlankLine">
              <RuleSettings>
                <BooleanProperty Name="Enabled">False</BooleanProperty>
              </RuleSettings>
            </Rule>
          </Rules>
          <AnalyzerSettings />
        </Analyzer>
      </Analyzers>
    </StyleCopSettings>
    
    0 讨论(0)
  • 2020-11-27 18:59

    Alternatively you could move the code in regions into partial classes. Then the issue with the stylecop rule will go away.

    0 讨论(0)
  • 2020-11-27 19:00

    If you've installed StyleCop, you can right-click your project and there will be a StyleCop option. Click this and you'll see you can prevent certain rules from even running against your project. Moreover, you can create a separate rules file to share between different projects. This means you can configure the rules once the way you want them and then share that configuration between all your projects.

    For individual overrides, SuppressMessage is the way to go.

    0 讨论(0)
  • 2020-11-27 19:02

    Here's what you need:

    [SuppressMessage("Microsoft.StyleCop.CSharp.OrderingRules", "SA1202:ElementsMustBeOrderedByAccess")]
    
    0 讨论(0)
  • 2020-11-27 19:02
    1. Go to Solution Explorer
    2. Go to your project
    3. Expand references
    4. Expand Analyzers
    5. Expand StyleCop.Analyzers
    6. Right click on a particular rule which you want to disable at a global (project) level
    7. Set Rule Set severity -> Select None
    0 讨论(0)
  • 2020-11-27 19:05

    In addition to the helpful answers already in place:

    If you suppress a warning in the suppression file GlobalSuppressions.cs, you can edit that [assembly: SuppressMessage(StyleCop...blabla line and entirely remove the Scope=... and Target=... tags. That makes the suppression global in the project.

    0 讨论(0)
提交回复
热议问题