如何抑制 StyleCop 警告?

发布于 2024-09-10 17:31:10 字数 645 浏览 8 评论 0原文

我正在使用 StyleCop,并且想要抑制一些不适合我的风格的警告。我更喜欢解决

1) 内联代码抑制
2)全局设置抑制

我已经在互联网上搜索过,但 仍然没有确定如何进行抑制。

对于方法 1),他们说要添加以下行:

[程序集:SuppressMessage("Microsoft.Design", “SA1202:所有私有方法必须放在所有公共方法之后”, 范围 =“命名空间”,目标 =“Consus.Client.ClientVaultModule.Services.OnlineDetection”)]

但他们没有说明要使用的位置和名称空间。

对于方法 2),他们说使用 GlobalSuppress 文件,但目前似乎很难搜索操作方法。

请帮忙。

[编辑] 就我而言,我有关于SA1202:所有私有方法必须放置在所有公共方法之后的警告,这很麻烦,因为我将相关代码分组到区域中。我想仅针对某些特定方法抑制这些警告。

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 suppressing

I've searched the internet but still not sure how to do the suppressing.

For method 1), They said to add the lines:

[assembly: SuppressMessage("Microsoft.Design",
"SA1202:All private methods must be placed after all public methods",
Scope = "namespace", Target = "Consus.Client.ClientVaultModule.Services.OnlineDetection")]

But they do not say where and which namespace to be used.

For method 2), they said to use GlobalSuppress file but it seems not easy to search for a how-to do it at the moment.

Please help.

[Edited]
In my case, I have the warning about SA1202: All private methods must be placed after all public methods which is bothering since I group my related codes into regions. I want to suppress those warning for just some certain methods.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

陌伤ぢ 2024-09-17 17:31:10

这是您需要的:

[SuppressMessage("Microsoft.StyleCop.CSharp.OrderingRules", "SA1202:ElementsMustBeOrderedByAccess")]

Here's what you need:

[SuppressMessage("Microsoft.StyleCop.CSharp.OrderingRules", "SA1202:ElementsMustBeOrderedByAccess")]
姐不稀罕 2024-09-17 17:31:10

内联抑制的示例与此类似 - 检查与抑制相比的代码中的命名空间

namespace Soapi
{
        ///<summary>
        ///</summary>
        ///<param name = "message"></param>
        ///<param name = "statusCode"></param>
        ///<param name = "innerException"></param>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.String.Format(System.String,System.Object,System.Object)")]
        public ApiException(string message, ErrorCode statusCode, Exception innerException)
            : base(String.Format("{0}\r\nStatusCode:{1}", message, statusCode), innerException)
        {
            this.statusCode = statusCode;
        }

全局抑制文件是项目根目录中名为 GlobalSuppressions.cs 的文件,可能如下所示:

// This file is used by Code Analysis to maintain SuppressMessage 
// attributes that are applied to this project. 
// Project-level suppressions either have no target or are given 
// a specific target and scoped to a namespace, type, member, etc. 
//
// To add a suppression to this file, right-click the message in the 
// Error List, point to "Suppress Message(s)", and click 
// "In Project Suppression File". 
// You do not need to add suppressions to this file manually. 

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.String.Format(System.String,System.Object,System.Object,System.Object)", Scope = "member", Target = "Soapi.ApiException.#.ctor(System.String,Soapi.ErrorCode,System.String,System.Exception)")]

您可以通过右键单击警告来自动生成此代码。

An example of inline suppression would be similar to this - examine the namespaces in the code compared to the suppression

namespace Soapi
{
        ///<summary>
        ///</summary>
        ///<param name = "message"></param>
        ///<param name = "statusCode"></param>
        ///<param name = "innerException"></param>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.String.Format(System.String,System.Object,System.Object)")]
        public ApiException(string message, ErrorCode statusCode, Exception innerException)
            : base(String.Format("{0}\r\nStatusCode:{1}", message, statusCode), innerException)
        {
            this.statusCode = statusCode;
        }

A global supression file is a file in the root of your project named GlobalSuppressions.cs and might look like this:

// This file is used by Code Analysis to maintain SuppressMessage 
// attributes that are applied to this project. 
// Project-level suppressions either have no target or are given 
// a specific target and scoped to a namespace, type, member, etc. 
//
// To add a suppression to this file, right-click the message in the 
// Error List, point to "Suppress Message(s)", and click 
// "In Project Suppression File". 
// You do not need to add suppressions to this file manually. 

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.String.Format(System.String,System.Object,System.Object,System.Object)", Scope = "member", Target = "Soapi.ApiException.#.ctor(System.String,Soapi.ErrorCode,System.String,System.Exception)")]

And you can generate this code automatically by right-clicking on the warning.

梦里人 2024-09-17 17:31:10

从 StyleCop 4.3.2 开始,可以通过在源代码中添加抑制属性来抑制规则违规的报告。

规则抑制
http://stylecop.soyuz5.com/Suppressions.html

但它说 -

全局抑制

StyleCop 不支持全局抑制的概念或
文件级抑制。必须对代码进行抑制
元素。

Starting with StyleCop 4.3.2, it is possible to suppress the reporting of rule violations by adding suppression attributes within the source code.

Rule Suppressions
http://stylecop.soyuz5.com/Suppressions.html

but it says -

Global Suppressions

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

吻泪 2024-09-17 17:31:10

如果你已经安装了StyleCop,你可以右键单击你的项目,就会有一个StyleCop选项。单击此按钮,您将看到您甚至可以阻止某些规则针对您的项目运行。此外,您可以创建单独的规则文件以在不同项目之间共享。这意味着您可以按照您想要的方式配置规则,然后在所有项目之间共享该配置。

对于单独的覆盖,SuppressMessage 是最佳选择。

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.

扎心 2024-09-17 17:31:10
  1. 转到解决方案资源管理器
  2. 转到您的项目
  3. 展开
  4. 引用 展开分析器
  5. 展开 StyleCop.Analyzers
  6. 右键单击​​要在全局(项目)级别禁用的特定规则
  7. 设置规则集严重性 ->选择无
  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
葬﹪忆之殇 2024-09-17 17:31:10

阅读 Style Cop 的警告,寻找字母数字代码。在你的情况下是“SA1202”。然后浏览至 Style Cop 网站上的相应页面。根据需要更改 URL https://github.com/DotNetAnalyzers/StyleCopAnalyzers /blob/master/documentation/SA1202.md

复制标记为“如何抑制违规”的行。将属性粘贴到 Style Cop 抱怨的类上方

[SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1202:ElementsMustBeOrderedByAccess", Justification = "Reviewed.")]

Read the admonition from Style Cop, looking for the alphanumeric code. In your case 'SA1202'. Then browse to the corresponding page on the Style Cop website. Change the URL as appropriate https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1202.md

Copy the line labelled 'How to Suppress Violations'. Paste the attribute above the class about which Style Cop moans

[SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1202:ElementsMustBeOrderedByAccess", Justification = "Reviewed.")]
尽揽少女心 2024-09-17 17:31:10

难道你不能直接删除规则而不弄脏你的代码吗?

FxCop 也是如此...

Cant you just remove the rule instead of soiling your code?

Same goes for FxCop...

单身情人 2024-09-17 17:31:10

1.
在您的情况下,正确的 SuppressMessage 属性应如下所示:

[SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1202:ElementsMustBeOrderedByAccess")]
private void SomeMethod()
{
}

请注意,您可以将其放置在任何其他元素上(例如,放置在类上 - 那么整个类中的所有类似违规行为都将被抑制)。

我也同意在这些领域写什么是相当不明显的。

实际上,第一个应该是StyleCop分析器类的完全限定名称,可以从源代码中找到(例如来自此处)。
第二个应该以规则代码开头,然后是冒号和规则枚举的名称(幸运的是,它总是看起来像设置编辑器中显示的规则名称,但带有没有空格)。

2.
关于“全局”抑制规则 - 为什么不通过设置编辑器将它们关闭?设置文件是通过文件系统继承的,因此您可以轻松地在文件夹结构的“顶部”放置一个“主”设置文件,以及一些其他文件(与主文件保持“差异”),但某些项目除外,如果您愿意(如此处所述)。

祝你好运!

1.
In your case, correct SuppressMessage attribute should like like the following:

[SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1202:ElementsMustBeOrderedByAccess")]
private void SomeMethod()
{
}

Note that you can place it on any other element (e.g, on the class - then all similar violations in the entire class will be supressed).

I also agree that it's quite unobvious what to write in these fields.

Actually, the first one should be the fully qualified name of StyleCop analyser class and could be found from the source code (e.g. from here).
The second one should start with rule code, then colon and the name of the rule enumeration (luckily, it always looks like the rule name displayed in the Settings Editor, but with no whitespaces).

2.
Regarding suppressing rules "globally" - why don't just turn them off via Settings Editor? Settings files are inherited through the file system, so you could easily have one "main" settings file at the "top" of your folder structure, and some other files (holding the "difference" from main) with exceptions made for some projects, if you want so (like described here).

Good luck!

ㄟ。诗瑗 2024-09-17 17:31:10

您可以在项目根文件夹中的 Settings.StyleCop 文件中禁用您不需要的规则。
您将需要包含规则的命名空间,可以在此处找到:
http://stylecop.soyuz5.com/StyleCop%20Rules.html

Settings.stylecop文件代码供您参考:

<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>

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>
若能看破又如何 2024-09-17 17:31:10

或者,您可以将区域中的代码移至部分类中。那么 stylecop 规则的问题就会消失。

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

年华零落成诗 2024-09-17 17:31:10

除了已有的有用答案之外:

如果您在抑制文件 GlobalSuppressions.cs 中抑制警告,
您可以编辑 [assemble: SuppressMessage(StyleCop...blabla 行并完全删除 Scope=...Target=... > 标签使得抑制在项目中全局化。

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.

别靠近我心 2024-09-17 17:31:10

README.mdStyleCop.Analyzers Visual Studio 2015+ 使用的 NuGet 包包含指向 规则文档。每条规则的文档都包含“如何抑制违规行为”部分。对于 SA1202 规则,选项包括

[SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1202:ElementsMustBeOrderedByAccess", Justification = "Reviewed.")]

#pragma warning disable SA1202 // ElementsMustBeOrderedByAccess
#pragma warning restore SA1202 // ElementsMustBeOrderedByAccess

The README.md for the StyleCop.Analyzers NuGet package used by Visual Studio 2015+ contains a link to the documentation for the rules. The documentation for each rule contains a "How to suppress violations" section. For the SA1202 rule, the options are:

[SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1202:ElementsMustBeOrderedByAccess", Justification = "Reviewed.")]

and

#pragma warning disable SA1202 // ElementsMustBeOrderedByAccess
#pragma warning restore SA1202 // ElementsMustBeOrderedByAccess
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文