16

Does any one of you know a way I'd be able to suppress e.g. CA2000 for an entire solution?

I'm thinking of something like a GlobalSuppressions class with the following line of code:

[assembly: SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope")]

Thanks for your help in advance.

bubbleking
  • 3,329
  • 3
  • 29
  • 49
GGMU
  • 161
  • 1
  • 3
  • Possible duplicate of [Suppress warning on solution level. Treat warning as error on solution level](https://stackoverflow.com/questions/18546635/suppress-warning-on-solution-level-treat-warning-as-error-on-solution-level) – Greg Apr 11 '18 at 00:02
  • Important to note, that you can edit the file that handles those. You can extend outward, ReSharper and other platforms already do this, but I believe the answer already exists here on Stack Overflow. – Greg Apr 11 '18 at 00:03
  • 3
    I wouldn't be surprised if in a month you are asking why you get a memory leak – Camilo Terevinto Apr 11 '18 at 00:12
  • 2
    There is a really good reason why you don't want to do this on a global level. – Ron Beyer Apr 11 '18 at 00:44
  • 2
    why do you want to suppress this? – Keith Nicholas Apr 11 '18 at 00:55
  • Because it is my task to do so. Some of our head devs gave me the tasks. – GGMU Apr 11 '18 at 01:22
  • Thanks for your help! It is done now – GGMU Apr 11 '18 at 01:22

3 Answers3

7

Global Suppressions can be configured in .editorconfig file.

  1. Create a file called .editorconfig in the root folder of your solution
  2. Open in a text editor
  3. Add root = true in first line
  4. Below that line add the rules to disable
    [*.{cs,vb}]
    dotnet_diagnostic.<Rule_Code>.severity=silent
    

Here is an example .editorconfig file

root = true

[*.{cs,vb}]
dotnet_diagnostic.CA2000.severity=silent

[*.{cs,vb}]
dotnet_diagnostic.MA0004.severity=silent    # Even works with 3rd part rules/analyser 
Vijay Nirmal
  • 5,239
  • 4
  • 26
  • 59
4

In case anyone stumbles on this, it is possible to create a global suppressions file (Eg: GlobalSuppressions.cs), add it "as link" to each project you need to. It should contains lines like: [assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Reliability", "CA2007:Consider calling ConfigureAwait on the awaited task", Justification = "ASP.NET Core doesn't use thread context to store request context.", Scope = "module")] For more details see: https://learn.microsoft.com/en-us/visualstudio/code-quality/in-source-suppression-overview?view=vs-2022#suppress-violations-using-a-global-suppression-file

mBardos
  • 2,872
  • 1
  • 19
  • 16
2

The best way of doing this is by creating a Directory.Build.props file which will be automatically included in all projects in sub-folders to where it is located. Then add the following to the file:

<?xml version="1.0" encoding="utf-8" ?>
<Project>
  <!-- This file will be automatically included in all projects because of its name -->
  <PropertyGroup>
    <NoWarn>CA2000</NoWarn>
  </PropertyGroup>
</Project>
Fredrik C
  • 570
  • 6
  • 22