Skip to content

Commit cfe1b13

Browse files
Fix async modifier on abstract methods
Only add async modifier when method is not abstract and returns Task-like type Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
1 parent 98f735b commit cfe1b13

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/Analyzers/CSharp/Tests/GenerateMethod/GenerateMethodTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10613,4 +10613,33 @@ private int Goo()
1061310613
}
1061410614
}
1061510615
""", new(parseOptions: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp14)));
10616+
10617+
[Fact]
10618+
public Task TestGenerateAbstractMethodReturningTask()
10619+
=> TestInRegularAndScriptAsync(
10620+
"""
10621+
using System.Threading.Tasks;
10622+
10623+
abstract class C
10624+
{
10625+
async Task M()
10626+
{
10627+
await [|M2|]();
10628+
}
10629+
}
10630+
""",
10631+
"""
10632+
using System.Threading.Tasks;
10633+
10634+
abstract class C
10635+
{
10636+
async Task M()
10637+
{
10638+
await M2();
10639+
}
10640+
10641+
protected abstract Task M2();
10642+
}
10643+
""",
10644+
index: 1);
1061610645
}

src/Analyzers/Core/CodeFixes/GenerateParameterizedMember/AbstractGenerateParameterizedMemberService.SignatureInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public async ValueTask<IMethodSymbol> GenerateMethodAsync(
105105
attributes: default,
106106
accessibility: DetermineAccessibility(isAbstract),
107107
modifiers: DeclarationModifiers.None
108-
.WithIsStatic(State.IsStatic).WithIsAbstract(isAbstract).WithIsUnsafe(isUnsafe).WithAsync(knownTypes.IsTaskLike(returnType)),
108+
.WithIsStatic(State.IsStatic).WithIsAbstract(isAbstract).WithIsUnsafe(isUnsafe).WithAsync(!isAbstract && knownTypes.IsTaskLike(returnType)),
109109
returnType: returnType,
110110
refKind: DetermineRefKind(cancellationToken),
111111
explicitInterfaceImplementations: default,

0 commit comments

Comments
 (0)