After a period of initial bad taste, I’ve come to like StyleCop.  I dislike a few of their rules, and I turn those off.  But overall I like how it helps many developers maintain a consistent coding style within a project.  I wrote a few rules of my own, and offer them here for free.

Source code here: NerdBank StyleCop Rules

And here are the rules that are currently in the library, with more likely to be added soon.  Remember you can pick and choose which rules you want to use within the Settings.StyleCop file.


This rule will make sure that lines of code don’t end with extra spaces or tabs that don’t do anything.  What does it hurt?  Well, it doesn’t much.  But just like almost any rule in StyleCop, it helps maintain consistent formatting.  I like enabling visible whitespace in Visual Studio so that I can make sure I’m consistent with tabs vs. spaces within a file.  And seeing whitespace characters at the end of a line is tacky.  But the bigger reason is that auto-formatting in C# will remove that trailing whitespace, which means if you’re using revision control you’ll often get diffs on lines that have nothing but removed whitespace changes, making your diff bigger than it needs to be.  This rule helps you avoid the whitespace to begin with.


This is the inverse of the SA1027 rule: TabsMustNotBeUsed.  It ensure that you do use tabs instead of spaces for indentation.  Beautiful.  I like tabs instead of spaces so that whoever is viewing the code can choose their preferred indentation size rather than having to use whatever the author thought was readable.


Sometimes when one person is using tabs with size 4 and another has size 3 tabs, extra spaces can make formatted code look wrong.  This rule just makes sure you don’t have a tab, a space, and another tab or something strange like that.

8 thoughts on “NoTrailingWhitespace StyleCop rule, and others”
  1. Are you talking about the Download button in github? It should work, but it seems to use some javascript so make sure you’re using a popular browser with javascript enabled… would be my only suggestion.

  2. The greatest custom rule for FxCop! Thank very much for sharing!! A must have for each developer 😉

    I was really wondering why Microsoft implemented SA1027 … Let the developer choose his preferred tab size :o)

  3. Thank very much for sharing!! A must have for each developer 😉

    I was really wondering why Microsoft implemented SA1027 … Let the developer choose his preferred tab size :o)

  4. Any change to get the sources updated so that it will compile with Microsoft.StyleCop.CSharp assemblies?

    I compiled it long time ago using but need to use StyleCop due relation to StyleCop for ReSharper Plug-In.

  5. Andrew,

    sorry for the delay – there is no option to subscribe to the comments 😉 (D'oh! – I just discovered the Email follow-up comments flag here ;-))

    I just tried to compile again and the same compiler errors occurred …

    Then I discovered that I am a fool:

    I used a wrong StyleCop assembly for compiling 😉

    But for the StyleCop I used:
    (and it compiled of course)

    So, everything works fine for StyleCop now!

    FYI two warnings:
    Warning 1 CA2210 : Microsoft.Design : Sign 'NerdBank.StyleCop.Rules.dll' with a strong name key. NerdBank.StyleCop.Rules

    Warning 2 CA1502 : Microsoft.Maintainability : 'NerdBankRules.AnalyzeDocument(CodeDocument)' has a cyclomatic complexity of 27. Rewrite or refactor the method to reduce complexity to 25. C:…DevelopmentStyleTestsStyleCopTestsCustomRulesAArnott-nerdbank.stylecop.rulessrcNerdBank.StyleCop.RulesNerdBankRules.cs 47 NerdBank.StyleCop.Rules

Comments are closed.