26

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 for all of my projects.

(If it matters, the warning is IDE0044. And I'm using C#.)

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
ispiro
  • 26,556
  • 38
  • 136
  • 291
  • 1
    You can create a source file in the solution directory with the `[SuppressMessage]` attribute in it. Then include it in each project with "Add Existing File... Add As Link". – Ron Beyer May 17 '18 at 20:06
  • @RonBeyer That's not exactly what I'm looking for, but thanks for the workaround. – ispiro May 17 '18 at 20:08

6 Answers6

18

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.

Image of the text editor preferences.

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
John Koerner
  • 37,428
  • 8
  • 84
  • 134
5

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?

Rahul
  • 76,197
  • 13
  • 71
  • 125
  • 3
    Thanks. But that only works for one project. Please correct me if I'm wrong. (I'm not the downvoter, though.) – ispiro May 17 '18 at 19:53
  • @ispiro yes as you have said *specific line of code* see the linked documentation for more information – Rahul May 17 '18 at 19:55
  • 1
    @Rahul he did say "specific line of code", but you have the context wrong. – mxmissile May 17 '18 at 20:00
  • 1
    @mxmissile, not entirely as OP says *how to suppress a warning for a specific line of code or for a specific project* – Rahul May 17 '18 at 20:02
  • 1
    Thanks. But @mxmissile is right. I just meant that I've already seen those, and that that is not what I'm looking for. I edited the question to be a little clearer. – ispiro May 17 '18 at 20:06
  • @ispiro don't think you can suppress message likewise and edited the post with the same statement – Rahul May 17 '18 at 20:09
  • See my answer for how to disable this in VS. – John Koerner May 18 '18 at 03:46
5

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.

Uladzislaŭ
  • 1,680
  • 10
  • 13
5

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

enter image description here

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

enter image description here

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

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
3

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

ccoutinho
  • 3,308
  • 5
  • 39
  • 47
  • 1
    I didn't post my complete file as I made it with VS and it's large, but I do indeed have something similar at the top (`[*.cs]`). Turns out it's important! I've added the info to my answer for people who are making the config file manually. Thanks for the feedback. – Stephen Kennedy Apr 09 '20 at 14:41
  • I think there is a small typo: it should be dotnet_diagnostic and not dotnet_diagnostics. https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/configuration-files – bkqc Aug 23 '22 at 19:06
-3

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.

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
BlythMeister
  • 299
  • 1
  • 12
  • 5
    Because in some situations readonly is not the best modifier to use (on arrays, for example, because arrays can be modified even if that particular reference can't be). Also, there are some situations where it's perfectly legitimate and desirable to reassign fields, and IDE0044 is still reported on them. – sjcaged May 28 '18 at 02:07
  • I disagree. Even on an array, setting to read only means you are not going to get rid of the entire array. If got get this warning, it's telling you this it's completely safe to. With regards to the changing of arrays. Whilst an array of list is great inside a class, you should only return a read only collection to protect your array – BlythMeister May 28 '18 at 05:21
  • 2
    Sure, if you also go the route of making every field access be a propery access instead. There are non-array reasons to suppress the message, as well; regardless of whether you agree with them, the concept is that it's a style warning, not a compiler warning. It's an IDExxxx warning, not a CSxxxx warning. You may reasonably disagree with my dispute of its necessity, and I may reasonably disagree with your perception of its universal appropriateness. People can reasonably disagree, and reasonably desire to disable the warning; this question is about doing so as a matter of their own policy. – sjcaged May 28 '18 at 07:05
  • 4
    In a non-ideal, i.e. real, world, it's very easy to get stuck with a project with nearly 2k warnings, mostly unused variables etc. I'm not fixing all those, and they can safely be suppressed. – ProfK Sep 07 '18 at 07:17
  • 2
    Well many people use VS for unity c# programming. In which u can add a SerializeField attribute to show and modify variables in the inspector which VS is unable to detect and this warning gets really annoying. –  Feb 28 '19 at 16:24
  • Should any switch-case construction have a default section? Are you sure? – nim Apr 22 '19 at 11:50