I'm trying to force a specific C# code style for if-else statements in my team that has employees using different IDEs (e.g. Visual Studio, Rider etc.). This style may seem a bit weird but has already spread out through the codebase and generates a lot of noise in PR's from team members who have autoformatting enabled in their IDEs.
Basically what I'm looking for is the following:
if (condition)
{
SomeLongMultilineMethodCall(
param1,
param2,
...);
}
else
SomeShortMethodCall();
Here multiline if
statement (even if it is a single expression) is wrapped with braces but no braces used for remaining else statement as it is single line.
I've looked through all popular roslyn analyzer libraries including StyleCop.Analyzers, Roslynator with its RCS1002 rule and even ReSharper, but none of them seem to be flexible enough. Is anyone aware of the way to automate this code style, or am I missing something?
Roslyn analyzers are the best way to stay independent from IDE and to integrate this check into your CI/CD pipeline. I don't think you can your requirements fully covered, so the closest solution you can get is using Roslynator. Try to set severity=warning to the following rules: