Repository navigation
VB -> C#: keep braces around a lone declaration in loop/lock/using/single-line If bodies (CS1023) - #1322
Merged
GrahamTheCoder merged 2 commits intoNov 18, 2026
Conversation
Pure refactor so the single-statement unpacking rule lives in one place. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_019FEr8FpweGikrBKYdcSCS1
C# forbids local declarations, labeled statements and local functions as the embedded statement of if/else/while/do/for/foreach/lock/using (CS1023, Binder.BindPossibleEmbeddedStatement). The block unpacking used by While, Do, For, For Each, SyncLock, Using (expression form) and single-line If/Else removed the braces around a lone Dim, giving e.g. "while (a) int z = F();". Multi-line If was unaffected because it always keeps its block. A lone label was already kept braced (its EmptyStatement counts as nested); a test pins that too. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_019FEr8FpweGikrBKYdcSCS1
1 task done
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.
No issue filed; reported directly.
Problem
converted to
while (a) int z = F();→ CS1023 "Embedded statement cannot be a declaration or labeled statement". Same forDo While/Until,Do ... Loop While,For,For Each,SyncLock,Using <expr>, and single-lineIf ... Then Dim .../Else Dim ....Solution
Extensions.UnpackNonNestedBlock/UnpackPossiblyNestedBlockunwrapped any single-statement block without checking C#'s embedded-statement rule. C# rejectsLocalDeclarationStatement,LabeledStatementandLocalFunctionStatementthere (Binder.BindPossibleEmbeddedStatement).StatementSyntax.CanBeEmbedded()guard inUnpackPossiblyNestedBlock, whichUnpackNonNestedBlocknow delegates to (pure refactor in its own commit first). All callers inMethodBodyExecutableStatementVisitorare covered.If/ElseIf/Elsewas unaffected:VisitMultiLineIfBlockandConvertElseClauseAsyncalways keep theBlock(only unwrapping a nestedifforelse if).EmptyStatementcounts as a nested statement. Pinned with a test anyway.CanBeEmbeddedrule mentioned for the upstream VB -> C#: Incorrect conversion ofIfimmediately preceded byElse#823 branch doesn't exist on master orclaude/fix-823-dangling-else, so it's added here inCodeConverter/CSharp/Extensions.cs; VB -> C#: Incorrect conversion ofIfimmediately preceded byElse#823 could reuse it.Tests
*KeepsBraces*inLoopStatementTests,MethodStatementTests): While, Do While, Do Until, Do…Loop While, For, For Each, lone label, single-line If, single-line Else, SyncLock, Using expression. The 10 declaration cases failed with CS1023 before the fix.VariableScopeTests.TestLoneDeclarationAsBlockBodyStillRuns: logs each loneDim's initializer side effect across all of these constructs and checks the log. Failed with CS1023 before the fix.Process
Generated by Claude Code