Pure methods should be tagged with PureAttribute

Rule ND1912 Immutability

A pure method changes no instance or static field, simplifying code by limiting side-effects. This query lists pure methods not tagged with a PureAttribute. The attribute expresses in source code that a method is and should remain pure — an intention continuously checked by the rule Methods tagged with PureAttribute must be pure.

Not a rule by default: many methods would match, and fixing plus maintaining that may counter-balance the benefits.

How to fix

Tag matched methods with NDepend.Attributes.PureAttribute (from NDepend.API.dll), System.Diagnostics.Contracts.PureAttribute (honored by the compiler only with Microsoft Code Contract enabled), or your own attribute (adapting this query).

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>Pure methods should be tagged with PureAttribute</Name>
// <Id>ND1912:PureMethodsShouldBeTaggedWithPureAttribute</Id>

// warnif count > 0   <-- not a code rule per default

from m in Application.Methods where 
  !m.IsGeneratedByCompiler &&
  !m.HasAttribute ("NDepend.Attributes.PureAttribute".AllowNoMatch()) && 
  !m.HasAttribute ("System.Diagnostics.Contracts.PureAttribute".AllowNoMatch()) &&
  !m.ChangesObjectState && !m.ChangesTypeState &&
   m.NbLinesOfCode > 0
select new {
   m, 
   m.NbLinesOfCode,
   Debt = 3.ToMinutes().ToDebt(),
   Severity = Severity.Low
}

//<Expl>
// The *{0}* is pure which makes it easy to work with, especially 
// in concurrent scenarios.
// Tag this method with *System.Diagnostics.Contracts.PureAttribute*.
// This way the intent to keep it pure is clearly
// expressed and regularly checked through the rule 
// *Methods tagged with PureAttribute must be pure*.
//</Expl>

//<Desc>
// A pure method changes no instance or static field, simplifying code by
// **limiting side-effects**. This query lists pure methods not tagged with
// a **PureAttribute**. The attribute expresses in source code that a method
// is and should remain pure — an intention continuously checked by the rule
// *Methods tagged with PureAttribute must be pure*.
//
// Not a rule by default: many methods would match, and fixing plus
// maintaining that may *counter-balance* the benefits.
//</Desc>

//<HowToFix>
// Tag matched methods with *NDepend.Attributes.PureAttribute* (from
// *NDepend.API.dll*), *System.Diagnostics.Contracts.PureAttribute* (honored
// by the compiler only with Microsoft Code Contract enabled), or your own
// attribute (adapting this query).
//</HowToFix>

Immutability: all 15 items