Types immutable should be tagged with ImmutableAttribute

Rule ND1910 Immutability

A type is immutable if its instance fields are only assigned inline or from constructor(s). This query lists immutable types not tagged with NDepend.Attributes.ImmutableAttribute. The attribute expresses in source code the intention that a class is and should remain immutable — an intention continuously checked by the rule Types tagged with ImmutableAttribute must be immutable.

How to fix

Tag matched types with ImmutableAttribute from NDepend.API.dll, 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>Types immutable should be tagged with ImmutableAttribute</Name>
// <Id>ND1910:TypesImmutableShouldBeTaggedWithImmutableAttribute</Id>

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

from t in Application.Types where 
  !t.HasAttribute ("NDepend.Attributes.ImmutableAttribute".AllowNoMatch()) && 
   t.IsImmutable && 
   // Don't need to tag record classes with [Immutable]
  !t.IsRecord &&
  !t.IsStructure // An immutable structure must be marked as readonly
select new { 
   t, 
   t.NbLinesOfCode,
   Debt = 3.ToMinutes().ToDebt(),
   Severity = Severity.Low
}

//<Expl>
// The *{0}* is immutable which makes it easy to work with, especially 
// in concurrent scenarios.
// Create an *ImmutableAttribute* attribute and tag this type with it.
// This way the intent to keep it immutable is clearly
// expressed and regularly checked through the rule 
// *Types tagged with ImmutableAttribute must be immutable*.
//</Expl>

//<Desc>
// A type is immutable if its instance fields are only assigned inline or
// from constructor(s). This query lists immutable types not tagged with
// **NDepend.Attributes.ImmutableAttribute**. The attribute expresses in
// source code the intention that a class is and should remain immutable —
// an intention continuously checked by the rule *Types tagged with
// ImmutableAttribute must be immutable*.
//</Desc>

//<HowToFix>
// Tag matched types with **ImmutableAttribute** from *NDepend.API.dll*, or
// your own attribute (adapting this query).
//</HowToFix>

Immutability: all 15 items