Copied from this UserVoice request:
Right now the SuppressMessageAttribute only suppresses diagnostics against a namespace, but not the types within it.
So this:
C# [assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "CC0061", Scope = "namespace", Target = "CodeCracker.Test.Design", Justification = "No need for tests to have async sufix.")]
Would not work against types inside the
CodeCracker.Test.Design
namespace, but only if the diagnostic was on the namespace itself. To be able to do that, we need to add a SuppressMessageAttribute to each type individually, what is error prone, ugly and demands a lot of attention.
There needs to be a way to allow for the suppression of diagnostics agains all types within a namespace. It should be a single line of code, just like the one above, but I don't want to suggest a syntax or format, and leave that to the C# team.
We could have a new scope called TypesInNamespace which would suppress within all types in that namespace.
I just wanted to confirm the points about the design by @mavasani @srivatsn and @shyamnamboodiripad. When we originally designed the suppress message functionality for Roslyn, we implemented the Scope = Namespace behavior to mimic that of FxCop to avoid breaking existing suppressions. It seems reasonable to add an additional scope for suppressing a message on all namespace members.
Is there any hope of this being added? I'd also like to see this for a few use cases.
The idea is sound but we don't have this planned for the immediate future.
This is still something I run into weekly, with the community adding analyzers via extensions and such, it's very useful to suppress certain ones globally. What's the best way to get this added? Or is there another built-in way I'm missing to suppress for the entire project?
Tagging @jinujoseph - we can consider this for 15.3. I'll take a quick look to see if it is easy to implement.
This approach would mean that for each analyzer you would like to suppress you would have to add assembly attribute into each project in your solution.
What is the reason for this approach instead of using ruleset file?
Ruleset file settings apply to all diagnostics, not to specific instances of diagnostics. If you want to suppress only in specific namespaces in a compilation, you have to do so with source suppression.
I am using VS 15.6.3, but still can't see this taking effect. Is this feature now available or when would it likely be available?
@jinujoseph does this meet the 15.8 bar?
No, lets target for 16
Another use case for this type of suppression -- I have a dotnet core web app that uses entity framework with code first migrations. Most of the generated migrations have some sort of violation in them, and to remedy this, I would love to suppress warnings for all types in the namespace MyApp.Core.Migrations.
Hopefully this makes it into 16, as adding suppressions for each type is both fragile and cumbersome.
I have a PR out for this: https://github.com/dotnet/roslyn/pull/31092
Sorry, it has taken such a long time to work on such a frequently requested feature.
Also reported at DC link
Fixed with https://github.com/dotnet/roslyn/pull/31092
https://github.com/dotnet/docs/issues/9046 tracks adding documentation for the new scope “NamespaceAndChildren”.
@giggio 👋 Your user voice request!
FYI: The new scope has been renamed to NamespaceAndDescendants
(https://github.com/dotnet/roslyn/pull/31940)
Most helpful comment
Is there any hope of this being added? I'd also like to see this for a few use cases.