How do I suppress StyleCop warning SA1403?

Montgomery 'monty' Jones picture Montgomery 'monty' Jones · Nov 22, 2012 · Viewed 7.2k times · Source

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:

  • SA1402:FileMayOnlyContainASingleClass
  • SA1403:FileMayOnlyContainASingleNamespace

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.

Answer

Richard Brent Arthur picture Richard Brent Arthur · Oct 25, 2013

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