Skip to content

Fix MA0042 comment placement for await using#1120

Merged
meziantou merged 1 commit into
mainfrom
meziantou/fix-ma0042-comments
May 4, 2026
Merged

Fix MA0042 comment placement for await using#1120
meziantou merged 1 commit into
mainfrom
meziantou/fix-ma0042-comments

Conversation

@meziantou
Copy link
Copy Markdown
Owner

Why

MA0042's code fix for using var did not preserve leading trivia correctly. When a comment preceded the statement, the fix inserted await on its own line before the comment instead of producing await using.

What changed

  • Added a regression test, Using_Diagnostic1_WithComment, in DoNotUseBlockingCallInAsyncContextAnalyzer_AsyncContextTests.
  • Updated ReplaceWithAwaitUsing in DoNotUseBlockingCallInAsyncContextFixer to preserve leading trivia by transferring the using token's leading trivia to the new await token and clearing it from using.

Result

The code fix now produces:

// MA0042 "Prefer using 'await using'"
await using var connection = new SqlConnection("");

instead of splitting await onto a separate line.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@meziantou meziantou enabled auto-merge (squash) May 4, 2026 22:25
@meziantou meziantou merged commit e374cbb into main May 4, 2026
13 checks passed
@meziantou meziantou deleted the meziantou/fix-ma0042-comments branch May 4, 2026 22:26
This was referenced May 4, 2026
This was referenced May 11, 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