Additional StyleCop rules for async / await
style programming.
The following examples will result in StyleCop warnings:
async void DoSomethingAsync()
--> method should return awaitable instead of void- see Stephen Cleary, Phil Haack,...
async Task DoSomething()
--> method should be namedDoSomethingAsync
- see Task-based Asynchronous Pattern (TAP)
void DoSomethingAsync()
--> method should haveasync
modifier or return a Task/Task- see Task-based Asynchronous Pattern (TAP)
If you have StyleCop integrated into your build already, just install the nuget package StyleCop.CSharp.Async.Rules in all projects you want to be verified.
If you don't have installed StyleCop yet, I'd recommend installing the nuget package StyleCop.MsBuild.
The rules-dll (StyleCop.CSharp.Async.Rules.dll) is available on the build server(see Artifacts).
You can place it alongside the StyleCop.dll and StyleCop will automatically pick it up.
Alternatively, you can also tell StyleCop where to pick it up by defining an StyleCopAdditionalAddinPaths
item in the *.csproj file:
<ItemGroup>
<StyleCopAdditionalAddinPaths Include="..\StyleCop.CSharp.Async.Rules\">
<Visible>false</Visible>
</StyleCopAdditionalAddinPaths>
</ItemGroup>
Description, supressing a violation and disabling a rule entirely
ID: AR0001:MethodsWithAsyncModifierMustEndWithAsync
Violated when an async
method is named Foo()
instead of FooAsync()
.
[SuppressMessage(
"StyleCop.CSharp.AsyncRules",
"AR0001:MethodsWithAsyncModifierMustEndWithAsync",
Justification = "Yipii-ai-ei-oh")]
async Task DoSomething() { }
<Analyzer AnalyzerId="StyleCop.CSharp.AsyncRules">
<Rules>
<Rule Name="MethodsWithAsyncModifierMustEndWithAsync">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
</Rules>
</Analyzer>
ID: AR0002:MethodEndingWithAsyncMustHaveAsyncModifierOrReturnTask
Hint: This was recently adapted and renamed from MethodEndingWithAsyncMustHaveAsyncModifier to MethodEndingWithAsyncMustHaveAsyncModifierOrReturnTask
Violated when a method named FooAsync
does not have the async
modifier and does not return a Task
/ Task<T>
.
[SuppressMessage(
"StyleCop.CSharp.AsyncRules",
"AR0002:MethodEndingWithAsyncMustHaveAsyncModifierOrReturnTask",
Justification = "I'm cheating")]
int FooAsync() { }
<Analyzer AnalyzerId="StyleCop.CSharp.AsyncRules">
<Rules>
<Rule Name="MethodEndingWithAsyncMustHaveAsyncModifierOrReturnTask">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
</Rules>
</Analyzer>
ID: AR1001:MethodsWithAsyncModifierShouldReturnAwaitable
Violated when a method async void FooAsync()
returns void instead of Task
or another awaitable type.
[SuppressMessage(
"StyleCop.CSharp.AsyncRules",
"AR1001:MethodsWithAsyncModifierShouldReturnAwaitable",
Justification = "no need to wait for task to end")]
async void HandleEventAsync() { }
<Analyzer AnalyzerId="StyleCop.CSharp.AsyncRules">
<Rules>
<Rule Name="MethodsWithAsyncModifierShouldReturnAwaitable">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
</Rules>
</Analyzer>