StyleFix - Automatically fix StyleCop violations

Microsoft StyleCop reports style violation in C# code. It helps standardizing codes throughout the enterprise and also makes reading, sharing and maintaining code much easier.

The only problem is that it reports more than 100 types of violations and so the number of violations that you see in a typical project may run into the thousands. Some of them may be easy to fix in the visual studio using macros and regular expressions but most of them take a lot of time and are hence not practical at all.

Microsoft blog suggests that the next release of the StyleCop will be automatically able to fix the violations. This is a welcome improvement! In the meantime let me give you a stop-gap approach that we are using here.

The following tools are useful:

  1. Ghostdoc - A automated documenting tool implemented as macros. Although I believe that automated (and syntactical) documenting defeats the purpose of documentation, this tool is a GEM!. You need to use it to see what I mean.
  2. NArrange - An open source project that does some things like rearranging the code to fix some stylecop violations of the code. However, it does not touch the majority of the violations.
  3. StyleFix - This is what we have written here to use it on our own code. It does a few things:
    1. Allows you to selectively choose files that will be processed by StyleCop.
    2. Automatically fixes many violations. This program is new and will be improved till anything better comes up on the horizon.


I am using StyleFix with some success. For example, for a small project with 8 files, the first time I ran with StyleCop, I got 1753 violations!!! After running StyleFix it reduced to 273 or so. Applying GhostDoc reduced it to 92. The rest of it, I had to do manually :-(

The executable is at:

The source code is at:

If you are using it I would like to know what you think. If you want to contribute, let me know I will gladly share the svn access. *** This program is still in beta, so please make appropriate backups before using it. I am not responsible if it screws up your code ***

Comments (9) -

  • Hi,

    You could also use StyleCop for ReSharper (you'll need ReSharper installed tho) - we went feature complete last week and implement Quick Fixes and Code Clean Up Modules to fix the majority of the StyleCop violations.

    See for more details.


  • Thanks, I looked at this a while back. It is a good commercial tool that does some stylecop fixes. I will probably need to look at it again.

    We wanted something simple which would just fix the stylecop errors without doing anything more.
  • If you haven't tried ReSharper I really urge you to - it's the single biggest productivity gain I've seen - we've rolled it out for all our developers and it's gone down very well. Although it's commercial it's really worth the value (I've had licenses since V2.0 and have always paid for them myself because I personally see the benefit). The cheapest option is the C# only version.

    Again I urge you to give it a go - StyleCop for ReSharper is just one of the *free* plugins that are available - there are also Agent Smith and Agent Johnson which also have some very useful features. We also make a lot of use of the Gallio plugin which gives some neat TDD features.

    The one thing I'd say about StyleFix is there are only a small number of StyleCop violations that can be fixed by string replacement and RegExp - some of the other violations are very tricky to solve and we had to use a lot of ReSharper's inbuilt features to fix them.

    The other thing that's good about R# is that you can wrap all the fixes in transactions, which means they will roll back if they fail and also allows you to Undo your changes.


  • Howard, Thanks for your comments
  • Hi,

    is there any documentation available for StyleFix?

    It seems to run ok, but does not change anything in the code.
  • Hi!

    Can you try this:

    Select the files and then click on Enable button at the bottom and then click on "Run stylefix" button.

    I think there is a bug which I need to fix.

    There is no other documentation but the source code is available in codeplex.
  • HI,

    I also tried the above mentioned steps i.e. Click on Enable button and then Click on Run StyleFix button. But the tool is not working Frown
    I am using VSTS 2008 and StyleCop 4.3.

  • Can you post a sample project (zip file) in the codeplex issue tracker so that I can take a look

  • Hi,

    I have the same problem. It seems to run but doesnot change the file. I have notice the same with the Ghostdoc.

    Any idea?

Comments are closed