Suppressing rules

Jan 5, 2011 at 2:48 PM
Edited Jan 5, 2011 at 3:07 PM

Hi,

Thanks for your excellent extension to StyleCop, it's saved us some time writing our own ruleset :)

 

Have an issue though that thought I'd query you about.

How do you suppress a rule?

Marking an offending entity with the SuppressMessage attribute does not work.

 

I see (from using Reflector) that your rules do not inherit from Microsoft.StyleCop.SourceAnalyzer, so I'm guessing that is the reason.

Why is this?

 

We have a data access layer that we use to wrap some of the .NET libraries, and so are forced to follow MS's naming schemes in some instances.

Namely DbType and IsDBNull conflicting with each other.

 

I also notice your source code is not in your repo, or at least I cannot get anonymous access.

I could obviously dump the code from Reflector and try and implement the required base classes myself but thought I'd ask before going that route.

 

Regards,

Westy

 

PS: I have a few other issues that I may query about, since I notice you are working towards release. Will try and compile them into a suitable post soon...

Coordinator
Jan 5, 2011 at 9:07 PM

Hi Westy,

First of all, thank you for your detailed feedback!

Regarding rule suppressing - it is the common feature of StyleCop engine.
SuppressMessage attribute allows suppressing any rule, whether it is original StyleCop rule or custom third-party rule.
What I mean is that if suppressing works with original rules - it will work with custom rules as well.

For example, StyleCop+ naming rules (aka Advanced Naming Rules) could be suppressed with the following construction:

[SuppressMessage("Shuruev.StyleCop.CSharp.StyleCopPlus", "SP0100:AdvancedNamingRules", Justification = "Some justification here.")]

You could also try suppressing some original rules as well.
If suppressing doesn't work, could you please share a piece of code illustrating it?

Regarding your guess about incorrect inheritance - I'm afraid you're not quite right.
It seems you were a bit confused by looking at the classes that perform checking, but are not analyzers technically.
As you could see in settings dialog, StyleCop+ is represented by one analyzer.
In source code terms, it is StyleCopPlus class, and of course, it is inherited from SourceAnalyzer - otherwise, your custom rules wouldn't be loaded at all ;)

And here is one thing I'd like to pay your attention to.
For example, if talking about "IsDBNull" name, you could consider the following options possible instead of suppressing entities:

  1. Add "DB" into abbreviations list.
    In this case DB can be used in any names in your code (covered with this settings instance).
  2. Add "$(AaBb)DB$(AaBb)" line in "Method Names" setting.
    In this case you will allow methods (only methods) to be named as IsDBNull(), WriteDBValue(), ReadDBValue(), etc.
  3. Add "IsDBNull" line in "Method Names" setting.
    In this case you allow "IsDBNull" as a possible method name.

I'm not saying that you should use these options, but at least you could know about them.
With settings inheritance they provide you with really flexible possibilities.


Regarding source code - unfortunately it is currently unavailable by some reasons beyond my control.
Maybe situation will change in future. Of course, you can use Reflector to obtain any things you want.
However publishing the question here may be much more productive ;)

Thank you once again for your feedback!
Don't hesitate to ask any other questions, report about bugs, or propose new features.

Best regards,
Oleg Shuruev

Jan 6, 2011 at 8:47 AM

Hi Oleg,

Thanks for your prompt reply!

 

Ahh, looks like we were using the wrong category name (i.e. namespace) for the rule suppression; again I used Reflector to try and figure it out. We have been successfully suppressing standard StyleCop rules so sorry that I assumed something wrong with your rules...

We don't use the edition of VS that gives you the nice right-click and suppress option, so have to put them in by hand, hence our mistake.

 

Thanks for your suggestions for coping with 'DB' in names. Unfortunately we do want to enforce the naming standard of them being uppercase, but obviously have to relax the rule when inheriting from framework classes, so think we'll stick with suppressing.

As you say though, handy to know that we can use those techniques in other cases where they could apply :)

 

Fair enough about the source code; I'm guessing you wrote this as part of your work and do not strictly 'own' it. Kudos for releasing the binaries to the community though!

I'll try and find some time to review the rules I have disabled and why, and get your comments on them at some point soon.

I imagine I may have some suggestions for 'nice to haves' aswell, and again would welcome your input.

 

Cheers,

Westy

Jul 18, 2011 at 9:44 PM

Doesn't appear to work

[SuppressMessage("StyleCopPlus.AdvancedNamingRules", "SP0100:AdvancedNamingRules", Justification = "Some justification")]

Thoughts?

Jul 19, 2011 at 12:13 PM

Does anyone know why this wouldn't work?

Thanks

Coordinator
Jul 19, 2011 at 12:28 PM
Edited Jul 19, 2011 at 12:30 PM

Hi clazette,

I guess it should be

[SuppressMessage("StyleCopPlus.StyleCopPlusRules", "SP0100:AdvancedNamingRules", Justification = "Some justification")]

Could you please try?

Thank you!

Best regards,
Oleg Shuruev

Jul 19, 2011 at 6:41 PM

Your suggestion worked. I tried to determine the proper namespace but reflector showed what I had posted. So, I'm not sure what's going on there.

Thanks

Coordinator
Jul 19, 2011 at 8:17 PM

StyleCopPlus.StyleCopPlusRules - namespace and name of analyzer class
SP0100:AdvancedNamingRules - rule code and rule name

StyleCop+ has only one analyzer class (named StyleCopPlusRules).
There is also a class named AdvancedNamingRules, but technically it's not an analyzer class.

And the rule for advanced naming checking has the same name (AdvancedNamingRules), which could have created a little bit confusion.

Hope that makes it more clear!

P.S. There is no need to use reflector - all source codes are on GitHub.

Best regards,
Oleg Shuruev

Nov 7, 2011 at 8:59 PM

shuruev, in an above post, you said:

Add "DB" into abbreviations list.

I have added my abbreviations to StyleCop+ abbreviations list, but they aren't being honored within the StyleCop for Resharper options.  I still get a warning for method names that contain the abbreviations.  It appears that the error do not show up when I run StyleCop itself, just in the real time view of the code.  Is it possible to get the abbreviations to be honored there?

Coordinator
Jan 16, 2012 at 6:04 PM

Hey gorter,

What I think is that they probably aren't honored cause StyleCop for ReSharper doesn't honor them.
It seems I cannot do anything with this currently.

Best regards,
Oleg Shuruev