You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A developer working on an async method may end up following a series of steps resulting in a completely unnecessary return await Task.FromResult( foo ); expression, instead of the much simpler return foo;.
This analyzer would not be about correctness, but cleanliness.
asyncTask<bool>Foo(){boolknownResult=false;returnawaitTask.FromResult(knownResult);// D2LXXXX: Do not await a Task.FromResult. Simply use the result value instead.}
Writing this analyzer becomes somewhat easier due to D2L0055, which requires that all awaited tasks are configured, and so we can limit the syntax we expect somewhat
It's worth noting that applying this codefix could easily end up with warning CS1998, but that's probably reasonable to let the developer deal with converting it to a non-async method.
// warning CS1998: This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.asyncTask<bool>Foo(){boolknownResult=false;returnknownResult;}
The text was updated successfully, but these errors were encountered:
A developer working on an
async
method may end up following a series of steps resulting in a completely unnecessaryreturn await Task.FromResult( foo );
expression, instead of the much simplerreturn foo;
.This analyzer would not be about correctness, but cleanliness.
Writing this analyzer becomes somewhat easier due to D2L0055, which requires that all awaited tasks are configured, and so we can limit the syntax we expect somewhat
Breaking this down:
IAwaitOperation
s.Operand
(Task.FromResult( 1 ).ConfigureAwait( false )
)InvocationExpression
and access it'sExpression
(Task.FromResult( 1 ).ConfigureAwait
)MemberAccessExpression
and access it'sExpression
(Task.FromResult( 1 )
)IncovationExpression
and access it'sExpression
MemberAccessExpression
FromResult
and theExpression
is of typeTask
, raise a diagnosticClearly there are other scenarios this would not cover, but I would not expect an initial implementation to.
Additionally, a codefix could be provided to remove the await expression.
It's worth noting that applying this codefix could easily end up with warning
CS1998
, but that's probably reasonable to let the developer deal with converting it to a non-async
method.The text was updated successfully, but these errors were encountered: