How to Suppress Gendarme Defects?

1.2k views Asked by At

Is it possible to suppress a specific gendarme defect message? I would like to do this in the source code with a flag or something like it.

3

There are 3 answers

0
Ethan J. Brown On

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
poupou On

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.

2
Lee Harold On

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.