Create or modify Roslyn IDE analyzers, code fixes, and code refactorings. Use when: adding a new IDE diagnostic (IDE0xxx), implementing a CodeFixProvider, implementing a CodeRefactoringProvider, writing analyzer/fixer tests, or working with AbstractBuiltInCodeStyleDiagnosticAnalyzer. Also use for: diagnostic analyzer, code action, FixAllProvider, TestInRegularAndScriptAsync, TestMissingInRegularAndScriptAsync.
CodeFixProviderCodeRefactoringProviderAbstractBuiltInCodeStyleDiagnosticAnalyzerAll IDE diagnostics use IDE0xxx format, defined as constants in src/Analyzers/Core/Analyzers/IDEDiagnosticIds.cs. Always reference these constants rather than hardcoding string IDs.
Inherit from — not raw :
AbstractBuiltInCodeStyleDiagnosticAnalyzerDiagnosticAnalyzer[DiagnosticAnalyzer(LanguageNames.CSharp)]
internal sealed class CSharpUseXxxDiagnosticAnalyzer : AbstractBuiltInCodeStyleDiagnosticAnalyzer
{
public CSharpUseXxxDiagnosticAnalyzer()
: base(IDEDiagnosticIds.UseXxxDiagnosticId,
EnforceOnBuildValues.UseXxx,
option: CSharpCodeStyleOptions.PreferXxx,
title: new LocalizableResourceString(
nameof(AnalyzersResources.Use_xxx),
AnalyzersResources.ResourceManager,
typeof(AnalyzersResources))) { }
protected override void InitializeWorker(AnalysisContext context)
=> context.RegisterCompilationStartAction(context =>
context.RegisterOperationAction(AnalyzeOperation, OperationKind.Invocation));
}
Use raw DiagnosticAnalyzer with a DiagnosticDescriptor:
[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)]
public sealed class MyAnalyzer : DiagnosticAnalyzer
{
private static readonly DiagnosticDescriptor s_rule = new(
"IDE0xxx", "Title", "Message format", "Category",
DiagnosticSeverity.Warning, isEnabledByDefault: true);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
=> ImmutableArray.Create(s_rule);
public override void Initialize(AnalysisContext context)
{
context.RegisterSyntaxNodeAction(AnalyzeNode, SyntaxKind.ClassDeclaration);
}
}
[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.UseXxx), Shared]
[method: ImportingConstructor]
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
internal sealed class CSharpUseXxxCodeFixProvider() : CodeFixProvider
{
public override ImmutableArray<string> FixableDiagnosticIds
=> [IDEDiagnosticIds.UseXxxDiagnosticId];
// Always provide FixAllProvider — typically BatchFixer
public override FixAllProvider? GetFixAllProvider()
=> WellKnownFixAllProviders.BatchFixer;
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var diagnostic = context.Diagnostics.First();
context.RegisterCodeFix(
CodeAction.Create(
title,
c => FixAsync(context.Document, diagnostic, c),
equivalenceKey: nameof(AnalyzersResources.Use_xxx)),
diagnostic);
}
}
[ExportCodeRefactoringProvider(LanguageNames.CSharp, Name = PredefinedCodeRefactoringProviderNames.MyRefactoring), Shared]
[method: ImportingConstructor]
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
internal sealed class MyRefactoringProvider() : CodeRefactoringProvider
{
public override async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
{
// Register refactoring actions via context.RegisterRefactoring(...)
}
}
IDE analyzers read user preferences from .editorconfig via the options system:
CSharpCodeStyleOptions, CodeStyleOptions2, etc.context.GetCSharpAnalyzerOptions().PreferXxx.resx files (e.g., AnalyzersResources.resx, FeaturesResources.resx)AnalyzersResources.Use_xxxnew LocalizableResourceString(nameof(AnalyzersResources.Use_xxx), AnalyzersResources.ResourceManager, typeof(AnalyzersResources)).resx files, run dotnet msbuild <path to csproj> /t:UpdateXlf to update .xlf localization filesFor analyzer + fixer pairs, inherit from AbstractCSharpDiagnosticProviderBasedUserDiagnosticTest_NoEditor:
public sealed class UseXxxTests : AbstractCSharpDiagnosticProviderBasedUserDiagnosticTest_NoEditor
{
internal override (DiagnosticAnalyzer, CodeFixProvider) CreateDiagnosticProviderAndFixer(Workspace workspace)
=> (new CSharpUseXxxDiagnosticAnalyzer(), new CSharpUseXxxCodeFixProvider());
[Fact]
public async Task TestBasicCase()
{
await TestInRegularAndScriptAsync(
"""
class C
{
void M()
{
[|var x = 1;|]
}
}
""",
"""
class C
{
void M()
{
int x = 1;
}
}
""");
}
[Fact]
public async Task TestNoFixWhenAlreadyCorrect()
{
await TestMissingInRegularAndScriptAsync(
"""
class C
{
void M()
{
int x = 1;
}
}
""");
}
}
[|...|] — diagnostic span (the code the diagnostic highlights){|DiagnosticId:...|} — named span with specific diagnostic ID$$ — cursor position marker (single instance only)await new VerifyCS.Test
{
TestCode = source,
FixedCode = fixedSource,
ExpectedDiagnostics = { VerifyCS.Diagnostic("IDE0xxx").WithSpan(5, 9, 5, 20) },
}.RunAsync();
TestInRegularAndScriptAsync to cover both regular and script contextsTestMissingInRegularAndScriptAsync to verify no fix is offered"""...""") over verbatim strings (@"...") for test source code[WorkItem("https://github.com/dotnet/roslyn/issues/NNN")] for tests fixing specific issuesIDEDiagnosticIds.cs.resx filedotnet msbuild <path to csproj> /t:UpdateXlf for localizationAbstractBuiltInCodeStyleDiagnosticAnalyzer for code style)FixAllProviderAbstractCSharpDiagnosticProviderBasedUserDiagnosticTest_NoEditor