-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CC0122 Replace Task.Result with await Task
- Loading branch information
Showing
9 changed files
with
372 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using CodeCracker.Properties; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using System; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
|
||
namespace CodeCracker.CSharp.Design | ||
{ | ||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public class ResultInAsyncAnalyzer : DiagnosticAnalyzer | ||
{ | ||
internal static readonly LocalizableString Title = new LocalizableResourceString(nameof(Resources.ResultInAsyncAnalyzer_Title), Resources.ResourceManager, typeof(Resources)); | ||
internal static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(Resources.ResultInAsync_MessageFormat), Resources.ResourceManager, typeof(Resources)); | ||
internal static readonly LocalizableString Description = new LocalizableResourceString(nameof(Resources.ResultInAsync_Description), Resources.ResourceManager, typeof(Resources)); | ||
internal const string Category = SupportedCategories.Design; | ||
|
||
internal static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor( | ||
DiagnosticId.ResultInAsync.ToDiagnosticId(), | ||
Title, | ||
MessageFormat, | ||
Category, | ||
DiagnosticSeverity.Warning, | ||
true, | ||
description: Description, | ||
helpLinkUri: HelpLink.ForDiagnostic(DiagnosticId.ResultInAsync)); | ||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule); | ||
|
||
public override void Initialize(AnalysisContext context) => | ||
context.RegisterSyntaxNodeAction(Analyzer, SyntaxKind.InvocationExpression); | ||
|
||
private static void Analyzer(SyntaxNodeAnalysisContext context) | ||
{ | ||
if (context.IsGenerated()) return; | ||
var invocation = (InvocationExpressionSyntax)context.Node; | ||
var parentMethod = invocation.Ancestors().OfType<MethodDeclarationSyntax>().FirstOrDefault(); | ||
if (parentMethod == null) return; | ||
var parentIsAsync = parentMethod.Modifiers.Any(n => n.IsKind(SyntaxKind.AsyncKeyword)); | ||
if (!parentIsAsync) return; | ||
// We now know that we are in async method | ||
|
||
var memberAccess = invocation.Parent as MemberAccessExpressionSyntax; | ||
if (memberAccess == null) return; | ||
var member = memberAccess.Name; | ||
if (member.ToString() != "Result") return; | ||
// We now know that we are accessing .Result | ||
|
||
var identifierSymbol = context.SemanticModel.GetSymbolInfo(memberAccess, context.CancellationToken).Symbol; | ||
if (identifierSymbol.OriginalDefinition.ToString() != "System.Threading.Tasks.Task<TResult>.Result") return; | ||
// We now know that we are accessing System.Threading.Tasks.Task<TResult>.Result | ||
|
||
SimpleNameSyntax identifier; | ||
identifier = invocation.Expression as IdentifierNameSyntax; | ||
if (identifier == null) | ||
{ | ||
var transient = invocation.Expression as MemberAccessExpressionSyntax; | ||
identifier = transient.Name; | ||
} | ||
if (identifier == null) return; // It's not supposed to happen. Don't throw an exception, though. | ||
context.ReportDiagnostic(Diagnostic.Create(Rule, identifier.GetLocation(), identifier.Identifier.Text)); | ||
} | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/CSharp/CodeCracker/Design/ResultInAsyncCodeFixProvider.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,75 @@ | ||
using CodeCracker.Properties; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeActions; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Formatting; | ||
using Microsoft.CodeAnalysis.Simplification; | ||
using System; | ||
using System.Collections.Immutable; | ||
using System.Composition; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace CodeCracker.CSharp.Design | ||
{ | ||
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(ResultInAsyncCodeFixProvider)), Shared] | ||
public class ResultInAsyncCodeFixProvider : CodeFixProvider | ||
{ | ||
public sealed override ImmutableArray<string> FixableDiagnosticIds => | ||
ImmutableArray.Create(DiagnosticId.ResultInAsync.ToDiagnosticId()); | ||
|
||
public sealed override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer; | ||
|
||
public async sealed override Task RegisterCodeFixesAsync(CodeFixContext context) | ||
{ | ||
var diagnostic = context.Diagnostics.First(); | ||
var compilation = (CSharpCompilation)await context.Document.Project.GetCompilationAsync(); | ||
context.RegisterCodeFix(CodeAction.Create( | ||
Resources.ResultInAsyncCodeFixProvider_Title, | ||
ct => ReplaceResultWithAwaitAsync(context.Document, diagnostic, ct), | ||
nameof(ResultInAsyncCodeFixProvider) | ||
), diagnostic); | ||
} | ||
|
||
private async static Task<Document> ReplaceResultWithAwaitAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken) | ||
{ | ||
var root = (await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false)); | ||
var sourceSpan = diagnostic.Location.SourceSpan; | ||
var invocation = root.FindToken(sourceSpan.Start).Parent.AncestorsAndSelf().OfType<InvocationExpressionSyntax>().First(); | ||
var memberAccess = invocation.Parent as MemberAccessExpressionSyntax; | ||
|
||
// Replace memberAccess with the async invocation | ||
SyntaxNode newRoot; | ||
|
||
// See if the member access expression is a part of something bigger | ||
// i.e. something.Result.something. Then we need to produce (await something.Result).something | ||
var parentAccess = memberAccess.Parent as MemberAccessExpressionSyntax; | ||
if (parentAccess != null) | ||
{ | ||
var rewritten = | ||
SyntaxFactory.ParenthesizedExpression( | ||
SyntaxFactory.AwaitExpression( | ||
invocation) | ||
) | ||
.WithLeadingTrivia(invocation.GetLeadingTrivia()) | ||
.WithTrailingTrivia(invocation.GetTrailingTrivia()); | ||
var subExpression = parentAccess.Expression; | ||
newRoot = root.ReplaceNode(subExpression, rewritten); | ||
} | ||
else | ||
{ | ||
var rewritten = | ||
SyntaxFactory.AwaitExpression( | ||
invocation) | ||
.WithLeadingTrivia(invocation.GetLeadingTrivia()) | ||
.WithTrailingTrivia(invocation.GetTrailingTrivia()); | ||
newRoot = root.ReplaceNode(memberAccess, rewritten); | ||
} | ||
|
||
return document.WithSyntaxRoot(newRoot); | ||
} | ||
} | ||
} |
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
36 changes: 36 additions & 0 deletions
36
src/Common/CodeCracker.Common/Properties/Resources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.