Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio: Suppress warnings for all files in a namespace [duplicate]

I have following namespaces in my project.

enter image description here

I want to disable a specific warning on a specific namespace (lets say Project.ViewModels). I can disable a warning on one files by doing this in the GlobalSuppression.cs

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Formatting", "RCS1057:Add empty line between declarations.", Justification = "<Pending>", Scope = "type", Target = "~T:Project.ViewModels.MainViewModel.cs")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Formatting", "RCS1057:Add empty line between declarations.", Justification = "<Pending>", Scope = "type", Target = "~T:Project.ViewModels.TreeViewModel.cs")]

I tried to change Scope from type to namespace and namespaceanddescendants but it didn't work.

[assembly: SuppressMessage("Formatting", "RCS1057:Add empty line between declarations.", Justification = "<Pending>", Scope = "namespace", Target = "~T:Project.ViewModels")]

Any idea how this can be fixed? I am using Visual Studio 2017.

like image 553
fhnaseer Avatar asked May 16 '19 09:05

fhnaseer


People also ask

How do you suppress Code Analysis warnings?

You can suppress violations in code using a preprocessor directive, the #pragma warning (C#) or Disable (Visual Basic) directive to suppress the warning for only a specific line of code. Or, you can use the SuppressMessage attribute.

How do you suppress ca1716?

If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule. To disable the rule for a file, folder, or project, set its severity to none in the configuration file.


1 Answers

You shouldn't use the ~T: for a namespace, that seems to only be for types. For a usage example, you can see how it's not being used for the namespace in the .NET Core code here. Also, from the docs namespace only:

suppresses warnings against the namespace itself. It does not suppress warnings against types within the namespace as shown below:

Depending on your file hierarchy, you'll potentially want to use namespaceanddescendants as shown below:

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Formatting", "RCS1057:Add empty line between declarations.", Justification = "<Pending>", Scope = "namespaceanddescendants", Target = "Project.ViewModels")]
like image 174
Joel B Avatar answered Oct 03 '22 16:10

Joel B