Skip to content

Commit

Permalink
Fixes microsoft#772. VSTHRD110 now detects a ConditionalAccessExpress…
Browse files Browse the repository at this point in the history
…ionSyntax parent of the invocation.
  • Loading branch information
bluetarpmedia committed Jun 16, 2021
1 parent 94e1337 commit f8406d0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Expand Up @@ -46,7 +46,7 @@ internal void AnalyzeInvocation(SyntaxNodeAnalysisContext context)

// Only consider invocations that are direct statements. Otherwise, we assume their
// result is awaited, assigned, or otherwise consumed.
if (invocation.Parent?.GetType().Equals(typeof(ExpressionStatementSyntax)) ?? false)
if (invocation.Parent is ExpressionStatementSyntax || invocation.Parent is ConditionalAccessExpressionSyntax)
{
var methodSymbol = context.SemanticModel.GetSymbolInfo(context.Node).Symbol as IMethodSymbol;
if (this.IsAwaitableType(methodSymbol?.ReturnType, context.Compilation, context.CancellationToken))
Expand Down
Expand Up @@ -363,6 +363,45 @@ void Foo()
await Verify.VerifyAnalyzerAsync(test, expected);
}

[Fact]
public async Task ConditionalAccess_ProducesDiagnostic()
{
var test = @"
using System.Threading.Tasks;
class Test {
void Foo(Test? tester)
{
tester?.BarAsync();
}
Task BarAsync() => null;
}
";

DiagnosticResult expected = this.CreateDiagnostic(7, 17, 8);
await Verify.VerifyAnalyzerAsync(test, expected);
}

[Fact]
public async Task ConditionalAccessAwaited_ProducesNoDiagnostic()
{
var test = @"
using System.Threading.Tasks;
class Test {
async Task Foo(Test? tester)
{
await (tester?.BarAsync() ?? Task.CompletedTask);
}
Task BarAsync() => null;
}
";

await Verify.VerifyAnalyzerAsync(test);
}

private DiagnosticResult CreateDiagnostic(int line, int column, int length)
=> Verify.Diagnostic().WithSpan(line, column, line, column + length);
}
Expand Down

0 comments on commit f8406d0

Please sign in to comment.