Consider this code, which is contained within a single file:
namespace Foo
{
public partial class One
{
}
}
namespace Baa
{
public partial class Two
{
}
}
Compiling this code results in two StyleCop warnings:
Suppressing SA1402 works as expected:
namespace Foo
{
[SuppressMessage("Microsoft.StyleCop.CSharp.MaintainabilityRules", "SA1402:FileMayOnlyContainASingleClass")]
public partial class One
{
}
}
namespace Baa
{
[SuppressMessage("Microsoft.StyleCop.CSharp.MaintainabilityRules", "SA1402:FileMayOnlyContainASingleClass")]
public partial class Two
{
}
}
Unfortunately, I cannot seem to suppress SA1403.
I've tried the following suppression above each of the classes (as per the SA1402 suppression):
[SuppressMessage("Microsoft.StyleCop.CSharp.MaintainabilityRules", "SA1403:FileMayOnlyContainASingleNamespace")]
I've also tried the following assembly level suppression, both at the top of the file and in my global suppression file (I realize that's not a sensible idea):
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.StyleCop.CSharp.MaintainabilityRules", "SA1403:FileMayOnlyContainASingleNamespace")]
But to no avail.
So, my question is: How do I suppress StyleCop warning SA1403?
Thanks,
E
P.S. Please suppress the urge to remind me that multiple namespaces in a single file demonstrates bad programming practice...
EDIT (20121127):
In line with Mightymuke's answer I've looked into the possibility of using a SourceFileList
.
We use the StyleCopOverrideSettingsFile
tag in our build, which points to a shared rules file. In line with Mightymuke's suggestion, I added the following to our file:
<SourceFileList>
<SourceFile>MyFileName.cs</SourceFile>
<Settings>
<Analyzers>
<Analyzer AnalyzerId="Microsoft.StyleCop.CSharp.MaintainabilityRules">
<Rules>
<Rule Name="FileMayOnlyContainASingleNamespace">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
</Rules>
</Analyzer>
</Analyzers>
</Settings>
</SourceFileList>
This did not work either.
I believe, however that this might be by design. I found a StyleCop bug report that suggested the SourceFileList
tag cannot be used at a level higher than project level.
With this information in mind I attempted to add a Settings.StyleCop
file at the project level.
Still no joy.
I'm now looking into the possibility that the StyleCopOverrideSettingsFile
tag is quashing every other settings file. This seems logical but, if it is, the behaviour I have seen would imply it's not possible to use a SourceFileList
in a build that also uses StyleCopOverrideSettingsFile
.
Alternatively, it may be simply that Settings.StyleCop
is the wrong file name or I've placed it in the wrong location (I put it next to the csproj file.
EDIT (20130425):
Eventually I gave up. The code in question was auto-generated by a visual studio template file (.TT) so I went to the additional effort of making the template code split out the namespaces into separate files.
You can right click on your project and choose StyleCop Settings. Popup will open and there you can customize warnings and choose what you want to suppress. In you case, warring SA1403 can be found on Maintainability Rules\File Contents
Beginning with StyleCop 4.4.0, it is also possible to suppress all of the rules within a rule namespace, using a single suppression attribute. This is indicated by replacing the rule CheckID and rule name with a single asterisk. The following code example suppresses all of StyleCop's default documentation rules within the inner class.
StyleCop violations are suppressed at the level to which an instance of the SuppressMessage attribute is applied. The purpose of this is to tightly couple the suppression information to the code where the violation occurs. For example, a StyleCop SuppressMessage attribute placed on a class will suppress the rule for all contents of the class.
Suppressions must be placed on a code element. One option though is to simply turn off the rules that you aren't interested in. This is the preferred method of global suppression. Show activity on this post. Seems that you probably need to change StyleCop configuration (settings) than to suppress some rules globally.
You need to set the scope of the suppression. In this case, use "module".
[module: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1403:FileMayOnlyContainASingleNamespace", Justification = "This is a generated file which generates all the necessary support classes.")]
I learned this from the following page, in the "Gobal-Level Suppressions" section: http://msdn.microsoft.com/en-us/library/ms244717.aspx#sectionToggle4
You will want to create a File List in your settings file.
From the documentation:
Now consider a situation where you’d like to disable some additional rules for a
couple of specific files in the project, Class3.cs and Class4.cs. In StyleCop
4.4, this can be accomplished by adding a <SourceFileList> section within the
settings file:
<SourceFileList>
<SourceFile>Class3.cs</SourceFile>
<SourceFile>Class4.cs</SourceFile>
<Settings>
<Analyzers>
<Analyzer AnalyzerId="Microsoft.StyleCop.CSharp.MaintainabilityRules">
<Rules>
<Rule Name="AccessModifierMustBeDeclared">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
</Rules>
</Analyzer>
</Analyzers>
</Settings>
</SourceFileList>
In your specific case it should look something like:
<SourceFileList>
<SourceFile>YourFile.cs</SourceFile>
<Settings>
<Analyzers>
<Analyzer AnalyzerId="Microsoft.StyleCop.CSharp.MaintainabilityRules">
<Rules>
<Rule Name="FileMayOnlyContainASingleClass">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
<Rule Name="FileMayOnlyContainASingleNamespace">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
</Rules>
</Analyzer>
</Analyzers>
</Settings>
</SourceFileList>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With