StyleCop code style settings and inspections

In our previous post, we looked at how we can specify ReSharper’s inspection severities inside our EditorConfig file. In this post, we will see how ReSharper and Rider integrate with StyleCop configurations.

In this series:

Integration of StyleCop configurations

In the same way as ReSharper handles Roslyn coding conventions from .editorconfig, it will also handle StyleCop rules from Settings.StyleCop files. First, let’s make sure that we have enabled Read settings from editorconfig and project settings under Options | Code Inspection | Settings (in Rider, this is under Editor | Inspection Settings). We also need to enable StyleCop support under Options | Code Editing | General Formatter Style (in Rider, this is under Editor | Code Style). After that, all relevant StyleCop configuration files (i.e. files located from the directory of the source file and all parent directories) will be evaluated and the ReSharper code style settings are being adapted accordingly:

Enable reading code formatting and inspection settings from StyleCop configurations

A Settings.StyleCop file with just the content <StyleCopSettings Version="105" /> is considered empty, and will automatically enable all the existing StyleCop rules SAXXXX. We can disable rules (or explicitly enable them when using a hierarchical approach) either by manually editing configuration files, or by using the StyleCop SettingsEditor:
Using the StyleCop SettingsEditor

ReSharper 2018.1 EAP adds support for most of the naming, maintainability, readability and layout rules. For the next releases, we plan to expand the amount of supported rules. At the same time, in many cases ReSharper and Rider already allow to specify much more fine-grained settings. For instance, the StyleCop rule SA1002 (semicolon must be spaced correctly) maps to the following ReSharper formatter settings:

Overriding formatter settings via StyleCop configuration

At the same time, it activates the inspection for C# | Formatting | Incorrect spacing | Around semicolon.

One last thing to note, is that when using EditorConfig files and StyleCop configuration files simultaneously, the EditorConfig files will always take precedence over StyleCop configuration files (and ReSharper DotSettings).

We hope that you’ll enjoy our recent ReSharper and Rider improvements, like formatting inspections, extended EditorConfig support and integration of Roslyn conventions and StyleCop configurations.

Download ReSharper 2018.1 EAP now! Or give Rider 2018.1 EAP a try. We’d love to hear your feedback!

This entry was posted in How-To's and tagged , , , , , . Bookmark the permalink.

7 Responses to StyleCop code style settings and inspections

  1. Pingback: ReSharper Ultimate 2018.1 リリース! | JetBrains ブログ

  2. Dan says:

    New to stylecop and trying to understand how to edit rule settings. Do I need the resharper stylecop extension installed? I have done so, and want to be able to customise every rule. For example, our entire codebase uses tabs, so I don’t want to be notified about the spaces rule. I have tried testing the above with SA1002 and don’t understand how it works. If I tick the bottom option “before semicolon” (not that I would want that) and then add a space before a semicolon, it still informs me that the spacing is wrong.

    • Matthias Koch says:

      Hi Dan,

      sorry for the late reply, must have missed the notification.
      The StyleCop.MSBuild package comes with the StyleCopSettingsEditor.exe which can help to explore and customize your StyleCop.Settings file. I guess that’s what you’re looking for?

      Regards, Matt

  3. Chris says:

    @dan
    just don’t edit the default stylecop rules. or there will be dragons :)
    Stylecop rules try to define a dotnet identity codestyle and are also there to avoid hour long discussions about what style is the best. The more its modified, the more discussions there will be.
    I just wish it would be default in resharper. That would save so much time (in discussions) and also train many developers a much more maintainable and readable codestyle.

  4. Dan says:

    Hi Chris,
    That would be ideal – however we already have established “decided” code styles, and that includes the tabs rule… I’ll try and start that conversation again and see what comes of it 😉

  5. Luca says:

    Will ReSharper work with the Roslyn-based StyleCop.Analyzers NuGet package which consume .ruleset files?

Leave a Reply

Your email address will not be published. Required fields are marked *