Pre .Net Core enabling code analysis for a C# project only required clicking a checkbox on the project settings. Now with .Net Core and the Roslyn compiler there are more steps to enable code analysis, but there are also more options for analyzers.
The analyzer I like is StyleCop. In this post I will show you how to setup StyleCop and configure it. It is well documented in the GitHub repo, but there were a few things that I noticed I needed to do that weren't mentioned there.
To Start with, assuming you have .Net Core project that you want to add code analysis to, you need to add the NuGet package for StyleCop.Analyzers version 1.0.2 as of this writing.
This line will be added to the csproj file
<PackageReference Include="StyleCop.Analyzers" Version="1.0.2" />
If you are developing a library, then unless you want all consumers of your library to also require that package you need to make the following change
<PackageReference Include="StyleCop.Analyzers" Version="1.0.2" PrivateAssets="All" />
As per the csproj reference this says that the StyleCop.Analyzers package should be used by the current project, but not be required as a dependency outside of the current project.
Now when you compile you will get warnings that start with "SA" such as
SA1101: Prefix local calls with this
To configure StyleCop Analyzers, you need to add stylecop.json to each project. As per the documentation the easy way to do this is to invoke a code fix on SA1633
This will generate the default file. If you have more than one project in the solution, I suggest you move the generated file to the same folder as the .sln file, and then share the one file among all the projects so that they all have the same StyleCop configuration.
You will need to add the file to each project, add it as a link to avoid making copies of the file, which was the point of having a single file.
For Visual Studio 2015 and 2017 you need to enable Stylecop.json. For Visual Studio 201, in each project right click on stylecop.json, select properties and in the properties page for the file set the build action to C# analyzer additional file.
Make sure to add stylecop.json to source control. Stylecop.json allows you to control the stylecop analyzer rule specifics. I always add the following at a bare minimum to the generated file.
"indentation": {
"indentationSize": 4,
"tabSize": 4,
"useTabs": true
},
"orderingRules": {
"systemUsingDirectivesFirst": true,
"blankLinesBetweenUsingGroups": "allow",
"usingDirectivesPlacement": "outsideNamespace"
}
For more information on stylecop.json see the StyleCop.json documentation
Now that code analysis is configured you should be seeing code analysis issues in the Error List in Visual Studio. Each code analysis issue is a link that will take you to a page that describes the issue and what causes it. We have a number of options for how to address the issues.
- We fix the issue - change the code to address the problem
- We suppress it in source using #pragma warnings. This can be done by right clicking on the item in the Error List and selecting Supress | In Source
- We suppress it in a Suppression file. Visual Studio will create a file in the project named GlobalSuppression.cs if it does not already exist and use System.Diagnostics.CodeAnalysis.SuppressMessage() to suppress that instance of the message
- Use a ruleset file to turn off the rule. This one takes a bit more explanation. Now that
Using a ruleset file to manage code analysis rules
To enable the use of a ruleset file you need to add a CodeAnalysisRuleSet tag to the csproj. That tag should contain a relative path to a .ruleset file, which is just an xml file. I like to put the .ruleset file in the same directory as stylecop.json and the .sln file.
<CodeAnalysisRuleSet>..\..\CodeAnalysisRules.ruleset</CodeAnalysisRuleSet>
It is easiest, in my opinion if you edit the ruleset file as text. You can use the file to hide rules that you don't want to apply to your code. Here is a sample ruleset file.
<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="My Code Analysis RuleSet" Description="This rule set contains all rules that I want to have enforced for C# programs." ToolsVersion="14.0">
<IncludeAll Action="Warning" />
<Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.Analyzers">
<Rule Id="SA1101" Action="None" />
<Rule Id="SA1623" Action="None" />
<Rule Id="SA1633" Action="None" />
</Rules>
</RuleSet>
In this ruleset I have disabled rules SA1101, SA1623 and SA1633. I set the rules to None, there are other options as listed here. As is pointed out on that page there is a subtle difference between marking a rule as Hidden vs None. "When a diagnostic rule is set to Hidden instances of that rule are still created, but aren't displayed to the user by default. However, the host process can access these diagnostics and take some host-specific action. For example, Visual Studio uses a combination of Hidden diagnostics and a custom editor extension to "grey out" dead or unnecessary code in the editor. A diagnostic rule set to None, however, is effectively turned off. Instances of this diagnostic may not be produced at all; even if they are suppressed rather than being made available to the host."
Now you know how to enable Code Analysis for .Net Core project to improve the quality of your code.