| name | static-analysis |
| description | .NET static analysis and code quality tools. Use when configuring analyzers, fixing warnings, or enforcing code standards. |
| allowed-tools | Read, Grep, Glob, Bash |
.NET Static Analysis
Built-in Analyzers
.NET Analyzers
.NET SDK includes analyzers for code quality and style. Enable in project file:
<PropertyGroup>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<AnalysisLevel>latest</AnalysisLevel>
<AnalysisMode>Recommended</AnalysisMode>
</PropertyGroup>
Analysis Categories
| Category | Prefix | Focus |
|---|
| Design | CA1xxx | API design guidelines |
| Globalization | CA2xxx | Internationalization |
| Performance | CA18xx | Performance optimizations |
| Security | CA2xxx, CA3xxx | Security vulnerabilities |
| Usage | CA2xxx | Correct API usage |
| Naming | CA17xx | Naming conventions |
| Reliability | CA2xxx | Error handling, resources |
Running Analysis
With Build
dotnet build
dotnet build /p:RunAnalyzersDuringBuild=true
dotnet build /p:TreatWarningsAsErrors=true
dotnet build /p:RunAnalyzers=true /p:RunCodeAnalysis=true
As Separate Step
dotnet format --verify-no-changes
dotnet format
dotnet build src/MyApp/MyApp.csproj /p:TreatWarningsAsErrors=true
Configuring Rules
EditorConfig
root = true
[*.cs]
dotnet_naming_rule.private_fields_should_be_camel_case.severity = warning
dotnet_naming_rule.private_fields_should_be_camel_case.symbols = private_fields
dotnet_naming_rule.private_fields_should_be_camel_case.style = camel_case_style
dotnet_naming_symbols.private_fields.applicable_kinds = field
dotnet_naming_symbols.private_fields.applicable_accessibilities = private
dotnet_naming_style.camel_case_style.capitalization = camel_case
dotnet_naming_style.camel_case_style.required_prefix = _
csharp_style_var_for_built_in_types = true:suggestion
csharp_style_expression_bodied_methods = when_on_single_line:suggestion
csharp_prefer_braces = true:warning
dotnet_diagnostic.CA1062.severity = warning
dotnet_diagnostic.CA2007.severity = none
dotnet_diagnostic.IDE0044.severity = warning
GlobalAnalyzerConfig
is_global = true
dotnet_diagnostic.CA1062.severity = warning
dotnet_diagnostic.CA2000.severity = error
Project-Level
<PropertyGroup>
<NoWarn>$(NoWarn);CA1062;CA2007</NoWarn>
<WarningsAsErrors>$(WarningsAsErrors);CA2000</WarningsAsErrors>
</PropertyGroup>
Suppressing Warnings
Code-Level Suppression
[SuppressMessage("Design", "CA1062:Validate arguments",
Justification = "Validated by framework")]
public void Process(Request request) { }
#pragma warning disable CA1062
public void Process(Request request) { }
#pragma warning restore CA1062
[assembly: SuppressMessage("Design", "CA1062")]
Global Suppressions
using System.Diagnostics.CodeAnalysis;
[assembly: SuppressMessage("Design", "CA1062",
Scope = "namespaceanddescendants",
Target = "~N:MyApp.Controllers")]
Common Analyzer Rules
CA1062 - Validate Arguments
public void Process(Request request)
{
request.Execute();
}
public void Process(Request request)
{
ArgumentNullException.ThrowIfNull(request);
request.Execute();
}
CA2007 - ConfigureAwait
await SomeAsync();
await SomeAsync().ConfigureAwait(false);
dotnet_diagnostic.CA2007.severity = none
CA1822 - Mark Members Static
public int Calculate(int x) => x * 2;
public static int Calculate(int x) => x * 2;
IDE0044 - Make Field Readonly
private int _value;
private readonly int _value;
CA2000 - Dispose Objects
public void Process()
{
var stream = new FileStream("file.txt", FileMode.Open);
}
public void Process()
{
using var stream = new FileStream("file.txt", FileMode.Open);
}
dotnet format
Check Formatting
dotnet format --verify-no-changes
dotnet format --include "src/**/*.cs" --verify-no-changes
dotnet format --exclude "**/Migrations/**"
Apply Fixes
dotnet format
dotnet format style
dotnet format analyzers
dotnet format --severity warn
Targeting
dotnet format whitespace
dotnet format style
dotnet format analyzers
dotnet format --diagnostics CA1062 IDE0044
Third-Party Analyzers
StyleCop.Analyzers
dotnet add package StyleCop.Analyzers
dotnet_diagnostic.SA1101.severity = none
dotnet_diagnostic.SA1200.severity = none
dotnet_diagnostic.SA1633.severity = none
Roslynator
dotnet add package Roslynator.Analyzers
SonarAnalyzer
dotnet add package SonarAnalyzer.CSharp
CI Integration
Quality Gate Script
#!/bin/bash
dotnet build --no-restore /p:TreatWarningsAsErrors=true
dotnet format --verify-no-changes
exit $?
Azure DevOps
- task: DotNetCoreCLI@2
displayName: 'Build with Analysis'
inputs:
command: build
arguments: '/p:TreatWarningsAsErrors=true'
See analyzers.md for detailed analyzer configurations.