MP0006
Generic module returns a task without async/await, except Task.FromResult; non-generic modules are exempt.
| Property | Value |
|---|---|
| Category | Usage |
| Default severity | Warning |
| Availability | Public analyzer package |
Why this rule exists
Using async and awaiting the result in a Module<T> keeps the method's scope alive until the operation completes. This matters when the method owns a resource with using or performs cleanup in finally: returning a task directly can run that cleanup before the task finishes.
MP0006 is a warning because returning a task directly is valid when no scope needs to remain alive. The pipeline still awaits the returned task. For an intentional pass-through, you can suppress the warning or configure its severity.
The rule exempts non-generic Module implementations, including inherited implementations. They can return Task.CompletedTask for synchronous work or delegate to another task. Generic modules that return only Task.FromResult(...) are also exempt.
Example
public class VersionModule : Module<string>
{
protected override async Task<string> ExecuteAsync(
IModuleContext context,
CancellationToken cancellationToken)
{
return await File.ReadAllTextAsync("version.txt", cancellationToken);
}
}
The code fix adds async and awaits the returned task. Non-generic modules do not need this change:
public class CleanupModule : Module
{
protected override Task ExecuteAsync(
IModuleContext context,
CancellationToken cancellationToken)
{
context.Files.GetFolder("./temp").Delete();
return Task.CompletedTask;
}
}
Configure severity
dotnet_diagnostic.MP0006.severity = warning
Use none to disable the rule, or silent, suggestion, warning, or error to change its severity.
Suppress a specific occurrence
#pragma warning disable MP0006
// Code that intentionally violates MP0006.
#pragma warning restore MP0006