Skip to content

Assert.MultipleAsync -> EnterMultipleScope autofix produce invalid codeΒ #888

Closed

Description

@MaceWindu
[Test]
public async ValueTask Test()
{
    await Assert.MultipleAsync(async () =>
    {
        await Task.CompletedTask;
    });
}

after fix applied:

// 1. ValueTask replaced with Task
// 2. duplicate async added
[Test]
public async async Task Test()
{
    using (Assert.EnterMultipleScope())
    {
        await Task.CompletedTask;
    }
}

Activity

  1. MaceWindu commented on Jun 12, 2025

    @MaceWindu
    Author

    Actually it is even worse. ValueTask -> Task conversion is not a big deal, but it also replace generic tasks with Task.

  2. mikkelbu commented on Jun 12, 2025

    @mikkelbu
    Member

    Thanks for the report @MaceWindu. I can see that I made an mistake in the IsAsyncTaskMethod

    private static bool IsAsyncTaskMethod(MethodDeclarationSyntax methodDeclaration)
    {
    var isAsync = methodDeclaration.Modifiers.Any(SyntaxKind.AsyncKeyword);
    var returnsTask = (methodDeclaration.ReturnType is IdentifierNameSyntax id && id.Identifier.Text == TaskTypeName)
    || (methodDeclaration.ReturnType is QualifiedNameSyntax qn && qn.ToString() == $"{SystemThreadingTasksNamespace}.{TaskTypeName}");
    return isAsync && returnsTask;
    }

    I wonder if it would be enough to check for the async modifier or alternatively extend the return type check with generic Task, ValueTask, and generic ValueTask.

    I should have time to look at this tonight or tomorrow

  3. added theissue type on Jun 12, 2025
  4. manfred-brands commented on Jun 12, 2025

    @manfred-brands
    Member

    @mikkelbu I think it should indeed be enough to check only for the async modifier. If it exists, no changes are needed.

  5. stevenaw commented on Jun 12, 2025

    @stevenaw
    Member

    FWIW I agree about only needing to check async. It's technically valid C# to use async void but NUnit hasn't permitted it in tests for quite a while within either the framework or analyzers. We should be able to assume any existing return type is awaitable.

  6. added this to the Release 4.9.1 milestone on Jun 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions