Skip to content

Commit

Permalink
Fix InvalidCastException thrown from VSTHRD102
Browse files Browse the repository at this point in the history
  • Loading branch information
AArnott committed Jan 26, 2022
1 parent 1160dbb commit 0afa632
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
Expand Up @@ -176,9 +176,8 @@ private static bool IsTaskCompletedWithWhenAll(SyntaxNodeAnalysisContext context
MemberAccessExpressionSyntax? memberAccess = memberAccessList.First();

// Does the invocation have the expected `Task.WhenAll` syntax? This is cheaper to verify before looking up its semantic type.
var correctSyntax =
((IdentifierNameSyntax)memberAccess.Expression).Identifier.ValueText == Types.Task.TypeName &&
((IdentifierNameSyntax)memberAccess.Name).Identifier.ValueText == Types.Task.WhenAll;
bool correctSyntax = memberAccess.Expression is IdentifierNameSyntax { Identifier.ValueText: Types.Task.TypeName }
&& memberAccess.Name is IdentifierNameSyntax { Identifier.ValueText: Types.Task.WhenAll };

if (!correctSyntax)
{
Expand Down
Expand Up @@ -353,6 +353,36 @@ public class Test {
public void Advise(Action<int> foo) { }
}
";
await Verify.VerifyAnalyzerAsync(test);
}

[Fact]
public async Task JtfRunInPrivateMethod__WithMultiMemberAccessExpression_ProducesDiagnostic()
{
var test = @"
using System.Threading.Tasks;
using Microsoft.VisualStudio.Threading;
class Other {
internal static JoinableTaskFactory factory;
}
class Test {
Foo foo;
JoinableTaskFactory jtf;
void F() {
object v = Other.factory.[|Run|](async () => { return await this.foo.DoSomethingAsync(); });
jtf.[|Run|](async delegate {
await jtf.SwitchToMainThreadAsync();
});
}
class Foo {
internal Task<object> DoSomethingAsync() => new TaskCompletionSource<object>().Task;
}
}
";
await Verify.VerifyAnalyzerAsync(test);
}
Expand Down

0 comments on commit 0afa632

Please sign in to comment.