Can StyleCop automatically fix anything?

后端 未结 9 2164
盖世英雄少女心
盖世英雄少女心 2020-12-30 22:20

I\'ve decided to start running StyleCop on a medium sized project and am getting over 1000 warnings.

Is there a quick easy way to fix most of these warnings? Most of

相关标签:
9条回答
  • 2020-12-30 22:41

    StyleCopFixer automatically fixes StyleCop exceptions within Visual Studio IDE for C# Only.

    Errors currently catered for are:

    1. SA1001
    2. SA1516
    3. SA1513
    4. SA1508
    5. SA1507
    6. SA1505
    7. SA1309
    8. SA1210
    9. SA1202
    10. SA1200
    11. SA1106
    12. SA1101
    13. SA1025
    14. SA1013
    15. SA1012
    16. SA1009
    17. SA1008
    18. SA1633
    19. SA1003
    20. SA1005
    21. SA1600 (for properties only)
    22. SA1121
    0 讨论(0)
  • 2020-12-30 22:46

    This doesn't exactly "automatically fix anything" but there is a StyleCop plug-in for Resharper on Codeplex that will automatically highlight the style errors in your code.

    Update: According to the StyleCop for ReSharper website, ReSharper now provides an increasing number of Quick-Fixes for StyleCop errors. You select a quick-fix from the list and the problem will be resolved automatically.

    0 讨论(0)
  • 2020-12-30 22:47

    Nowadays you can use the StyleCop.Analyzers NuGet package in your projects.

    Updating the package can result in a lot of new warnings.

    Ctrl + . shows available quick fixes. A fix can be applied to the whole document, project or solution at once.

    A Preview Changes window lets you deselect the lines to resolve manually.

    CodeMaid is a good tool to fix formatting errors in batch or automatically.

    0 讨论(0)
  • 2020-12-30 22:50
    1. Turn off the warnings! Not recommended, but you could use this in the short term until you get a bulk of the issues resolved.
    2. Write a program to fix some of the simpler warnings, or use find/replace
    3. Use code snippets to help with things like file header documentation

    There was mention on the StyleCop blog regarding implementing auto fixing for rules

    Also check out the entry on the blog regarding integrating in to an existing large project.

    Note: It seems that StyleCop only returns around 1000 warnings, so you will continue to get this number even after fixing a heap.

    0 讨论(0)
  • 2020-12-30 22:51

    If you have Resharper you can download a StyleCop plugin for Resharper which features some automatic fixes. StyleCop for Resharper on codeplex.

    0 讨论(0)
  • 2020-12-30 22:52

    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.

    Of course, if there were an automated tool to achieve consistent formatting, I'd hook it up post-check-in, and let it help me. But Style Cop is made by sadists. 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, it scolds them 'invalid spacing around the comma'
    Charlie: —and then explains how to fix it?
    Bob: No, that's in a separate document
    Charlie: —on Google?
    Bob: No.

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

    0 讨论(0)
提交回复
热议问题