-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
214 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/Meziantou.Analyzer/Rules/ReplaceEnumToStringWithNameofAnalyzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Operations; | ||
|
||
namespace Meziantou.Analyzer.Rules | ||
{ | ||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public class ReplaceEnumToStringWithNameofAnalyzer : DiagnosticAnalyzer | ||
{ | ||
private static readonly DiagnosticDescriptor s_rule = new DiagnosticDescriptor( | ||
RuleIdentifiers.ReplaceEnumToStringWithNameof, | ||
title: "Replace with nameof", | ||
messageFormat: "Replace with nameof", | ||
RuleCategories.Performance, | ||
DiagnosticSeverity.Info, | ||
isEnabledByDefault: true, | ||
description: "", | ||
helpLinkUri: RuleIdentifiers.GetHelpUri(RuleIdentifiers.ReplaceEnumToStringWithNameof)); | ||
|
||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(s_rule); | ||
|
||
public override void Initialize(AnalysisContext context) | ||
{ | ||
context.EnableConcurrentExecution(); | ||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
|
||
context.RegisterOperationAction(Analyze, OperationKind.Invocation); | ||
} | ||
|
||
private static void Analyze(OperationAnalysisContext context) | ||
{ | ||
var operation = (IInvocationOperation)context.Operation; | ||
if (operation.TargetMethod.Name != nameof(object.ToString)) | ||
return; | ||
|
||
if (!operation.TargetMethod.ContainingType.IsEqualTo(context.Compilation.GetSpecialType(SpecialType.System_Enum))) | ||
return; | ||
|
||
var expression = operation.Children.First() as IMemberReferenceOperation; | ||
if (expression == null) | ||
return; | ||
|
||
if (expression.Member.ContainingType.EnumUnderlyingType == null) | ||
return; | ||
|
||
context.ReportDiagnostic(s_rule, operation); | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/Meziantou.Analyzer/Rules/ReplaceEnumToStringWithNameofFixer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System.Collections.Immutable; | ||
using System.Composition; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeActions; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.Editing; | ||
using Microsoft.CodeAnalysis.Operations; | ||
|
||
namespace Meziantou.Analyzer.Rules | ||
{ | ||
[ExportCodeFixProvider(LanguageNames.CSharp), Shared] | ||
public class ReplaceEnumToStringWithNameofFixer : CodeFixProvider | ||
{ | ||
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(RuleIdentifiers.ReplaceEnumToStringWithNameof); | ||
|
||
public override FixAllProvider GetFixAllProvider() | ||
{ | ||
return WellKnownFixAllProviders.BatchFixer; | ||
} | ||
|
||
public override async Task RegisterCodeFixesAsync(CodeFixContext context) | ||
{ | ||
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); | ||
var nodeToFix = root.FindNode(context.Span, getInnermostNodeForTie: true); | ||
if (nodeToFix == null) | ||
return; | ||
|
||
var title = "Use nameof"; | ||
context.RegisterCodeFix(CodeAction.Create(title, ct => UseNameof(context.Document, nodeToFix, ct), equivalenceKey: title), context.Diagnostics); | ||
} | ||
|
||
private static async Task<Document> UseNameof(Document document, SyntaxNode nodeToFix, CancellationToken cancellationToken) | ||
{ | ||
var editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false); | ||
var generator = editor.Generator; | ||
var operation = (IInvocationOperation)editor.SemanticModel.GetOperation(nodeToFix, cancellationToken); | ||
|
||
var newExpression = generator.NameOfExpression(operation.Children.First().Syntax); | ||
|
||
editor.ReplaceNode(nodeToFix, newExpression); | ||
return editor.GetChangedDocument(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.