- It is a common requirement to enable/disable specific analyzer or/and to change its action (from Warning to Info etc.)
- This can be easily accomplished by using rule set.
- Rule set is a group of rules where each rule define "Action" for a specific analyzer.
- Action None deactivates analyzer.
- Other actions specifies that analyzer is active but it differs in how it is displayed in the IDE.
Action | Description |
---|---|
None | disabled |
Hidden | not visible |
Info | visible as Message |
Warning | visible as Warning |
Error | visible as Error |
Rule set is typically stored in a file with extension ruleset and it has following structure:
<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="My Rules" ToolsVersion="15.0">
<Rules AnalyzerId="Roslynator.CSharp.Analyzers" RuleNamespace="Roslynator.CSharp.Analyzers">
<Rule Id="RCS1001" Action="Warning" />
</Rules>
</RuleSet>
- Skip this step if you already have one.
- Go to Solution Explorer - Solution - Project - References - Analyzers - Open Active Rule Set.
- Modify rule set.
- Save rule set (this will create a new file ProjectName.ruleset in your project folder.
- Move rule set file to a solution root folder (or any other location).
- Open rule set file in text editor.
- Change value of attribute 'Name' (rule set is represented in the IDE by its name).
- Go to Main Menu - Analysis - Configure Code Analysis - For Solution
- Change rule set for each project.
- Change configuration and repeat previous step (optional).
- Go to Solution Explorer - Solution - Context Menu - Add - New Item - Code Analysis Rule Set
- Open rule set file in text editor.
- Change value of attribute 'Name' (rule set is represented in the IDE by its name).
- Open csproj file in text editor or go to Solution Explorer - Project - Context Menu - Edit ProjectName.csproj
- Add following
PropertyGroup
(or add element to the existingPropertyGroup
):
<PropertyGroup>
<CodeAnalysisRuleSet>relative_or_absolute_path_to_ruleset_file</CodeAnalysisRuleSet>
</PropertyGroup>
Note: If you want to disable an analyzer completely you have use a rule set.
class C
{
[SuppressMessage("Readability", "RCS1008", Justification = "<Pending>")]
void M()
{
var x = Foo(); // no RCS1008 here
}
}
[assembly: SuppressMessage("Readability", "RCS1008", Justification = "<Pending>", Scope = "member", Target = "~M:C.M")]
class C
{
void M()
{
var x = Foo(); // no RCS1008 here
}
}
class C
{
void M()
{
#pragma warning disable RCS1008
var x = Foo(); // no RCS1008 here
#pragma warning restore RCS1008
}
}
Go to Visual Studio Tools > Options > Roslynator > Global Suppressions