Suppress a warning for all projects in Visual Studio

不想你离开。 提交于 2019-12-05 00:02:38

A recent update to Visual Studio 2017 (15.7.1) has an option for this now. Under the Tools->Options menu, select the TextEditor->C#->Code Style->General tab. Under Field preferences, there is a Prefer readonly option. Set that to No.

There is also an editorconfig setting you can set if you want to check this preference in along side your code, so others who consume your code don't get the warning, but that has to be done on a per solution basis. The editorconfig value you would set would be:

 dotnet_style_readonly_field = false:none

You can use the SuppressMessage attribute present under System.Diagnostics.CodeAnalysis namespace like

[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "args")]

Well as you have edited saying I want to suppress a specific warning for all of my projects

You can't do that for a entire project wise AFAIK. But check the linked post once if that helps

How to suppress code analysis messages for all type members?

BlythMeister

IDE0044 is "add readonly modifier" so why not just add the modifier?

Warnings are telling you that you're doing something wrong, but the app will compile.

It's best to have zero warnings in an ideal world.

You may try to use Directory.Build.props adding NoWarn property for specific warnings. I haven't verified it though.

And as it's said in another answer, it's better to fix the root cause instead of ignoring it.

To suppress warnings for all projects, you need to create a .editorconfig file in a top-level directory. For example, I have mine in trunk and I commit it to source control so that my colleagues share the same settings.

The settings in this file apply to all projects in trunk and subfolders, unless overridden by another .editorconfig file further down the folder tree e.g. you might you have a project specific EditorConfig file in a subfolder which has different settings. See File hierarchy and precedence for more details.

Creating an EditorConfig file

You can use a text editor for this if you just want to change one specific setting. However, Visual Studio can create a .editorconfig file with sensible defaults for .NET for you. From MSDN:

  • Create a new project

  • From the menu bar, choose Project > Add New Item; or press Ctrl+Shift+A

  • Select the editorconfig File (.NET) template to add an EditorConfig file prepopulated with default .NET code style, formatting, and naming conventions

  • Optionally delete the project - we don't really need it

Visual Studio 2019 - Creating an EditorConfig file from current settings

In Visual Studio 2019, you can instead create an EditorConfig file from your current settings. Just click the following button in the Options dialog under Text Editor > C# > Code Style > General:

Disabling IDE0044 in the editor config file

To disable IDE0044 specifically, add or change the following setting in the .editorconfig file:

dotnet_style_readonly_field = false:none

(In Visual Studio 2019, you can set the Prefer readonly option to No under TextEditor-> C# -> Code Style-> General in Options and then press the Generate .editorconfig file from settings button as detailed above).

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