Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix InvalidCastException thrown from VSTHRD102 #979

Merged
merged 1 commit into from Jan 26, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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