Skip to content
Open
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 @@ -132,9 +132,19 @@ internal void AnalyzeInvocation(SyntaxNodeAnalysisContext context)
!methodSymbol.HasAsyncCompatibleReturnType())
{
string asyncMethodName = methodSymbol.Name + VSTHRD200UseAsyncNamingConventionAnalyzer.MandatoryAsyncSuffix;

// For reduced extension methods (invoked as instance.Method()), look up the async
// alternative on the receiver type so that extension methods defined in a separate
// static class (but applicable to the receiver) are found via includeReducedExtensionMethods.
// LookupSymbols with the static declaring class as container does not return extension
// methods applicable to the receiver type.
INamespaceOrTypeSymbol lookupContainer = methodSymbol.ReducedFrom is { } reducedFrom && reducedFrom.Parameters.Length > 0
? (INamespaceOrTypeSymbol)reducedFrom.Parameters[0].Type
: methodSymbol.ContainingType;

ImmutableArray<ISymbol> symbols = context.SemanticModel.LookupSymbols(
invocationExpressionSyntax.Expression.GetLocation().SourceSpan.Start,
methodSymbol.ContainingType,
lookupContainer,
asyncMethodName,
includeReducedExtensionMethods: true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1410,6 +1410,55 @@ async Task<int> BarAsync(int id) {
await CSVerify.VerifyAnalyzerAsync(test);
}

[Fact]
public async Task SyncExtensionMethodWhereAsyncAlternativeExistsInSameStaticClassGeneratesWarning()
{
var test = @"
using System.Threading.Tasks;

public interface IExecutable { }

public static class ExecutableExtensions
{
public static string GetOutput(this IExecutable executable) => """";
public static Task<string> GetOutputAsync(this IExecutable executable) => Task.FromResult("""");
}

class Test
{
async Task DoWorkAsync()
{
IExecutable exec = null!;
string result = exec.{|#0:GetOutput|}();
}
}
";

var withFix = @"
using System.Threading.Tasks;

public interface IExecutable { }

public static class ExecutableExtensions
{
public static string GetOutput(this IExecutable executable) => """";
public static Task<string> GetOutputAsync(this IExecutable executable) => Task.FromResult("""");
}

class Test
{
async Task DoWorkAsync()
{
IExecutable exec = null!;
string result = await exec.GetOutputAsync();
}
}
";

DiagnosticResult expected = CSVerify.Diagnostic(Descriptor).WithLocation(0).WithArguments("GetOutput", "GetOutputAsync");
await CSVerify.VerifyCodeFixAsync(test, expected, withFix);
}

private DiagnosticResult CreateDiagnostic(int line, int column, int length, string methodName)
=> CSVerify.Diagnostic(DescriptorNoAlternativeMethod).WithSpan(line, column, line, column + length).WithArguments(methodName);

Expand Down
Loading