'SuppressMessage' for a whole namespace

C#.NetNamespacesCode AnalysisFxcop

C# Problem Overview


I use underscores for my test methods for a better readability and I want to suppress FxCop errors/warnings for the whole test namespace.

How can I achieve this? I played with GlobalSuppressions.cs but nothing worked:

[module: System.Diagnostics.CodeAnalysis.SuppressMessage(
    "Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores",
    Scope = "namespace", Target = "Company.Product.Tests")]

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage(
    "Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores",
    Scope = "namespace", Target = "Company.Product.Tests")]

C# Solutions


Solution 1 - C#

Suppression of a code analysis warning for a namespace and all its descendant symbols is possible since Visual Studio 2019:

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage(
    "Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores",
    Justification = "Test methods require underscores for readability."
    Scope = "namespaceanddescendants", Target = "Company.Product.Tests")]

> Scope - The target on which the warning is being suppressed. If the target is not specified, it is set to the target of the attribute. Supported scopes include the following: > > * ... > > * namespaceanddescendants - (New for Visual Studio 2019) This scope suppresses warnings in a namespace and all its descendant symbols. The namespaceanddescendants value is only valid for Roslyn analyzers, and is ignored by binary, FxCop-based static analysis.

Suppress code analysis warnings#SuppressMessage attribute @ MS Docs

Solution 2 - C#

You can use the "module" scope for this, which is supported by older compilers than the ones that support the newer "namespaceanddescendants" scope. The module scope impacts everything in the project, and it does not require a target specification.

Example Usage:

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage(
    "Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores",
    Justification = "Test methods require underscores for readability."
    Scope = "module")]

Solution 3 - C#

I have managed to ignore that particular warning specifically for Test projects (I follow a naming convention in which these always end up with "Tests.cs") by using a .editorconfig file, containing the following rule:

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

More information on my answer here

Solution 4 - C#

Yes, that's not possible with FxCop <= 10.0.

What you can do, is to disable CA1707 using a custom rules file (maybe just for your test projects).

Solution 5 - C#

As already said, it is not possible out of the box. Imho, it is intended because suppress have to be done unitarily.

There is a workaround to do this manually through FXCop 10 with the Copy As > Module-level SuppressMessage functionality.

Cons, you will have to repeat this each time the namespace is modified but as already said, the global suppressions should be isolated.

  1. Open your assembly in FXCop 10 (System.Xml here) and run analysis
  2. Select your namespace (System.Xml here)
  3. Select all violated rules
  4. Right click and Copy As > Module-level SupressMessage
  5. Then paste it in a GlobalSuppressions.cs for example

Note: this can be done at assembly, namespace or type level.

Module-level SuppressMessage

Solution 6 - C#

I think it is not possible as harlam357 already said.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontimmkrauseView Question on Stackoverflow
Solution 1 - C#LeniaalView Answer on Stackoverflow
Solution 2 - C#carlin.scottView Answer on Stackoverflow
Solution 3 - C#ccoutinhoView Answer on Stackoverflow
Solution 4 - C#ulrichbView Answer on Stackoverflow
Solution 5 - C#JoeBillyView Answer on Stackoverflow
Solution 6 - C#timmkrauseView Answer on Stackoverflow