Roslyn Analyzer Rulesets and stylecop.json support in Rider 2018.2
Code analysis is an important technique to keep our code clean, readable and free of defects. Besides its own code analysis features, Rider 2018.1 started to add support for Roslyn Analyzers, such as
xunit.analyzers, which seamlessly blend into the familiar way of highlighting code inspections, configuring their severities or applying related quick-fixes.
In Rider 2018.2, issues with .NET Core toolsets have meanwhile been fixed. But more importantly, Rider 2018.2 further improves the integration with Roslyn Analyzers by adding support for ruleset files and StyleCop.json configuration files used with
Before we get started, we need to make sure to enable the option Read settings from editorconfig, project settings and rule sets, which is located in Preferences | Editor | Inspections Settings:
Once enabled, we can start working with rule set files.
Working with rule set files
Rule set files are referenced from project files (csproj) via the
CodeAnalysisRuleSet property and are usually shared between all projects in a solution. A typical rule set file is organized by analyzerId and defines an action (
inherit) for a certain ruleId:
<?xml version="1.0" encoding="utf-8"?7gt; <RuleSet Name="Name" Description="Description" ToolsVersion="15.0"> <Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.Analyzers"> <Rule Id="SA0000" Action="Error" /> </Rules> </RuleSet>
StyleCop.Analyzersas an example again, we can set the action for rule SA1200 – Using directive must appear within a namespace declaration – to Error:
Note that the error is visible not only in the editor, but also in the solution explorer and solution-wide analysis indicator.
Working with stylecop.json
Of course, not everyone likes their using directives inside the namespace declarations. This is why
StyleCop.Analyzers allows us to change such behavior with a stylecop.json configuration file. Adding a setting for
usingDirectivesPlacement with a value of
outsideNamespace for instance, will make the old error disappear, but we will instead see errors when putting using directives inside namespace:
Pro tip: having the schema file referenced will give us code completion.
Note that the stylecop.json configuration file must be included into a special item-group with the name
AdditionalFiles. For completeness, here is the full content of the project file from the previous examples:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netstandard2.0</TargetFramework> <CodeAnalysisRuleSet>Custom.ruleset</CodeAnalysisRuleSet> </PropertyGroup> <ItemGroup> <PackageReference Include="StyleCop.Analyzers" Version="1.0.2" /> <AdditionalFiles Include="stylecop.json" /> </ItemGroup> </Project>
Download Rider 2018.2 now! We’d love to hear your feedback!