Get Rid of Issues that are False Positives

With NDepend, code rules are C# LINQ queries that match patterns in analyzed code to detect code smells and error-prone code. You might consider that some issues found are actually not something that need to be fixed: in such situation the issue is a false positive.

It is important to keep the number of false positives close to zero. Almost every new NDepend versions propose some false positive improvements in the default rules set.

If you believe that an NDepend default rule consistently produces some false positives please let us know at [email protected].

Else, here are the strategies to get rid of some false positives.

Suppress the issue

You can use the .NET attribute SuppressMessageAttribute to suppress an issue. This possibility is explained in the documentation Suppress some NDepend issues with SuppressMessageAttribute.

Disable the rule

If you estimate that a rule (or a group of rules) should not apply to your code you can just disable or delete it from the Rules Explorer panel.

Disabling a rule in the Rules Explorer panel

Change the rule

An NDepend code rule is a C# LINQ query prefixed with a warnif prefix. You can read details in the Write Your Own Code Rules documentation.

By default the NDepend UI shows the rule description and how-to-fix texts. But you can click the View Source Code button to view and edit the rule code.

View Rule Description vs. View Rule Source Code

For example the default code rule Potentially Dead Types already contains several filters to avoid matching ASP.NET MVC Controller classes or entity Framework ModelSnapshot classes. These classes are invoked by the infrastructure and without these filters they would be seen as dead code (unused code). Maybe more filters need to be added in rules to avoid some false positives in your context.

Filters to avoid matching types only used by the infrastructure

As this screenshot suggests often false positive filters are based on:

  • Naming: elements with such prefix or suffix shouldn't be matched
  • Object-Model: elements that derive from this class or implement this interface shouldn't be matched
  • Attribute: elements tagged with this attribute shouldn't be matched
  • Boolean Flag: static, public, nested, abstract, delegate... classes shouldn't be matched
  • Code Metric: classes with less than 5 members shouldn't be matched

Refine the JustMyCode code view

Most of default NDepend rules rely on the JustMyCode code view. This code view is obtained from code queries prefixed with notmycode in the Defining JustMyCode group.

As its name suggest the JustMyCode code view don't contain code generated by some tools like UI designers. Typically issues on generated code is a problem because by definition generated code cannot be changed manually. It means that issues on generated code cannot be fixed.

However if you want to get rid of issues on one or several code elements (assembly, namespace, class, method or field) you just have to modify the corresponding notmycode query (or create a new one) to make sure it matches those elements. For example if you match a class in a notmycode query, not only issues on the class itself will be suppressed but also issues on its methods and fields as well.