The analyzers of the SDK
The C# compiler is also a platform for analyzers: programs that read the syntax and the semantic model of a project and report diagnostics of their own. Some ship with the SDK, and a package can add more. This lesson turns on the full set, reads a real warning, and turns it into an error.
The program
The calculator has two instance methods that use no instance state, Product and Sum, and the top-level statements call both through an instance. The compiler builds it, and the analyzer has an opinion about it:
Console.WriteLine($"sum {Sum(2, 3)}");
Console.WriteLine($"product {Product(2, 3)}");
var calculator = new Calculator();
Console.WriteLine($"instance {calculator.Sum(4, 5)}");
static int Product(int left, int right) => left * right;
static int Sum(int left, int right) => left + right;
sealed class Calculator
{
public int Product(int left, int right) => left * right;
public int Sum(int left, int right) => left + right;
}
AnalysisMode All
The project property AnalysisMode selects the set of rules. All enables every rule that ships with the SDK, and the build then reports the two methods:
dotnet build app.csproj -p:AnalysisMode=All
Program.cs(15,16): warning CA1822: Member 'Sum' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)
Program.cs(13,16): warning CA1822: Member 'Product' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)
Build succeeded.
Program.cs(15,16): warning CA1822: Member 'Sum' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)
Program.cs(13,16): warning CA1822: Member 'Product' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)
2 Warning(s)
0 Error(s)
The two warnings are the same diagnostic at two lines, one per method, and the build still succeeds: a warning does not stop a program. The rule is CA1822, its category is performance, and its advice is to mark the method static so that the call costs no instance.
Warnings as errors
-warnaserror promotes every warning of the build, including the analyzer ones, to an error. The same project now fails:
dotnet build app.csproj -p:AnalysisMode=All -warnaserror
Program.cs(13,16): error CA1822: Member 'Product' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)
Program.cs(15,16): error CA1822: Member 'Sum' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)
Build FAILED.
Program.cs(13,16): error CA1822: Member 'Product' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)
Program.cs(15,16): error CA1822: Member 'Sum' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)
0 Warning(s)
2 Error(s)
The only change is the word warning becoming error and the build failing, and that is the whole mechanism of a strict build: a rule that the team cares about is an error, and nobody can merge code that violates it. The source did not change, so the analyzer was always right; the default build simply did not report CA1822.
Configuring the analyzers
A project that turns on every rule inherits noise, so the settings are per rule and per severity:
- AnalysisMode selects a set: Default, Recommended, Minimum or All.
- EnableNETAnalyzers is true for a project that targets a modern framework; setting it false removes the SDK analyzers.
- NoWarn lists diagnostic ids the build ignores, and WarningsAsErrors lists the ones it promotes.
- An .editorconfig sets a severity per rule, such as dotnet_diagnostic.CA1822.severity = error, which both the build and the IDE follow.
- A NuGet package can add analyzers of its own; those rules appear with their own prefixes, and the package needs the feed.
The default mode of the measured project was silent about these two methods: the same build without AnalysisMode=All succeeded with no warning. A team that wants the rule states it, and the lesson on publishing starts from a clean build because the analyzers already had their say.
Exercises
- Make the two methods static and run the All build again.
- Add NoWarn=CA1822 and confirm that the All build is quiet.
- Set the severity of CA1822 in an .editorconfig and compare with the command-line flag.
- Try AnalysisMode=Recommended and list the rules it reports.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What is a Roslyn analyzer?
Answer
A diagnostic that inspects the syntax and semantics of the code
It reports rules such as CA1822 during the build or in the editor.
-
What did CA1822 report?
Answer
A member that does not access instance data and can be static
The measured message says exactly that for Product and Sum.
-
What does -warnaserror do to an analyzer warning?
Answer
Promotes it to an error and fails the build
The measured build changed the word to error and printed Build FAILED.
-
Why did the default build print no CA1822 warning?
Answer
AnalysisMode All enables rules the default set leaves out
The same source warned only when the full set was enabled.
-
What does this program print?
Console.WriteLine(int.MaxValue > 0);Answer
True
The maximum int is positive.
-
Where do the analyzers of this lesson come from?
Answer
The .NET SDK, with no package
The rules are part of the SDK, which is why the offline build reports them.
-
What does the NoWarn property do?
Answer
Lists diagnostic ids the build ignores
It is a list of ids, and CA1822 is one of them.
-
What does this program print?
Console.WriteLine(5 % 2 == 1);Answer
True
Five divided by two leaves a remainder of one.
-
Why would a team enable warnings as errors in CI?
Answer
A rule the team cares about then blocks the merge
The build status is what a merge checks.
-
How is the severity of one rule set for the editor and the build?
Answer
In .editorconfig, with dotnet_diagnostic.ID.severity
The same file configures the IDE and the command-line build.
The code on this page was built with the .NET SDK 10.0.303 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev