Skip to content

MA0042: add DbCommand await-using parity with DbConnection#1124

Merged
meziantou merged 1 commit intomainfrom
meziantou/ma0042-dbcommand
May 5, 2026
Merged

MA0042: add DbCommand await-using parity with DbConnection#1124
meziantou merged 1 commit intomainfrom
meziantou/ma0042-dbcommand

Conversation

@meziantou
Copy link
Copy Markdown
Owner

@meziantou meziantou commented May 5, 2026

MA0042 already avoids reporting using -> await using for directly instantiated DbConnection subclasses when they do not override DisposeAsync, because the base implementation is synchronous. This change applies the same behavior to DbCommand to avoid unnecessary diagnostics in equivalent cases.

What changed

  • Extended DoNotUseBlockingCallInAsyncContextAnalyzer to resolve System.Data.Common.DbCommand and apply the same direct-new subclass check used for DbConnection.
  • For DbCommand instances created with new, diagnostics are now reported only when DisposeAsync is declared in the subclass chain (excluding DbCommand itself).
  • Kept existing behavior for factory-returned DbCommand instances, which still report because runtime types may override DisposeAsync.
  • Added MA0042 tests covering:
    • direct new with no override (no diagnostic)
    • factory method with no override (diagnostic)
    • direct new with override (diagnostic)
    • override in an intermediate base (diagnostic)
  • Updated docs/Rules/MA0042.md to document the new DbCommand behavior alongside Stream and DbConnection.

Validation

  • Ran DoNotUseBlockingCallInAsyncContextAnalyzer_AsyncContextTests on default Roslyn.
  • Ran the same test set with /p:RoslynVersion=roslyn4.2.
  • Ran dotnet run --project src/DocumentationGenerator until stable (no further markdown updates).

Contributes to #1121

Add DbCommand parity with DbConnection for direct object creation in await-using diagnostics, and update tests and MA0042 documentation.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@meziantou meziantou merged commit c1b7418 into main May 5, 2026
13 checks passed
@meziantou meziantou deleted the meziantou/ma0042-dbcommand branch May 5, 2026 16:04
This was referenced May 5, 2026
This was referenced May 8, 2026
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.

1 participant