Skip to content

Code fixer adds async keyword but does not change return type from object to Task<object> #4491

@thomhurst

Description

@thomhurst

Description

The NUnit migration code fixer (TUNU0001) adds the async keyword to methods but fails to update the return type when the original return type is object. This results in a compilation error because async methods cannot return object directly - they must return Task, Task<T>, ValueTask, or ValueTask<T>.

Reproduction Steps

  1. Create a file with a method returning object that contains assertions:
using NUnit.Framework;

public abstract class BaseClass
{
    public abstract object Invoke();
}

public class TestClass : BaseClass
{
    public override object Invoke()
    {
        Assert.NotNull("test");
        Assert.AreEqual(1, 1);
        return "value";
    }
}
  1. Run the code fixer: dotnet format analyzers --severity info --diagnostics TUNU0001

  2. The code is converted to:

using System.Threading.Tasks;

public abstract class BaseClass
{
    public abstract object Invoke();
}

public class TestClass : BaseClass
{
    public override async object Invoke()  // ERROR: async methods cannot return object
    {
        Assert.NotNull("test");
        await Assert.That(1).IsEqualTo(1);
        return "value";
    }
}
  1. Building produces error:
error CS1983: The return type of an async method must be void, Task, Task<T>, ValueTask, or ValueTask<T>

Expected Behavior

When the code fixer adds the async keyword to a method, it should also update the return type:

  • objectTask<object>
  • intTask<int>
  • etc.

The correct conversion should be:

public override async Task<object> Invoke()
{
    Assert.NotNull("test");
    await Assert.That(1).IsEqualTo(1);
    return "value";
}

Note: The base class declaration would also need to be updated, or the code fixer should recognize that changing the return type would break inheritance and handle this case specially.

Environment

  • TUnit Version: 1.11.64
  • .NET SDK: 10.0.100-preview.1.25103.12
  • OS: Windows 11

Additional Context

This issue was found in a real codebase where a class overrides a method from an abstract base class. The method contains NUnit assertions that were converted to TUnit's async assertions, but the return type was not updated.


Note: Please add tests when fixing this issue to prevent regression.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions