Suppressing the StyleCop Warnings at the global level

荒凉一梦 提交于 2020-02-21 11:21:10

问题


How to suppress the StyleCop warnings globally in the solution?

The solution is continually built using Jenkins (continuous build and integration tool), and it applies all the StyleCop rules. The solution uses TAB character instead of 4 spaces as it is the standard taken by my dev team. Because of this, several SA1027 warnings are thrown by StlyeCop.

How do I remove SA1027 warning from Jenkins? That would also help.

Giving the SuppressMessage on every C# file would not look nice. That's why I am looking for a global suppression approach.


回答1:


StyleCop doesn't support the notion of global suppression of a rule. It requires the SuppressMessage attribute be placed on the given code element.

From the Source Analysis Blog (Source)

StyleCop does not support the notion of global suppressions or file-level suppressions. Suppressions must be placed on a code element.

One option though is to simply turn off the rules that you aren't interested in. This is the preferred method of global suppression.




回答2:


Seems that you probably need to change StyleCop configuration (settings) than to suppress some rules globally. StyleCop settings are "inherited through file system" so you could just create an appropriate settings file at the level of your solution folder.

Regarding your specific "tabs vs. spaces" subject, you could not only turn off StyleCop rules that requre spaces, but also to use some StyleCop plugins (like StyleCop+) that contains rules requiring tabs.




回答3:


VS2015 + StyleCop Analyzers:

StyleCop.Settings does not get used anymore, but you can still do what JaredPar said and switch off specific warnings globally. Do this by modifying your current rule set (under the project's Properties GUI > Code Analysis > Open), and saving your changes as a new rule set.




回答4:


File Settings.StyleCop should contain:

<StyleCopSettings Version="105">
  <Analyzers>
    <Analyzer AnalyzerId="StyleCop.CSharp.SpacingRules">
      <Rules>
        <Rule Name="TabsMustNotBeUsed">
          <RuleSettings>
            <BooleanProperty Name="Enabled">False</BooleanProperty>
          </RuleSettings>
        </Rule>
      </Rules>
      <AnalyzerSettings />
    </Analyzer>
  </Analyzers>
</StyleCopSettings>



回答5:


Find your .ruleset file. (under Solution Items or wherever it is) Change the Action to None.

<Rule Id="SA1027" Action="None" />


来源:https://stackoverflow.com/questions/7247206/suppressing-the-stylecop-warnings-at-the-global-level

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!