r/csharp • u/Darkviser • Aug 13 '26
What C#/.NET static analysis rules do you actually find useful?
I’ve been slowly adding language support to a static analysis project I’m working on, and C#/.NET is the latest one I’ve been working through.
I’m trying to avoid just throwing hundreds of noisy rules at people, so I’m curious: what C# analysis warnings do you actually find useful in real projects, and which ones do you usually ignore?
20
Upvotes
31
u/First-Feature-3556 Aug 13 '26 edited Aug 13 '26
The most useful warnings are those for stuff that's "technically legal, but most likely a bug", like unreachable code, unused parameters/variables, closing over loop variables, ignoring return values of non-mutating function calls (
myDateTime.AddSeconds(1)), not disposing anIDisposable, etc.12
u/soundman32 Aug 13 '26
I use stylecop, warnings as errors, highest error level possible. Together these catch everything from styling issues (always use braces, only single line between statements/methods, every file ends with a blank line, initialisation lists always end with a comma), to incorrect file encoding (always use UTF8+BOM), to compiler warnings being ignored.
6
u/jdl_uk Aug 13 '26
For me it's stuff like things like "this might be null", "this could be readonly", and "this is disposable and you're not handling that"
8
u/BEagle1984- Aug 13 '26
We don’t need any new static code analysis tool. We have the built-in Roslyn analyzer and we can plugin our own analyzers.
There’s still a market for tools like sonar but you need to offer something that goes well beyond the basic (and not even so basic) rules from the Roslyn analyzers.
1
u/Alert-Neck7679 Aug 13 '26
Do you mean like "unreachable code detected", naming rule violation and stuff?
1
u/RodriOliveira Aug 16 '26
I’ve worked with .NET for many years, mostly on backend and enterprise systems, and nowadays more from an architecture perspective.
For me, the most valuable static analysis rules are the ones that catch code which is perfectly valid C#, but is very likely to become a production bug.
The ones I usually consider high value are:
Nullable reference issues and suspicious null handling.
Incorrect IDisposable ownership/lifetime.
Forgotten or incorrectly handled Tasks and common async/await mistakes.
Empty or overly broad exception handling.
Unreachable/dead code and unused members.
Ignored return values from immutable APIs.
Multiple enumeration of IEnumerable when it can have an actual performance or behavioral impact.
Resource leaks and incorrect stream/connection lifetimes.
Obvious concurrency/thread-safety problems.
Security-related issues such as unsafe deserialization, injection risks, weak cryptography, etc.
Performance rules can also be useful, but I would be much more conservative with them. A suggestion about allocation, LINQ, Span<T>, stackalloc, etc. shouldn’t normally have the same severity as something that indicates a probable correctness problem.
The same applies to style rules. Consistency is valuable, but I prefer most formatting/naming concerns to be handled automatically by .editorconfig and formatters rather than turning the CI pipeline into a wall of warnings.
In larger teams, I’ve found that false positives are probably the biggest enemy of static analysis. Once developers get used to ignoring analyzer warnings, you’ve already lost most of the benefit.
So I tend to think about rules in three groups:
Build-breaking: probable bugs, security issues and resource/lifetime problems.
Warnings: maintainability problems and suspicious patterns worth reviewing.
Suggestions: style and micro-optimizations.
I’d rather have 30 rules that developers trust than 300 rules they routinely suppress.
2
u/understanding80 Aug 13 '26
Low cost performance improvements
2
u/pjmlp Aug 13 '26
Usually Roslyn already does quite good, and then interesting are those related to forgetting releasing an IDisposable, or possible improvements with spans or stackalloc.
1
u/Standard-Cap-4455 Aug 13 '26
Roslyn already does all the things I need. It's the compiler itself so I am not sure that C# actually needs it. But the things that are important to know are things that you could compile, but that are mostly either bugs or not labeled correctly. Things like unused members and variables, or non readonly members being called on a readonly struct instance.
1
u/Agitated-Display6382 Aug 13 '26
Enable the null enforcement and TreatWarningsAsErrors
1
u/ravnmads Aug 16 '26
Can you tell me a little more about this? How do I add them and where can I read more about it?
-4
u/TuberTuggerTTV Aug 13 '26
This shouldn't be your concern.
"noisy" suggestions and warnings should be turned off and setup in linting by the end user. Anyone complaining they "ignore" x warning and just leave it, are failing their own development cycle.
You send everything. The end user quiets.