Skip to content

Commit

Permalink
Merge pull request #1355 from AArnott/fix1354
Browse files Browse the repository at this point in the history
Raise VSTHRD002 on improper sync-blocking of configured awaiters
  • Loading branch information
AArnott authored Sep 23, 2024
2 parents bbe3328 + 183e5de commit d516ea9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
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|]();
}
}
";
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

0 comments on commit d516ea9

Please sign in to comment.