Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Rename;

namespace Microsoft.VisualStudio.Threading.Analyzers;
Expand Down Expand Up @@ -67,12 +64,12 @@ public AddAsyncSuffixCodeAction(Document document, Diagnostic diagnostic, string
protected override async Task<Solution?> GetChangedSolutionAsync(CancellationToken cancellationToken)
{
SyntaxNode root = await this.document.GetSyntaxRootOrThrowAsync(cancellationToken).ConfigureAwait(false);
var methodDeclaration = (MethodDeclarationSyntax)root.FindNode(this.diagnostic.Location.SourceSpan);
SyntaxNode declaration = root.FindNode(this.diagnostic.Location.SourceSpan);

SemanticModel? semanticModel = await this.document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
IMethodSymbol methodSymbol = semanticModel.GetDeclaredSymbol(methodDeclaration, cancellationToken) ?? throw new InvalidOperationException("Unable to get method symbol.");

Solution? solution = this.document.Project.Solution;
IMethodSymbol? methodSymbol = semanticModel?.GetDeclaredSymbol(declaration, cancellationToken) as IMethodSymbol ?? throw new InvalidOperationException("Unable to get method symbol.");

Solution? updatedSolution = await Renamer.RenameSymbolAsync(
solution,
methodSymbol,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Operations;

namespace Microsoft.VisualStudio.Threading.Analyzers;

Expand Down Expand Up @@ -54,28 +53,44 @@ public override void Initialize(AnalysisContext context)
{
CommonInterest.AwaitableTypeTester awaitableTypes = CommonInterest.CollectAwaitableTypes(context.Compilation, context.CancellationToken);
context.RegisterSymbolAction(Utils.DebuggableWrapper(context => this.AnalyzeNode(context, awaitableTypes)), SymbolKind.Method);
context.RegisterOperationAction(Utils.DebuggableWrapper(context => this.AnalyzeLocalFunction(context, awaitableTypes)), OperationKind.LocalFunction);
});
}

private void AnalyzeLocalFunction(OperationAnalysisContext context, CommonInterest.AwaitableTypeTester awaitableTypes)
{
if (this.AnalyzeMethodSymbol(context.Compilation, ((ILocalFunctionOperation)context.Operation).Symbol, awaitableTypes, context.CancellationToken) is Diagnostic diagnostic)
{
context.ReportDiagnostic(diagnostic);
}
}

private void AnalyzeNode(SymbolAnalysisContext context, CommonInterest.AwaitableTypeTester awaitableTypes)
{
var methodSymbol = (IMethodSymbol)context.Symbol;
if (this.AnalyzeMethodSymbol(context.Compilation, (IMethodSymbol)context.Symbol, awaitableTypes, context.CancellationToken) is Diagnostic diagnostic)
{
context.ReportDiagnostic(diagnostic);
}
}

private Diagnostic? AnalyzeMethodSymbol(Compilation compilation, IMethodSymbol methodSymbol, CommonInterest.AwaitableTypeTester awaitableTypes, CancellationToken cancellationToken)
{
if (methodSymbol.AssociatedSymbol is IPropertySymbol)
{
// Skip accessor methods associated with properties.
return;
return null;
}

// Skip entrypoint methods since their name is non-negotiable.
if (Utils.IsEntrypointMethod(methodSymbol, context.Compilation, context.CancellationToken))
if (Utils.IsEntrypointMethod(methodSymbol, compilation, cancellationToken))
{
return;
return null;
}

// Skip the method of the recommended dispose pattern.
if (methodSymbol.Name == "DisposeAsyncCore")
{
return;
return null;
}

bool hasAsyncFocusedReturnType = Utils.HasAsyncCompatibleReturnType(methodSymbol);
Expand All @@ -88,7 +103,7 @@ private void AnalyzeNode(SymbolAnalysisContext context, CommonInterest.Awaitable
// Do deeper checks to skip over methods that implement API contracts that are controlled elsewhere.
if (methodSymbol.FindInterfacesImplemented().Any() || methodSymbol.IsOverride)
{
return;
return null;
}

if (hasAsyncFocusedReturnType)
Expand All @@ -97,21 +112,23 @@ private void AnalyzeNode(SymbolAnalysisContext context, CommonInterest.Awaitable
// Not just any awaitable, since some stray extension method shouldn't change the world for everyone.
ImmutableDictionary<string, string?>? properties = ImmutableDictionary<string, string?>.Empty
.Add(NewNameKey, methodSymbol.Name + MandatoryAsyncSuffix);
context.ReportDiagnostic(Diagnostic.Create(
return Diagnostic.Create(
AddAsyncDescriptor,
methodSymbol.Locations[0],
properties));
properties);
}
else if (!awaitableTypes.IsAwaitableType(methodSymbol.ReturnType))
{
// Only warn about abusing the Async suffix if the return type is not awaitable.
ImmutableDictionary<string, string?>? properties = ImmutableDictionary<string, string?>.Empty
.Add(NewNameKey, methodSymbol.Name.Substring(0, methodSymbol.Name.Length - MandatoryAsyncSuffix.Length));
context.ReportDiagnostic(Diagnostic.Create(
return Diagnostic.Create(
RemoveAsyncDescriptor,
methodSymbol.Locations[0],
properties));
properties);
}
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -584,4 +584,39 @@ protected virtual async ValueTask DisposeAsyncCore()
";
await CSVerify.VerifyAnalyzerAsync(test);
}

[Fact]
public async Task LocalFunctionUsesAsyncSuffix()
{
string test = """
using System.Threading.Tasks;

class MyClass
{
void Foo()
{
async Task {|#0:Bar|}()
{
}
}
}
""";

string fix = """
using System.Threading.Tasks;

class MyClass
{
void Foo()
{
async Task BarAsync()
{
}
}
}
""";

DiagnosticResult expected = CSVerify.Diagnostic(AddSuffixDescriptor).WithLocation(0);
await CSVerify.VerifyCodeFixAsync(test, expected, fix);
}
}