-
-
Notifications
You must be signed in to change notification settings - Fork 110
Description
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
- Create a file with a method returning
objectthat 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";
}
}-
Run the code fixer:
dotnet format analyzers --severity info --diagnostics TUNU0001 -
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";
}
}- 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:
object→Task<object>int→Task<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.