Skip to content

Do not call Task.WhenAll with a single argument #33806

Closed
dotnet/roslyn-analyzers
#4841

Description

@terrajobst

There's no reason to call WhenAll with a single Task; just use the Task.

Category: Performance

Activity

  1. added
    api-suggestion
    code-analyzer
    on Mar 19, 2020
  2. added
    untriaged
    on Mar 19, 2020
  3. added this to the Future milestone on Mar 20, 2020
  4. added
    code-fixer
    on Mar 21, 2020
  5. jeffhandley commented on Mar 21, 2020

    @jeffhandley
    Member

    Estimates:

    • Analyzer: Small
    • Fixer: Medium
  6. removed
    untriaged
    on Mar 21, 2020
  7. carlossanlop commented on Oct 30, 2020

    @carlossanlop
    Contributor

    If the API review folks agree, we can group this proposal with #33807.

    If they prefer a separate rule ID, then please approve separately.

    Here is a usage example:

    using System.Threading.Tasks;
    
    namespace MyNamespace
    {
        class Sample
        {
            public static void Main()
            {
                Task task = ...;
    
                // Instead of this
                await Task.WhenAll(task);
                // Suggest this
                await task;
    
                // Instead of this
                await Task.WhenAll(GetTask());
                // Suggest this
                await GetTask();
    
                // Analyzer should trigger, but should not offer a fix because whenResult is used somewhere else
                Task whenResult = Task.WhenAll(task);
                DoSomethingWithTask(whenResult);
            }
    
            static Task GetTask()
            {
                return ...;
            }
    
            static void DoSomethingWithTask(Task task)
            {
                ...;
            }
        }
    }
    • This analyzer should not affect any of the other WhenAll overloads, because they take a Task[]. Only this overload, which takes a params Task[] task would be affected.

    • This analyzer should also have a fixer, except for the 3rd case indicated in the usage example.

    • Suggested severity: Info.

    • Suggested milestone if approved: 6.0 (seems simple to address).

    • Edit: Removed the incorrect Wait() usages and added await as described in the video.

  8. added
    api-ready-for-review
    and removed
    api-suggestion
    on Oct 30, 2020
  9. added
    api-approved
    and removed
    api-ready-for-review
    on Feb 4, 2021
  10. terrajobst commented on Feb 4, 2021

    @terrajobst
    ContributorAuthor

    Video

    • Makes sense, but we shouldn't use Wait():
    async Task M(Task task)
    {
        // Before
        await Task.WhenAll(task);
    
        // After
        await task;
    }
  11. added
    help wanted
    on Feb 4, 2021
  12. carlossanlop commented on Feb 4, 2021

    @carlossanlop
    Contributor

    Thanks for the correction, and thanks for approving.

  13. removed
    help wanted
    on Feb 5, 2021
  14. carlossanlop commented on Feb 5, 2021

    @carlossanlop
    Contributor

    This is also yours, @ryzngard, per your request in #33807. Thanks for your help! Let us know if you have any questions.

  15. jeffhandley commented on Feb 5, 2021

    @jeffhandley
    Member

    @ryzngard Here's a getting started guide as well. Included in there is our typical "Definition of Done" for creating .NET Libraries analyzers.

  16. stephentoub commented on Jul 13, 2021

    @stephentoub
    Member

    This was implemented as CA1842/1843. Thanks!

  17. ghost locked as resolved and limited conversation to collaborators on Aug 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.Threading.Taskscode-analyzercode-fixer

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions