Do not report MA0214 on functions returning only completed tasks - #1372
Merged
Merged
Conversation
MA0214 flagged "async bridging methods" such as `Task FooAsync() => Task.CompletedTask;` or `Task<bool> BarAsync() => Task.FromResult(false);`, where awaiting the task brings no benefit and only adds a state machine. The rule is now skipped when every return of the function produces an already-completed task (Task/ValueTask CompletedTask, FromResult, FromException, FromCanceled, new ValueTask(), new ValueTask<T>(value)). Functions mixing a completed task with a real task are still reported. The detection of completed tasks moves from AwaitTaskBeforeDisposingResourcesAnalyzer to AwaitableTypes.IsCompletedTask so that both rules share it. Fixes #1371
This was referenced Sep 3, 2026
Closed
Closed
Bump Meziantou.Analyzer from 3.0.139 to 3.0.203
Analogy-LogViewer/Analogy.LogViewer.NLog.Targets#561
Closed
Closed
Bump Meziantou.Analyzer from 3.0.139 to 3.0.203
Analogy-LogViewer/Analogy.AspNetCore.LogProvider#547
Closed
Closed
Closed
This was referenced Sep 25, 2026
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1371
What
MA0214 (Use 'await' instead of returning the task) no longer reports a function when every return produces an already-completed task:
Completed tasks recognized:
Task.CompletedTask/ValueTask.CompletedTask,Task/ValueTaskFromResult,FromException,FromCanceled,new ValueTask(), andnew ValueTask<T>(value).Functions that mix a completed task with a real task are still reported on every return:
Why
These are "bridging methods", e.g. implementing an asynchronous interface synchronously. Awaiting the task there brings no accuracy to the stack trace and only adds a state machine, so the rule was making the code worse.
Notes for the reviewer
AwaitTaskBeforeDisposingResourcesAnalyzer(MA0100). It moved toAwaitableTypes.IsCompletedTaskand both rules now share it; MA0100 behavior is unchanged (its four now-unused symbol properties were removed).async Task Foo() => await Task.CompletedTask;it already suggestsTask Foo() => Task.CompletedTask;, which is the shape MA0214 now leaves alone.CompletedTaskproperty declared on another type (still reported).dotnet run --project src/DocumentationGeneratorproduces no further markdown change. Full test suite passes on roslyn5.9 (3862 tests) and roslyn4.8 (3750 tests).