New analyzer: Replace Task.Result with await and make method async Task
Recently we've found some deadlocks in our code. They were caused by a stray .Result in situations when there was an async method earlier in the call stack. Using .Result is considered a bad practice.
I propose an analyzer that removes .Result and replaces it with an await call
Before:
private async Task<bool> DoSomethingAsync()
{
return AsyncMethod().Result;
}
After:
private async Task<bool> DoSomethingAsync()
{
return await AsyncMethod();
}
Category: Design
Severity: Warning
Diagnostic id: CC0122
I'm curious what's your opinion on
- This refactoring will cause a build error, because method's signature changes
- Are there any use cases where using
.Result is OK?
New analyzer: Replace
Task.Resultwithawaitand make methodasync TaskRecently we've found some deadlocks in our code. They were caused by a stray
.Resultin situations when there was anasyncmethod earlier in the call stack. Using.Resultis considered a bad practice.I propose an analyzer that removes
.Resultand replaces it with anawaitcallBefore:
After:
Category:
DesignSeverity:
WarningDiagnostic id:
CC0122I'm curious what's your opinion on
.Resultis OK?