Skip to content

NUnit Migration: Methods with ref/out parameters incorrectly converted to async #4341

@thomhurst

Description

@thomhurst

Description

When running the TUnit NUnit migration code fixer, methods containing ref, in, or out parameters are incorrectly converted to async Task methods. This causes compilation errors because C# does not allow async methods to have ref, in, or out parameters.

Steps to Reproduce

  1. Create a project with NUnit tests that have helper methods with ref or out parameters
  2. Add TUnit packages
  3. Run dotnet format analyzers --severity info --diagnostics TUNU0001
  4. Build the project

Expected Behavior

Methods with ref, in, or out parameters should NOT be converted to async methods. The assertions inside them should remain synchronous or the migration should leave them untouched.

Actual Behavior

Methods with ref/out parameters are converted to async, causing build errors:

error CS1988: Async methods cannot have ref, in or out parameters

Example

Before migration:

private static void HandleRealized(object sender, object expected, ref bool realized, string message)
{
    Assert.AreSame(AsVirtual(expected), sender, message);
    realized = true;
}

After migration (current - broken):

private async static Task HandleRealized(object sender, object expected, ref bool realized, string message)
{
    await Assert.That(sender).IsSameReferenceAs(AsVirtual(expected)).Because(message);
    realized = true;
}

Expected after migration:

Option 1 - Don't convert to async:

private static void HandleRealized(object sender, object expected, ref bool realized, string message)
{
    Assert.That(sender).IsSameReferenceAs(AsVirtual(expected)).Because(message).Wait();
    realized = true;
}

Option 2 - Leave unchanged for manual migration:

// Original code left unchanged - manual migration required due to ref parameter
private static void HandleRealized(object sender, object expected, ref bool realized, string message)
{
    Assert.AreSame(AsVirtual(expected), sender, message);
    realized = true;
}

Impact

This is a critical issue because it generates code that cannot compile.

Environment

  • TUnit version: 1.9.91
  • .NET version: .NET 8.0
  • OS: Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions