views:

884

answers:

2

I'm using StyleCop and FxCop tools to improve my code but I came to a place where there are two rules, one in StyleCop and one in FxCop that exclude each other. If I fix my code to match the rule from StyleCop then FxCop validation fails and vice versa.

First rule is SA1200 from StyleCop which says that all using directives must be placed inside of the namespace.

All using directives must be placed inside of the namespace.

So I have done something like this

namespace MyNamespace
{
    using System;

    ...
}

It was ok for StyleCop, no more warnings. Now I run FxCop validation and it tells me that CA1014 is violated:

Mark 'MyApp.dll' with CLSCompliant(true) because it exposes externally visible types.

To resolve this I should do something like this:

[ClsCompliant(true)]
namespace MyNamespace
{
    ...
}

but now I cannot build my project because ClsCompliant attribute is not recognized (because it's from System namespace which I include inside of the MyNamespace). So if I move using System; directive outside of MyNamespace declaration. This will make my code compile but again it will break the rule from StyleCop.

Is there any way to deal with this problem except for disabling one of the rules in StyleCop or FxCop? And if that's not possible which of the rules should I disable? Which is less important?

+10  A: 

Use full attribute name:

[System.CLSCompliant(true)]
namespace MyNamespace
{
    ...
}

BTW: if you want to mark your whole assembly as CLSCompliant, put

[assembly: System.CLSCompliant(true)]

in Properties/AssemblyInfo.cs file

maciejkow
I think the assembly should be marked CLSCompliant as maciejkow pointed out instead of marking the namespace compliant.
David Silva Smith
A: 

My suggestion is to turn off the "All using directives must be placed inside of the namespace." rule in StyleCop. It's impractical to adhere to it, especially since most of the code generators (even VS own ones) do not follow this practice.

Igor Brejc
I think it's better to set StyleCop to just ignore generated files.
RaYell
Yes, if they are marked with the appropriate attribute. Unfortunately some of the code generators do not mark the generated code.
Igor Brejc