Skip to content

Do not report CA2007 for pattern-based await using and await foreach - #55036

Merged
tannergooding merged 5 commits into
dotnet:mainfrom
DoctorKrolic:configure-pattern-based-awaits
Jul 16, 2026
Merged

Do not report CA2007 for pattern-based await using and await foreach#55036
tannergooding merged 5 commits into
dotnet:mainfrom
DoctorKrolic:configure-pattern-based-awaits

Conversation

@DoctorKrolic

Copy link
Copy Markdown
Contributor

Fixes: #53461

@DoctorKrolic

Copy link
Copy Markdown
Contributor Author

May I have a review here please?

@tannergooding tannergooding left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This needs a secondary sign-off, I've tagged some folks internally

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Updates the CA2007 (DoNotDirectlyAwaitATask) analyzer to avoid reporting diagnostics for pattern-based await using and await foreach constructs where ConfigureAwait cannot be applied (e.g., duck-typed async disposal/enumeration), addressing #53461.

Changes:

  • Refines await using and await foreach analysis to only report when the resource/collection is implicitly convertible to IAsyncDisposable / IAsyncEnumerable<T>, excluding pattern-based-only types.
  • Adds unit tests covering pattern-based await using (statement + declaration) and pattern-based await foreach to ensure no CA2007 diagnostic is reported.
Show a summary per file
File Description
src/Microsoft.CodeAnalysis.NetAnalyzers/src/Microsoft.CodeAnalysis.NetAnalyzers/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/DoNotDirectlyAwaitATask.cs Narrows CA2007 reporting for await using/await foreach to interface-convertible cases to avoid flagging pattern-only types.
src/Microsoft.CodeAnalysis.NetAnalyzers/tests/Microsoft.CodeAnalysis.NetAnalyzers.UnitTests/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/DoNotDirectlyAwaitATaskTests.cs Adds regression tests ensuring no diagnostic for pattern-based await using/await foreach.

Copilot's findings

  • Files reviewed: 2/2 changed files
  • Comments generated: 1

Comment on lines 906 to +907
}
} No newline at end of file
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'll leave this up to you, feel free to ignore and close if you don't think its meaningful enough to provide.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@DoctorKrolic if you could consider this, possibly for a follow up, that'd be great. I didn't feel it worth blocking on but would be add a little more robustness

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.

I am currently on vacation, will look at it when I return

@tannergooding
tannergooding merged commit 8584580 into dotnet:main Jul 16, 2026
27 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview7 milestone Jul 17, 2026
@DoctorKrolic
DoctorKrolic deleted the configure-pattern-based-awaits branch July 25, 2026 13:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CA2007 flagged for await using duck-typed async disposable.

4 participants