Skip to content

Add back async fixers#81835

Merged
CyrusNajmabadi merged 25 commits into
dotnet:mainfrom
CyrusNajmabadi:asyncCodeFixes
Jan 7, 2026
Merged

Add back async fixers#81835
CyrusNajmabadi merged 25 commits into
dotnet:mainfrom
CyrusNajmabadi:asyncCodeFixes

Conversation

@CyrusNajmabadi

@CyrusNajmabadi CyrusNajmabadi commented Jan 2, 2026

Copy link
Copy Markdown
Contributor

This PR adds a new IDE/code-style layer analyzer for detecting when an async method/lambda contains no awaits and thus can be made synchronous (and no longer task-returning). This previously existed, but was removed when we removed the reporting of CS1998 in #80144.

The reason to add this back in is that it can still be useful for users to know when they have made a method asynchronous unintentionally. For example, if it at one point had an async call, but then the last async call was removed. In that case, the method could be made entirely synchronous, potentially virally, making a large swath of a component that was previously async, sync.

The analyzer is NOT enabled by default. It can be opted into by users who liked having CS1998 catch and notify them when they no longer needed tasks at all.

There are places though where this sort of notification may be undesirable. For example, it is common to have task-returning methods in interfaces or as virtual methods in base classes. Implementations of these methods need to stay task-returning to abide by their contract. Because of this, instead of having a single diagnostic ID controlling this, we have two:

  1. IDE0390. Reports on normal methods and lambdas.
  2. IDE0391. Reports on methods that are interface-impls or overrides.

Users who want to strictly avoid unnecessary task-returning methods in all cases can enable both. Users that just want to know for the standard case, but need to keep tasks for interface/overrides, can just enable the first.

--

Note: teh codefixprovider was just brought back from #80144, as were the tests. The tests were updated to use the new diagnostic id. and new tests were added to validate that we now have two distinct diagnostic IDs.

Comment thread src/Dependencies/Threading/IAsyncEnumerableExtensions.cs
@CyrusNajmabadi CyrusNajmabadi marked this pull request as ready for review January 2, 2026 19:46
@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner January 2, 2026 19:46
[ExtensionOrder(After = PredefinedCodeFixProviderNames.AddImport)]
[method: ImportingConstructor]
[method: SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
internal sealed class CSharpMakeMethodSynchronousCodeFixProvider() : AbstractMakeMethodSynchronousCodeFixProvider

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this type is unchanged from the original. It was brought back from #80144

[ExtensionOrder(After = PredefinedCodeFixProviderNames.MakeMethodSynchronous)]
[method: ImportingConstructor]
[method: SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
internal sealed partial class CSharpRemoveAsyncModifierCodeFixProvider() : AbstractRemoveAsyncModifierCodeFixProvider<ReturnStatementSyntax, ExpressionSyntax>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type is unchanged. It was brought back directly from #80144


namespace Microsoft.CodeAnalysis.CSharp.RemoveAsyncModifier;

internal static class RemoveAsyncModifierHelpers

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type is unchanged it was brought back directly from #80144

@CyrusNajmabadi

Copy link
Copy Markdown
Contributor Author

@dotnet/roslyn-ide @JoeRobich ptal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants