Suppress a warning for all projects in Visual Studio

前端 未结 6 1258
心在旅途
心在旅途 2021-01-04 00:48

I\'ve seen answers showing how to suppress a warning for a specific line of code or for a specific project. I don\'t want that.

I want to suppress a specific warning

相关标签:
6条回答
  • 2021-01-04 01:01

    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.

    0 讨论(0)
  • 2021-01-04 01:02

    I would like to add to Stephen's post that his solution with the .editorconfig file didn't work out for me without specifying the files I want to apply the rule to. For example, and given that I want to apply a rule to all the test files and that I follow a naming convention in which these end up with "Tests.cs", I have managed to ignore the CA1707 rule in those files by using the following rule:

    [*Tests.cs]
    dotnet_diagnostics.CA1707.severity = none
    

    More information on my answer here

    0 讨论(0)
  • 2021-01-04 01:03

    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
    
    0 讨论(0)
  • 2021-01-04 01:08

    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:

    If you're creating in a text editor you'll probably need this at the top of the file, adjusted as necessary:

    # Remove the line below if you want to inherit .editorconfig settings from higher directories
    root = true
    
    # C# files
    [*.cs]
    

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

    0 讨论(0)
  • 2021-01-04 01:18

    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?

    0 讨论(0)
  • 2021-01-04 01:21

    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.

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