Immutable struct should be declared as readonly

Rule ND1914 Immutability

Declare immutable structures as readonly struct: the compiler and JIT learn the immutability intent and can optimize, e.g. passing/returning instances by-reference instead of copying them (no mutation risk). More: https://learn.microsoft.com/en-us/dotnet/csharp/write-safe-efficient-code#declare-immutable-structs-as-readonly

How to fix

Declare matched structures as readonly struct.

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>Immutable struct should be declared as readonly</Name>
// <Id>ND1914:ImmutableStructShouldBeDeclaredAsReadonly</Id>
warnif count > 0 
from t in JustMyCode.Types
//   Match structures that are immutable but not declared as 'readonly struct'
where t.IsStructure && 
      t.IsImmutable && 
     !t.IsReadOnly &&  
     !t.IsGeneratedByCompiler
select new { 
   t,
   Debt = 2.ToMinutes().ToDebt(),
   Severity = Severity.Medium
}

//<Expl>
// The *{0}* should be declared as *readonly*.
// This way not only the intent is clearly expressed and some optimizations 
// can be performed by the compiler and the runtime.
//</Expl>

//<Desc>
// Declare immutable structures as **readonly struct**: the compiler and JIT
// learn the immutability intent and can optimize, e.g. passing/returning
// instances by-reference instead of copying them (no mutation risk). More:
// https://learn.microsoft.com/en-us/dotnet/csharp/write-safe-efficient-code#declare-immutable-structs-as-readonly
//</Desc>

//<HowToFix>
// Declare matched structures as **readonly struct**.
//</HowToFix>

Immutability: all 15 items