How to suppress gendarme shortcomings?

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 this.

+7
static-analysis code-analysis static-code-analysis gendarme
source share
3 answers

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

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

[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 the .Rules.Naming.dll gendarme. A complete list of rules and their assembly names is here .

+5
source share

If you use a console runner, you can use the defect file (outside the source) to suppress any defects in the method, type, or assembly.

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

+1
source share

As far as I can see, there is no way to enable [SuppressMessage] in the gendarme (in 2.8). I grabbed the last source from GitHub because it did not work as described.

SupressMessageEngine is present in the code, and there are tests that run it by manually overriding Runner.Engines.Subscribe. But [EngineDependency (typeof (SuppressMessageEngine)]] does not apply to all compiled rules that he subscribed to when the gendarme actually works.

I also looked at the source to find a way to always subscribe to a specific engine through config - but it is not.

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

The only "workaround" I can come up with is to write a custom rule that, when called, adds a SuppressMessageEngine subscription and does nothing. Hacky yes, but this should work based on what I saw in their code.

FYI - just implemented this. You need to create your own custom rule, import Mono.Cecil and Gendarme.Framework and the target platform .NET 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 {} } 

Unfortunately, this will not entail FxCopCompatibility attributes (i.e. SupressMessage for the FxCop rule that matches the gendarme rule will also suppress the gendarme rule), but at least it allows the use of gendarme names for suppression.

0
source share

All Articles