Unnecessary NDepend SuppressMessageAttribute Usage

Quality Gate Quality Gates

Finds SuppressMessageAttribute("NDepend", ...) usages that suppress no NDepend issue; they can be safely removed. Disabled by default (advanced usage only). On test code, only ND1611 suppressions are checked: no other default rule reports issues there. Doc: https://www.ndepend.com/docs/suppress-issues

CQLinq source

This quality gate is written in CQLinq, C# LINQ over the NDepend code model. It ships with every new NDepend project: you can read it, adapt it, or use it as a starting point for your own custom rules.

// <QualityGate Name="Unnecessary NDepend SuppressMessageAttribute Usage" Unit="usages" />
failif count > 50
warnif count > 0
// There can be several SuppressMessageAttribute
// in System.Runtime, netstandard and mscorlib for example
let suppressAttributes = ThirdParty.Types.
  WithFullName("System.Diagnostics.CodeAnalysis.SuppressMessageAttribute")

// Get the tags of SuppressMessageAttribute used
// from suppressed issues
let ndpSuppressTagsUsed = 
  (from i in context.IssuesSet.AllSuppressedIssues 
   where i.Rule.Provider == RuleProvider.CodeQueryRule
   select i.SuppressAttributeTag).ToHashSetEx()

// Get all tags like SuppressMessageAttribute("NDepend"...)
let allNdpSuppressTags = suppressAttributes
  .SelectMany(sa => sa.TagsWithMeAsAttribute)
  // Filter only NDepend category
  .Where(s => s.ValueOf("Category") != null && 
              s.ValueOf("Category").AsString.StartsWith("NDepend"))

// Infer unnecessary tags like SuppressMessageAttribute("NDepend"...)
let ndpTagsSuppressUnused = allNdpSuppressTags.Except(ndpSuppressTagsUsed)

// Uncomment to get instead all tags that actually suppress one or several issues
//from tag in ndpSuppressTagsUsed
from tag in ndpTagsSuppressUnused
let checkId = tag.ValueOf("CheckId") == null ? null : tag.ValueOf("CheckId").AsString
where !tag.CodeElementTagged.IsThirdParty &&
      // Rules don't report issues on test code except ND1611 'Test methods should assert something':
      // there, only an ND1611 suppression can be unnecessary
      (!tag.CodeElementTagged.IsTestCode ||
       (checkId != null && (checkId == "ND1611" || checkId.StartsWith("ND1611:"))))
select new { 
  tag.CodeElementTagged, 
  tag.AttributeType,
  desc=tag.Parameters.Select(p =>p.Name +": "+p.Value.AsString).Aggregate(",    ") 
}

//<Desc>
// Finds SuppressMessageAttribute("NDepend", ...) usages that suppress no
// NDepend issue; they can be safely removed. Disabled by default (advanced
// usage only). On test code, only ND1611 suppressions are checked: no
// other default rule reports issues there.
// Doc: https://www.ndepend.com/docs/suppress-issues
//</Desc>

Quality Gates: all 19 items