Wrong usage of IsNotDeadCodeAttribute

Rule ND1703 Dead Code

NDepend.Attributes.IsNotDeadCodeAttribute (from NDepend.API.dll, replaceable by your own attribute) marks elements not used directly but used somehow (like through reflection), for the Potentially dead Types/Methods/Fields rules.

This rule matches elements tagged with this attribute that are nevertheless used directly somewhere in the code.

How to fix

Remove the now-useless IsNotDeadCodeAttribute tagging of matched elements.

CQLinq source

This rule 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.

// <Name>Wrong usage of IsNotDeadCodeAttribute</Name>
// <Id>ND1703:WrongUsageOfIsNotDeadCodeAttribute</Id>

warnif count > 0

let tAttr = Types.WithFullName("NDepend.Attributes.IsNotDeadCodeAttribute").FirstOrDefault()
where tAttr != null

// Get types that do a wrong usage of IsNotDeadCodeAttribute
let types = from t in Application.Types where 
   t.HasAttribute("NDepend.Attributes.IsNotDeadCodeAttribute".AllowNoMatch()) &&

   ( // types used don't need to be tagged with IsNotDeadCodeAttribute!
     t.TypesUsingMe.Count(t1 =>
        !t.NestedTypes.Contains(t1) &&
        !t1.IsTestCode &&   // test users don't count: the attribute also exempts from the rule 'Code used only by tests'
        !t1.HasAttribute("NDepend.Attributes.IsNotDeadCodeAttribute".AllowNoMatch()) ) > 0  ||

     // Static types that define only const fields cannot be seen as used in IL code.
     // They don't need to be tagged with IsNotDeadCodeAttribute.
     (t.IsStatic && t.NbMethods == 0 && !t.Fields.Where(f => !f.IsLiteral).Any())
   )
   select t

// Get methods that do a wrong usage of IsNotDeadCodeAttribute
let methods = from m in Application.Methods where 
   m.HasAttribute("NDepend.Attributes.IsNotDeadCodeAttribute".AllowNoMatch()) &&
   m.MethodsCallingMe.Count(m1 => !m1.IsTestCode && !m1.HasAttribute("NDepend.Attributes.IsNotDeadCodeAttribute".AllowNoMatch())) > 0
   select m

// Get fields that do a wrong usage of IsNotDeadCodeAttribute
let fields = from f in Application.Fields where 
   f.HasAttribute("NDepend.Attributes.IsNotDeadCodeAttribute".AllowNoMatch()) &&
   f.MethodsUsingMe.Count(m1 => !m1.IsTestCode && !m1.HasAttribute("NDepend.Attributes.IsNotDeadCodeAttribute".AllowNoMatch())) > 0
   select f

from member in types.Cast<IMember>().Concat(methods).Concat(fields)
select new { 
   member,
   Debt = 4.ToMinutes().ToDebt(),
   Severity = Severity.Medium
}

//<Expl>
// The *{0}* shouldn't be tagged with *IsNotDeadCodeAttribute* because it is used in the context of analyzed code.
//</Expl>

//<Desc>
// **NDepend.Attributes.IsNotDeadCodeAttribute** (from *NDepend.API.dll*,
// replaceable by your own attribute) marks elements not used directly but
// used somehow (like through reflection), for the *Potentially dead
// Types/Methods/Fields* rules.
//
// This rule matches elements tagged with this attribute that are
// nevertheless used directly somewhere in the code.
//</Desc>

//<HowToFix>
// Remove the now-useless *IsNotDeadCodeAttribute* tagging of matched
// elements.
//</HowToFix>

Dead Code: all 5 items