// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System.Collections.Immutable; using System.Composition; using System.Threading; using System.Threading.Tasks; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CodeFixes; using Microsoft.CodeAnalysis.Editing; using Microsoft.CodeAnalysis.NetAnalyzers; namespace Microsoft.CodeQuality.Analyzers.Maintainability { /// <summary> /// CA1823: Avoid unused private fields /// </summary> [ExportCodeFixProvider(LanguageNames.CSharp, LanguageNames.VisualBasic, Name = AvoidUnusedPrivateFieldsAnalyzer.RuleId), Shared] public sealed class AvoidUnusedPrivateFieldsFixer : SyntaxEditorBasedCodeFixProvider { public sealed override ImmutableArray<string> FixableDiagnosticIds { get; } = ImmutableArray.Create(AvoidUnusedPrivateFieldsAnalyzer.RuleId); public sealed override Task RegisterCodeFixesAsync(CodeFixContext context) { string title = MicrosoftCodeQualityAnalyzersResources.AvoidUnusedPrivateFieldsTitle; RegisterCodeFix(context, title, title); return Task.CompletedTask; } protected sealed override Task ApplyFixAsync(Document document, Diagnostic diagnostic, SyntaxEditor editor, CancellationToken cancellationToken) { SyntaxNode node = editor.OriginalRoot.FindNode(diagnostic.Location.SourceSpan); if (editor.Generator.GetDeclaration(node) is SyntaxNode declaration) { editor.RemoveNode(declaration); } return Task.CompletedTask; } } }