dotnet/roslyn

Consider new warning for unobserved async-iterator call

開放

#33,939 建立於 2019年3月7日

 (1 則留言) (0 個反應) (0 位負責人)C# (4,257 個分叉)batch import
Area-CompilersBugConcept-Diagnostic ClarityFeature - Async Streamshelp wanted

倉庫指標

星標
 (20,414 顆星)
PR 合併指標
 (平均合併 6天 17小時) (30 天內合併 256 個 PR)

描述

Version Used: 3.0.19.12206 (ec366687)

Steps to Reproduce: Compile the following code:

using System.Collections.Generic;
using System.Threading.Tasks;

class C
{
  async IAsyncEnumerable<int> ProduceAsync()
  {
    await Task.CompletedTask;
    yield return 42;
  }

  void Consume()
  {
    ProduceAsync(); // currently emits WRN_UnobservedAwaitableExpression
  }
}

Expected Behavior:

Emit new warning with text like "Because result of asynchronous iterator call is not being iterated, execution of the current method continues before the call is completed. Consider using the 'await foreach' over the result of the call." For async-iterators returning IAsyncEnumerator<T> the second sentence may be omitted. Or maybe you'll come up with better wording which will work for both cases :)

Actual Behavior:

The warning "CS4014: Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call" is emitted. But it's somewhat misleading.

貢獻者指南