Keep the initialization order in the MA0004 code fix - #1496
Merged
Merged
Conversation
When extracting the declaration of an `await using` statement, the fixer walked up through the enclosing `using (expr)` statements to find a place where a statement could be inserted, then moved the declaration above all of them. This evaluated the initializer before the expressions of those `using` statements, and left their resources undisposed when the initializer throws. Insert the declaration only when it can go directly before the `using` statement, and otherwise fall back to wrapping the declaration and the `using` statement in a block, which the fixer already did for the other positions where a statement cannot be inserted. That fallback now formats the block it creates, as its body kept the indentation it had.
meziantou
deleted the
feature/ma0004-using-initialization-order-05086b
branch
September 12, 2026 04:33
meziantou
added a commit
that referenced
this pull request
Sep 12, 2026
Resolve the conflict in UseConfigureAwaitFixer with #1496, which removed the walk through the enclosing using statements so the declaration is only inserted directly before the using statement, and already added the formatter annotation to the block fallback. Keep that structure and check the variable name against the enclosing function before inserting, so the scope is still preserved by the block fallback when the name is used elsewhere.
This was referenced Sep 12, 2026
This was referenced Sep 17, 2026
Merged
Open
Open
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The MA0004 code fix extracts the declaration of an
await usingstatement so thatConfigureAwaitcan be appended to the variable:TryInsertVariableStatementBeforeUsingwalked up through the enclosingusing (expr)statements to find a position where a statement could be inserted, then hoisted the declaration above all of them. That moves the initializer ahead of every crossedusingexpression:is fixed into
Create("inner")now runs beforeCreate("outer"). It also breaks exception safety: if the hoisted initializer throws, the resources acquired by the crossedusingstatements are never disposed, whereas originally they were.Change
The helper no longer ascends. It inserts the declaration only when it can go directly before the
usingstatement itself (its parent is a block, a switch section, or the global statement list). When theusingstatement is the embedded statement of another statement, the fixer falls back to wrapping the declaration and theusingstatement in a block, which it already did for the other positions where a statement cannot be inserted:The generated block now carries
Formatter.Annotation, so its body is reindented instead of keeping the indentation it had. That fallback path had no test coverage before, so the formatting had gone unnoticed.Tests
AwaitUsing_UnderUsingWithSideEffect_KeepsEvaluationOrder, with a side-effecting expression in the enclosingusingand in the initializer.AwaitUsing_WithMultipleUsings_ShouldNotThrowexpected the hoisted form, which is exactly the transformation that reorders the acquisition, so it now expects the block form.MA0004 tests pass on all five Roslyn versions (35 each), and the full Roslyn 5.9 suite passes (4576 tests, 0 failed, 0 skipped).
dotnet run --project src/DocumentationGeneratorproduces no markdown change.