开发者

How to Suppress Gendarme Defects?

开发者 https://www.devze.com 2022-12-26 15:32 出处:网络
Is it possible to suppress a specific gend开发者_JS百科arme defect message? I would like to do this in the source code with a flag or something like it.As poupou already noted, version 2.10 supports t

Is it possible to suppress a specific gend开发者_JS百科arme defect message? I would like to do this in the source code with a flag or something like it.


As poupou already noted, version 2.10 supports the [SuppressMessage] attribute.

For example, to suppress the AvoidNonAlphanumericIdentifierRule rule, do this:

[SuppressMessage("Gendarme.Rules.Naming", "AvoidNonAlphanumericIdentifierRule")]
protected void Application_Start()
{
     ...
}

Note that you need to specify the name of the assembly where the rule lives... in this case, AvoidNonAlphanumericIdentifierRule lives in Gendarme.Rules.Naming.dll. The full list of rules and their assembly names are here.


If you use the console runner then you can use a defect file (out-of-source) to suppress any defect on a method, type or assembly.

The new Gendarme 2.8 has basic (read incomplete and buggy) support for the [SuppressMessage] attribute (same as fxcop). Expect this feature to work properly once 2.10 is released.


As far as I can see, there is no way to enable [SuppressMessage] in Gendarme (at 2.8). I grabbed the latest source off of GitHub, because it wasn't working as described.

The SupressMessageEngine is there in the code, and there are tests exercising it via a manual override of Runner.Engines.Subscribe. But the [EngineDependency (typeof(SuppressMessageEngine))] isn't applied to all of the compiled rules, which is how it gets subscribed to when Gendarme actually runs.

I also looked at the source to find a way to always subscribe to a particular engine via config -- but there is none.

I could be wrong, but it looks like an oversight that they forgot to go back and apply the appropriate EngineDependency attributes.

The only "workaround" that I can think of is to write a custom rule that when called, adds a subscription SuppressMessageEngine and doesn't do anything else. Hacky yes, but this should work based on what I've seen in their code.

FYI -- Just implemented this. You need to create your own custom rule, import Mono.Cecil and Gendarme.Framework and target .NET framework 3.5

using Gendarme.Framework;
using Gendarme.Framework.Engines;

namespace MyRules
{
   [Problem("Gendarme devs forgot to attribute rules with SuppressMessageEngine")]
   [Solution("Include this rule")]
   [EngineDependency(typeof(SuppressMessageEngine))]
   public class AddSuppressMessageSupportRule : Rule {}
}

Sadly, this won't pull in the FxCopCompatibility attributes that are there (i.e. a SupressMessage for a FxCop rule that matches a Gendarme rule will also suppress the Gendarme rule), but at least it lets you use the Gendarme names to suppress.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号