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

Raise VSTHRD002 on improper sync-blocking of configured awaiters #1355

Merged
merged 1 commit into from
Sep 23, 2024
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 @@ -37,8 +37,10 @@ internal static class CommonInterest
new SyncBlockingMethod(new QualifiedMember(new QualifiedType(Namespaces.SystemThreadingTasks, nameof(Task)), nameof(Task.Wait)), null),
new SyncBlockingMethod(new QualifiedMember(new QualifiedType(Namespaces.SystemThreadingTasks, nameof(Task)), nameof(Task.WaitAll)), null),
new SyncBlockingMethod(new QualifiedMember(new QualifiedType(Namespaces.SystemThreadingTasks, nameof(Task)), nameof(Task.WaitAny)), null),
new SyncBlockingMethod(new QualifiedMember(new QualifiedType(Namespaces.SystemRuntimeCompilerServices, nameof(ConfiguredTaskAwaitable.ConfiguredTaskAwaiter)), nameof(ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult)), null),
new SyncBlockingMethod(new QualifiedMember(new QualifiedType(Namespaces.SystemRuntimeCompilerServices, nameof(TaskAwaiter)), nameof(TaskAwaiter.GetResult)), null),
new SyncBlockingMethod(new QualifiedMember(new QualifiedType(Namespaces.SystemRuntimeCompilerServices, nameof(ValueTaskAwaiter)), nameof(ValueTaskAwaiter.GetResult)), null),
new SyncBlockingMethod(new QualifiedMember(new QualifiedType(Namespaces.SystemRuntimeCompilerServices, nameof(ConfiguredValueTaskAwaitable.ConfiguredValueTaskAwaiter)), nameof(ConfiguredValueTaskAwaitable.ConfiguredValueTaskAwaiter.GetResult)), null),
};

internal static readonly IEnumerable<SyncBlockingMethod> SyncBlockingMethods = JTFSyncBlockers.Concat(ProblematicSyncBlockingMethods).Concat(new[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,34 @@ async Task FAsync() {
await CSVerify.VerifyCodeFixAsync(test, expected, withFix);
}

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

class Test {
void F() {
var task = Task.Run(() => 1);
task.ConfigureAwait(false).GetAwaiter().[|GetResult|]();
Copy link
Member

@BertanAygun BertanAygun Sep 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not familiar with [|..|] syntax? Is it specific to CSVerify to annotate the expected location of the warning?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's the much easier syntax to mark where the diagnostic is expected than what the prior tests use.

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

class Test {
async Task FAsync() {
var task = Task.Run(() => 1);
await task.ConfigureAwait(false);
}
}
";
await CSVerify.VerifyCodeFixAsync(test, withFix);
}

[Fact]
public async Task ValueTask_GetAwaiter_GetResult_ShouldReportWarning()
{
Expand Down Expand Up @@ -567,6 +595,34 @@ async Task FAsync() {
await CSVerify.VerifyCodeFixAsync(test, expected, withFix);
}

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

class Test {
void F() {
ValueTask task = default;
task.ConfigureAwait(false).GetAwaiter().[|GetResult|]();
}
}
";
var withFix = @"
using System;
using System.Threading.Tasks;

class Test {
async Task FAsync() {
ValueTask task = default;
await task.ConfigureAwait(false);
}
}
";
await CSVerify.VerifyCodeFixAsync(test, withFix);
}

[Fact]
public async Task TaskResult_FixUpdatesCallers()
{
Expand Down