-->

Is there a StyleCop/FxCop autofixing tool? [closed

2020-02-18 14:44发布

问题:


Want to improve this question? Update the question so it's on-topic for Stack Overflow.

Closed 2 years ago.

Is there a tool that can automatically fix StyleCop and FxCop rules, rather than manually fixing thousands of warning and errors?

There must be one, it is such a good business opportunity!

回答1:

No, frustratingly. That's why Style Cop is counter-productive. Consistent formatting looks nice, but there's little productivity benefit to it (what really makes code hard to read is length and indirection). Perfect spacing is not worth spending hours adjusting lines by hand.

If there were an automated tool to achieve consistent formatting, I might hook it up post-check-in. But Style Cop is made by sadists who force the user to do the tedious formatting by hand. Presumably their conversation went like this:

Bob: Using pattern matching, the software identifies ugly code
Charlie: —and fixes it!
Bob: No. It prints a message about each transgression.
Charlie: And then the user right-clicks to fix them all?
Bob: No. The message scolds them 'invalid spacing around the comma'.
Charlie: And then explains how the user can fix it?
Bob: No, that information is in a separate document.
Charlie: On Google?
Bob: No.

I hate Style Cop. It's below a minimum viable product.



回答2:

StyleFix will fix many of the violations found by StyleCop.
GhostDoc makes it easy to add comments to your methods.

Lateley I've been using Telerik JustCode. It really helps PREVENT the style issues.



回答3:

FxCop doesn't come with its own fixing features just yet. Project Roslyn (targeted to be released somewhere after VS11) will provide a new engine for static analysis rules and comes with fixing built in as a feature.

There are a number of tools that provide their own fixing rules. A few have been mentioned before:

  • JetBrains Resharper
  • DevExpress CodeRush & Refactor Pro
  • Telerik JustCode
  • Visual Assist X
  • Typemock Testlint Pro (specifically for unit tests)

These sometimes flag more or other things than FxCop or StyleCop would. And herein lies a danger of conflicting rules. Which is already occurring here and there if you're one tool such as FxCop.

And there are a few open source projects that attempt to provide fixes for the most common FxCop and StyleCop warnings. I haven't tried those for StyleCop, but most of the FxCop auto fixing tools don't really work well, as there are but few FxCop rules that are truly easy to fix.

None of these tools will be able to help you fix custom rules (or rules you've downloaded from other sources, such as MSOCAF or open source projects (FxCopContrib for example) or other Microsoft projects (Such as the rules included in the Open Source Web Service Software Factory CTP).

And, especially with FxCop rules I see that while the thing being flagged, which seems very simple to resolve, actually points to a much larger issue.

  • Take for example the performance rule requesting you to make all your methods static. This is actually pointing out to you that you have classes with low cohesion. Just marking everything static might make your code a tiny bit faster, but won't improve your design and will most certainly make testing and maintaining your code harder in the long run.
  • Or the rule that mentions you should provide an IFormatProvider to any method that has an overload which supports it. I see too many developers who just put CurrentCulture in there and are done with it. Still getting import/export errors when they transfer files between two differently configured systems. As globalization, localization and internationalization are hard subjects to tackle.

This applies a lot less to StyleCop, as the coding style is often not related to the actual underlying implementation or design. And it looks like StylecopFixer is a great add-on if you're not using the Resharper integration which now ships with StyleCop by default.



回答4:

StyleCop maybe (for some violations) - there are tons of code formatters.

FxCop no (that I am aware of).

ReSharper can help with its code cleanup and refactorings, but it's not going to magically fix all FxCop violations (although it will point out and help to fix some issues).

FxCop issues are similar to design patterns; they generally require thought to determine the proper way to apply the solution. In some cases the violation may not apply, and can be suppressed. You can also tune FxCop rules to your particular needs; the standard rules were intended for distributed frameworks and libraries, and are not universal. The majority of them are very, very good suggestions, though.



回答5:

StyleCopFixer is the tool that automatically fix StyleCop exceptions within Visual Studio IDE.



回答6:

Some tools which I use to fix the stylecop fix issues are,

  1. CodeMaid ( Free one open source)
  2. GhostDoc (Already suggested in earlier replies in the same post.You need to buy this, comes with a limited 14 days trial edition)
  3. StyleCopFixer ( Already suggested in earlier replies in the same post.Free one, comes as an extension to VS2010)

Hope it helps :)



回答7:

Running Stylecop for the first time usually most of the errors thrown in my experience are the ones connected with code-formatting.

I used two tools to reorganize the code to match the Stylecop rules:

  • NArrange
  • Regionerate

The first one became my preference; it's command line tool, I added this to custom tools in Visual Studio. The second one is a plugin for Visual Studio, therefore it was quite convenient to work with single files.

As already mentioned, there are some tools that try to fix some Stylecop errors, but in my opinion they worked quite poorly, therefore I abandoned using them and can't recommend any.

I don't think there are tools for FxCop, and I'm not convinced there should be..



回答8:

StyleCopAutoFix is a console application that will automatically fix StyleCop violations related to missing (or unnecessary) empty lines. It is free an open-source.

While the scope of the violations it fixes is fairly limited, it fix them in a safe way (it is unlikely it will break your code or change the behavior). These violations are also the most common ones.

If you use Visual Studio, I also recommend to run this script : https://gist.github.com/JayBazuzi/9e0de544cdfe0c7a4358. It automatically auto format all code of all files in a solution.

On projects I ran this application (and the VS script above) it fixes up to 60% of all violations.

Disclaimer : I am the author of this.



回答9:

There is a (relatively) new Visual Studio extension on the block (StyleCopAid) - give it a try:

https://visualstudiogallery.msdn.microsoft.com/505d2931-143f-4055-b7dd-a5d7c95bb085

Among other things - fixes ordering rules, suppresses some messages, and has an undo option.

Update from July 2018: "The tool supports Visual Studio 2010, 2012, and 2013 versions."