Fix StyleCop violations - Code Cleanup Option?
I am evaluating whether to use Rider / vs ReSharper and Visual Studio. I am currently using both and switching when I can't find functionality or features.
Once I'm struggling to find is "Fix StyleCop violations" in code cleanup in Rider. This option is available in ReSharper but not in Rider. Am I missing something to turn this on or is there an extension that turns it on?
I have a separate post in ReSharper forum about configuring these fixes in Visual Studio - so I am struggling to find the best of both worlds.
Please sign in to leave a comment.
Hello,
By default, JetBrains Rider does not read settings from settings.stylecop files. So first you should enable Preferences | Editor | Code Style | Read code style from Settings.StyleCop files and Editor | Inspection Setting | Read settings from editorconfig, project settings and rule sets. Once it is enabled, settings from settings.stylecop files should override Rider code formatting and code syntax styles, and enable corresponding code inspections. If you notice some issues with code formatting and syntax styles then, please report an issue with examples on our bug tracker.
Hi Olga, in your earlier response, you mentioned "Once it is enabled, settings from settings.stylecop files should override Rider code formatting and code syntax styles, and enable corresponding code inspections".
This response seems to address coding formatting and standards for future development -- to prevent future occurrences of these violations.
However, my question is whether there is an automated way to fix these issues in an existing codebase. Not just flag the issue in the editor with an inspection, but actually fix all of them -- en masse. Is this possible in Rider? Version info below. Thank you!
-----
JetBrains Rider 2020.1.3
Build #RD-201.7846.1, built on May 15, 2020
Runtime version: 11.0.7+10-b765.53 x86_64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
.NET Core 3.1.3
macOS 10.15.4
The settings should be applied with Code Cleanup task, all specified code styles and formatter preferences should be applied to the existing code base.
The StyleCop implementation in Rider is currently really ambiguous, could you elaborate on the relationship between - "StyleCop.Analyzers" installed as a NuGet package and "Enabled StyleCop support".
When I "Enable StyleCop support", I can no longer change inspection settings by right-clicking or in the Setting menu.
I can't see how to edit or tune these settings.
With "Enable Roslyn Analyzers" ticked, "StyleCop.Analyzers" NuGet installed and "Enable StyleCop support" unticked. I seem to be able to control inspection settings for both style cop Roslyn analyzers and riders internal ones. But I cannot see where either of these settings is saved so I can distribute them to the team or between projects.
There is also enable "EditorConfig support" and "Read settings from editorconfig, project settings and rules sets" which add a further level of confusion. How is this supported in Rider when it comes to saving code style preferences?
Finally, how do all of these operate with code cleanup?
Gregwilliambryant
As far as I understand, you are using StyleCop.Analyzers package with rulesets, and you'd like Rider to honor it in terms of code style and inspections. Also, you expect Code Сleanup to arrange code according to your analyzers. Please correct me if I'm wrong.
I must say that Rider has a number of known issues with StyleCop.Analyzers package. First of all, StyleCop analyzers are not integrated in Code Cleanup in any way. Here is the feature request: https://youtrack.jetbrains.com/issue/RSRP-469244.
Moreover, Rider cannot edit rulesets from StyleCop.Analyzers. You need to tune it up manually if you'd like to change severity for an inspection. The problem is that context actions doesn't affect the corresponding ruleset file, it affects only R# settings. Here is the issue for that case: https://youtrack.jetbrains.com/issue/RIDER-31902
To sum up, you can only see inspections from StyleCop.Analyzers, and edit its severity manually. Code cleanup for your case is not supported yet. Rider is now able to read and apply code style settings only from old settings.stylecop files, see this docs page. We should definitely mention this restriction in Rider docs. I've created a task for that: https://youtrack.jetbrains.com/issue/RIDER-55131.
I'd also like to explain the Rider settings you've mentioned:
"Read settings from editorconfig, project settings and rules sets" is exclusively about inspection settings page. Speaking about Stylecop, this checkbox enables reading code inspection severity settings from Stylecop Roslyn Analysers Rule Sets.
"Enable StyleCop support" is about reading settings from old settings.stylecop file and has nothing to do with "StyleCop.Analyzers" package.
"Enable Roslyn Analyzers" is the setting that you need to work with "StyleCop.Analyzers" package.
"EditorConfig support" is about reading code style settings from .editorconfig file. If there is no .editorconfig file in your solution, then this checkbox will take no effect.
Hope this helps! Please let me know if you have any questions.
We'd be really grateful if someone could share a reasonably big solution, which uses StyleCop rules for code cleanup and inspections. It would help us a lot to test how Rider now handles such cases and what can be improved in terms of UI and functionality. Even better if you work with it in another IDE and cannot use Rider because it cannot understand your code style and inspections.