I am trying to uses the GlobalSuppressions file to suppress a specific rule on a specific namespace on my test project, like so:
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "CC0061", Scope = "namespace", Target = "CodeCracker.Test.Design", Justification = "No need for tests to have async sufix.")]
But it does not work. The diagnostic keeps being generated. The method specific works:
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "CC0061:TaskNameAsyncAnalyzer", Justification = "No need for tests to have async sufix.", Scope = "member", Target = "~M:CodeCracker.Test.Design.CopyEventToVariableBeforeFireTests.IgnoreMemberAccess")]
I guess this is a bug, right?
@giggio This is as per the design of FxCop source level suppressions, and we have retained that design in Roslyn. Please see the note in section "Global-Level Suppressions" in here: https://msdn.microsoft.com/en-us/library/ms244717.aspx
"When you suppress a warning with namespace scope, it suppresses the warning against the namespace itself. It does not suppress the warning against types within the namespace."
@srivatsn By Design?
@shyamnamboodiripad Can you double check if we are fine with the current design for suppress message attribute applied with namespace target? Seems reasonable to me..
Let's stick to the FxCop convention for the suppressmessage attribute otherwise we will end up breaking a bunch of suppressions in existing code.
I agree it makes sense to stick with the FxCop convention in this case.
Ok, it makes sense not to break everyone. But how would then I suppress diagnostics against types inside the namespace? Is that at all possible? Or will I have to add each one individually? This would be really annoying.
You can suppress a diagnostic on the entire type in one of the following ways:
1) using Scope = "type" and Target = "~T:
2) By explicitly applying the attribute on the type node without any scope or target.
If either approach doesn't work, its a bug that must be fixed.
But yes, you would have to do it on individual type. Alternatively you can suppress it on the entire assembly by having as assembly attribute with no scope and target.
Ok, this is insufficient. The ability to suppress a diagnostic on a specific namespace, including all its types, is really important. I am seeing that there is no way to do this today? May I suggest that feature? :)
Just as a follow up, I posted the idea to uservoice: https://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/7091554-allow-suppressmessageattribute-to-work-types-withi
We could just add a new scope called "TypesInNamespace" and specifying a namespace there should suppress in all types in that namespace.
Created #486 to track this request.
Yes, I don't think implementing this is tough for us in Roslyn. However, it would be good to know the original thinking behind the current design. Probably Nate remembers, let me follow up with him.
I will tell you why I need it, and maybe it helps. You can see on my original post above that I am asking for it on the namespace CodeCracker.Test.Design
. This is a test assembly, CodeCracker.Test.dll
, and the specific diagnostic we want to suppress is the one that requires the Async
suffix for async methods. We don't want to have to add Async
to the end of every test method, it looks ugly, and it is unnecessary. And I want the diagnostic reported on all other methods that are not direct test methods, like test helpers, etc, as I want to know if they are async or not.
Right now we suppressed the diagnostic on the whole assembly, but it is not ideal. Ideally we would suppress only the test namespaces and that would be it.
Thanks for following up on this on #486. :)
Most helpful comment
Ok, this is insufficient. The ability to suppress a diagnostic on a specific namespace, including all its types, is really important. I am seeing that there is no way to do this today? May I suggest that feature? :)