Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StyleCop and FxCop rules exclude each other

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?

like image 222
RaYell Avatar asked Sep 03 '09 07:09

RaYell


2 Answers

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

like image 119
maciejkow Avatar answered Nov 05 '22 15:11

maciejkow


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.

like image 3
Igor Brejc Avatar answered Nov 05 '22 14:11

Igor Brejc