Skip to content

Add configurable Db* special-cases for MA0042/MA0045#1136

Merged
meziantou merged 1 commit intomainfrom
meziantou/plan-db-sealed-option
May 6, 2026
Merged

Add configurable Db* special-cases for MA0042/MA0045#1136
meziantou merged 1 commit intomainfrom
meziantou/plan-db-sealed-option

Conversation

@meziantou
Copy link
Copy Markdown
Owner

MA0042 and MA0045 were still raising await-using diagnostics for common ADO.NET factory-returned types, including the SqlDataReader scenario from #1134. In most real providers, DisposeAsync falls back to synchronous disposal in base Db* implementations, so these reports were often false positives.

Summary

  • Added a new analyzer option: MA0042.enable_db_special_cases (default: true).
  • Updated DoNotUseBlockingCallInAsyncContextAnalyzer await-using logic to treat Db* compile-time hierarchies as effectively sealed for:
    • DbConnection
    • DbCommand
    • DbDataReader
    • DbTransaction
    • DbBatch
  • With the option enabled, factory-returned Db* values are only reported when the known hierarchy already overrides DisposeAsync below the relevant Db* base type.
  • Preserved SQLite special-case behavior and precedence (existing enable_sqlite_special_cases semantics remain unchanged).
  • Updated rule documentation for MA0042/MA0045 to describe the new option and default behavior.
  • Expanded tests to cover:
    • new default no-diagnostic behavior for Db* factory-returned values without DisposeAsync overrides,
    • diagnostics when MA0042.enable_db_special_cases = false,
    • both MA0042 and MA0045 analyzer paths.

Validation

  • dotnet test tests/Meziantou.Analyzer.Test/Meziantou.Analyzer.Test.csproj --filter "FullyQualifiedName~DoNotUseBlockingCallInAsyncContextAnalyzer_AsyncContextTests|FullyQualifiedName~DoNotUseBlockingCallInAsyncContextAnalyzer_NonAsyncContextTests"
  • dotnet test tests/Meziantou.Analyzer.Test/Meziantou.Analyzer.Test.csproj /p:RoslynVersion=roslyn4.2 --filter "FullyQualifiedName~DoNotUseBlockingCallInAsyncContextAnalyzer_AsyncContextTests|FullyQualifiedName~DoNotUseBlockingCallInAsyncContextAnalyzer_NonAsyncContextTests"
  • dotnet run --project src/DocumentationGenerator
  • dotnet build Meziantou.Analyzer.slnx

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@meziantou meziantou merged commit 7ff3aa6 into main May 6, 2026
13 checks passed
@meziantou meziantou deleted the meziantou/plan-db-sealed-option branch May 6, 2026 23:43
This was referenced May 7, 2026
This was referenced May 9, 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