如何抑制 StyleCop 警告?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
这是您需要的:
Here's what you need:
内联抑制的示例与此类似 - 检查与抑制相比的代码中的命名空间
全局抑制文件是项目根目录中名为 GlobalSuppressions.cs 的文件,可能如下所示:
您可以通过右键单击警告来自动生成此代码。
An example of inline suppression would be similar to this - examine the namespaces in the code compared to the suppression
A global supression file is a file in the root of your project named
GlobalSuppressions.cs
and might look like this:And you can generate this code automatically by right-clicking on the warning.
从 StyleCop 4.3.2 开始,可以通过在源代码中添加抑制属性来抑制规则违规的报告。
规则抑制
http://stylecop.soyuz5.com/Suppressions.html
但它说 -
全局抑制
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,你可以右键单击你的项目,就会有一个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.阅读 Style Cop 的警告,寻找字母数字代码。在你的情况下是“SA1202”。然后浏览至 Style Cop 网站上的相应页面。根据需要更改 URL https://github.com/DotNetAnalyzers/StyleCopAnalyzers /blob/master/documentation/SA1202.md
复制标记为“如何抑制违规”的行。将属性粘贴到 Style Cop 抱怨的类上方
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
难道你不能直接删除规则而不弄脏你的代码吗?
FxCop 也是如此...
Cant you just remove the rule instead of soiling your code?
Same goes for FxCop...
1.
在您的情况下,正确的
SuppressMessage
属性应如下所示:请注意,您可以将其放置在任何其他元素上(例如,放置在类上 - 那么整个类中的所有类似违规行为都将被抑制)。
我也同意在这些领域写什么是相当不明显的。
实际上,第一个应该是StyleCop分析器类的完全限定名称,可以从源代码中找到(例如来自此处)。
第二个应该以规则代码开头,然后是冒号和规则枚举的名称(幸运的是,它总是看起来像设置编辑器中显示的规则名称,但带有没有空格)。
2.
关于“全局”抑制规则 - 为什么不通过设置编辑器将它们关闭?设置文件是通过文件系统继承的,因此您可以轻松地在文件夹结构的“顶部”放置一个“主”设置文件,以及一些其他文件(与主文件保持“差异”),但某些项目除外,如果您愿意(如此处所述)。
祝你好运!
1.
In your case, correct
SuppressMessage
attribute should like like the following: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!
您可以在项目根文件夹中的 Settings.StyleCop 文件中禁用您不需要的规则。
您将需要包含规则的命名空间,可以在此处找到:
http://stylecop.soyuz5.com/StyleCop%20Rules.html
Settings.stylecop文件代码供您参考:
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:
或者,您可以将区域中的代码移至部分类中。那么 stylecop 规则的问题就会消失。
Alternatively you could move the code in regions into partial classes. Then the issue with the stylecop rule will go away.
除了已有的有用答案之外:
如果您在抑制文件
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 theScope=...
andTarget=...
tags. That makes the suppression global in the project.README.md 的 StyleCop.Analyzers Visual Studio 2015+ 使用的 NuGet 包包含指向 规则文档。每条规则的文档都包含“如何抑制违规行为”部分。对于 SA1202 规则,选项包括
:
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:
and