Method non-synchronized that read mutable states

Code Query ND2505 .NET Framework Usage / System.Threading

Lists methods reading mutable state (modifiable instance or static fields) without synchronizing access — a potential state corruption in multi-threaded programs. Not a rule: most matches are not an issue.

CQLinq source

This code query 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>Method non-synchronized that read mutable states</Name>
// <Id>ND2505:MethodNonSynchronizedThatReadMutableStates</Id>

from m in Application.Methods where 
 (m.ReadsMutableObjectState || m.ReadsMutableTypeState) && 
 !m.IsUsing ("System.Threading.Monitor".AllowNoMatch()) && 
 !m.IsUsing ("System.Threading.ReaderWriterLock".AllowNoMatch())
select new {
   m, 
   mutableFieldsUsed = m.FieldsUsed.Where(f => !f.IsImmutable)
}

//<Desc>
// Lists methods reading mutable state (modifiable instance or static
// fields) without synchronizing access — a potential state corruption in
// multi-threaded programs. Not a rule: most matches are not an issue.
//</Desc>

.NET Framework Usage / System.Threading: all 6 items