r/dotnet • u/kralmoriaty • 23h ago
I built a Roslyn analyzer for .NET that enforces architecture rules and flags allocations the compiler hides
I have been maintaining Clean Architecture codebases where the dependency
rules exist only in a wiki, and they rot anyway. So I built RungerHeimdall:
a Roslyn analyzer that enforces the rules at compile time.
What it does
Architecture (8 rules): layers declared in architecture.json, dependency
direction enforced, forbidden types and namespaces, namespace-must-match-folder,
repositories/services must be interfaces, framework allow-list, no static
mutable state, endpoints cannot construct infrastructure types.
Performance (20 rules): 12 deterministic allocation rules and 8 context-aware
ones. Examples: empty params array, ToList() before foreach, string concat in a
loop, struct compared through EqualityComparer<T>.Default (boxes), foreach over
non-generic IEnumerable of value types, Task.Run inside async, async void,
blocking on Task, repeated enumeration, ValueTask awaited twice,
Task.FromResult in a loop, Array.Resize in a loop.
Adoption: heimdall-baseline.json freezes existing violations so you only see
new ones. Temporary exemptions require a reason and can expire.
Hot paths: [HotPath]/[ColdPath], plus framework entry points (controllers,
hosted services, SignalR hubs, Minimal API handlers, Azure Functions,
MassTransit consumers), loops and iterators.
Honesty section: two of my original rules (empty params, Enum.HasFlag)
measured 0 B on .NET 9 because the JIT now removes the allocation. I
downgraded both to info and published the numbers. The benchmark table is at
https://github.com/rungerheimdall/rungerheimdall/blob/main/docs/benchmarks.md
Quick try:
dotnet add package RungerHeimdall.Analyzers --version 1.1.1
heimdall init
dotnet build
Rule reference (filterable): https://rungerheimdall.github.io/rungerheimdall/rules.html
Repo: https://github.com/rungerheimdall/rungerheimdall
Package: https://www.nuget.org/packages/RungerHeimdall.Analyzers
MIT, no telemetry. I am the author. Feedback on false positives would be the
most useful thing right now — that is what kills analyzers.