From 5cff392c45b756a044c9713d176fe9c2e115d73f Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 18 Nov 2026 19:08:10 +0000 Subject: [PATCH 1/2] Make UnpackNonNestedBlock delegate to UnpackPossiblyNestedBlock Pure refactor so the single-statement unpacking rule lives in one place. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_019FEr8FpweGikrBKYdcSCS1 --- CodeConverter/CSharp/Extensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CodeConverter/CSharp/Extensions.cs b/CodeConverter/CSharp/Extensions.cs index df6292d4e..1f72ccc03 100644 --- a/CodeConverter/CSharp/Extensions.cs +++ b/CodeConverter/CSharp/Extensions.cs @@ -12,7 +12,7 @@ internal static class Extensions /// public static StatementSyntax UnpackNonNestedBlock(this BlockSyntax block) { - return block.Statements.Count == 1 && !block.ContainsNestedStatements() ? block.Statements[0] : block; + return block.ContainsNestedStatements() ? block : block.UnpackPossiblyNestedBlock(); } /// From 5efe10e48e6dbdeddafba47714aaa4bfcb14737e Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 18 Nov 2026 19:08:10 +0000 Subject: [PATCH 2/2] Keep braces when a lone statement can't be an embedded statement 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 Claude-Session: https://claude.ai/code/session_019FEr8FpweGikrBKYdcSCS1 --- CodeConverter/CSharp/Extensions.cs | 14 +- .../StatementTests/LoopStatementTests.cs | 222 +++++++++++++++++- .../StatementTests/MethodStatementTests.cs | 123 ++++++++++ .../VBToCS/VariableScopeTests.vb | 39 +++ 4 files changed, 395 insertions(+), 3 deletions(-) diff --git a/CodeConverter/CSharp/Extensions.cs b/CodeConverter/CSharp/Extensions.cs index 1f72ccc03..12d3af36b 100644 --- a/CodeConverter/CSharp/Extensions.cs +++ b/CodeConverter/CSharp/Extensions.cs @@ -1,4 +1,5 @@ -using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; namespace ICSharpCode.CodeConverter.CSharp; @@ -56,7 +57,16 @@ public static bool TryUnpackSingleExpressionFromStatement(this StatementSyntax s public static StatementSyntax UnpackPossiblyNestedBlock(this BlockSyntax block) { SyntaxList statementSyntaxs = block.Statements; - return statementSyntaxs.Count == 1 ? statementSyntaxs[0] : block; + return statementSyntaxs.Count == 1 && statementSyntaxs[0].CanBeEmbedded() ? statementSyntaxs[0] : block; + } + + /// + /// C# forbids declarations and labels as the embedded statement of if/else/while/do/for/foreach/lock/using (CS1023), + /// see BindPossibleEmbeddedStatement in https://github.com/dotnet/roslyn/blob/main/src/Compilers/CSharp/Portable/Binder/Binder_Statements.cs + /// + public static bool CanBeEmbedded(this StatementSyntax statement) + { + return !statement.IsKind(SyntaxKind.LocalDeclarationStatement, SyntaxKind.LabeledStatement, SyntaxKind.LocalFunctionStatement); } private static bool ContainsNestedStatements(this BlockSyntax block) diff --git a/Tests/CSharp/StatementTests/LoopStatementTests.cs b/Tests/CSharp/StatementTests/LoopStatementTests.cs index d30e023c5..32a5eef65 100644 --- a/Tests/CSharp/StatementTests/LoopStatementTests.cs +++ b/Tests/CSharp/StatementTests/LoopStatementTests.cs @@ -812,4 +812,224 @@ private void TestMethod(bool someCondition) } }"); } -} \ No newline at end of file + + [Fact] + public async Task WhileWithLoneLocalDeclarationKeepsBracesAsync() + { + await TestConversionVisualBasicToCSharpAsync(@"Class TestClass + Private Sub TestMethod(a As Boolean) + While a + Dim z = F() + End While + End Sub + + Private Function F() As Integer + Return 1 + End Function +End Class", @" +internal partial class TestClass +{ + private void TestMethod(bool a) + { + while (a) + { + int z = F(); + } + } + + private int F() + { + return 1; + } +}"); + } + + [Fact] + public async Task DoWhileWithLoneLocalDeclarationKeepsBracesAsync() + { + await TestConversionVisualBasicToCSharpAsync(@"Class TestClass + Private Sub TestMethod(a As Boolean) + Do While a + Dim z = F() + Loop + End Sub + + Private Function F() As Integer + Return 1 + End Function +End Class", @" +internal partial class TestClass +{ + private void TestMethod(bool a) + { + while (a) + { + int z = F(); + } + } + + private int F() + { + return 1; + } +}"); + } + + [Fact] + public async Task DoUntilWithLoneLocalDeclarationKeepsBracesAsync() + { + await TestConversionVisualBasicToCSharpAsync(@"Class TestClass + Private Sub TestMethod(a As Boolean) + Do Until a + Dim z = F() + Loop + End Sub + + Private Function F() As Integer + Return 1 + End Function +End Class", @" +internal partial class TestClass +{ + private void TestMethod(bool a) + { + while (!a) + { + int z = F(); + } + } + + private int F() + { + return 1; + } +}"); + } + + [Fact] + public async Task DoLoopWhileWithLoneLocalDeclarationKeepsBracesAsync() + { + await TestConversionVisualBasicToCSharpAsync(@"Class TestClass + Private Sub TestMethod(a As Boolean) + Do + Dim z = F() + Loop While a + End Sub + + Private Function F() As Integer + Return 1 + End Function +End Class", @" +internal partial class TestClass +{ + private void TestMethod(bool a) + { + do + { + int z = F(); + } + while (a); + } + + private int F() + { + return 1; + } +}"); + } + + [Fact] + public async Task ForWithLoneLocalDeclarationKeepsBracesAsync() + { + await TestConversionVisualBasicToCSharpAsync(@"Class TestClass + Private Sub TestMethod() + For i = 1 To 2 + Dim z = F() + Next + End Sub + + Private Function F() As Integer + Return 1 + End Function +End Class", @" +internal partial class TestClass +{ + private void TestMethod() + { + for (int i = 1; i <= 2; i++) + { + int z = F(); + } + } + + private int F() + { + return 1; + } +}"); + } + + [Fact] + public async Task ForEachWithLoneLocalDeclarationKeepsBracesAsync() + { + await TestConversionVisualBasicToCSharpAsync(@"Class TestClass + Private Sub TestMethod(xs As Integer()) + For Each x In xs + Dim z = F() + Next + End Sub + + Private Function F() As Integer + Return 1 + End Function +End Class", @" +internal partial class TestClass +{ + private void TestMethod(int[] xs) + { + foreach (var x in xs) + { + int z = F(); + } + } + + private int F() + { + return 1; + } +}"); + } + + [Fact] + public async Task WhileWithLoneLabelKeepsBracesAsync() + { + await TestConversionVisualBasicToCSharpAsync(@"Class TestClass + Private Sub TestMethod(a As Boolean) + While a +lbl: + End While + End Sub + + Private Function F() As Integer + Return 1 + End Function +End Class", @" +internal partial class TestClass +{ + private void TestMethod(bool a) + { + while (a) + { + lbl: + ; + + } + } + + private int F() + { + return 1; + } +}"); + } +} diff --git a/Tests/CSharp/StatementTests/MethodStatementTests.cs b/Tests/CSharp/StatementTests/MethodStatementTests.cs index 1bcc291c6..0bf4323ae 100644 --- a/Tests/CSharp/StatementTests/MethodStatementTests.cs +++ b/Tests/CSharp/StatementTests/MethodStatementTests.cs @@ -1861,6 +1861,129 @@ public void AllAssignmentOperators() set_Item(0, get_Item(0) >> 2); set_StrItem(0, get_StrItem(0) + "" World""); } +}"); + } + + [Fact] + public async Task SingleLineIfWithLoneLocalDeclarationKeepsBracesAsync() + { + await TestConversionVisualBasicToCSharpAsync(@"Class TestClass + Private Sub TestMethod(a As Boolean) + If a Then Dim z = F() + End Sub + + Private Function F() As Integer + Return 1 + End Function +End Class", @" +internal partial class TestClass +{ + private void TestMethod(bool a) + { + if (a) + { + int z = F(); + } + } + + private int F() + { + return 1; + } +}"); + } + + [Fact] + public async Task SingleLineIfElseWithLoneLocalDeclarationKeepsBracesAsync() + { + await TestConversionVisualBasicToCSharpAsync(@"Class TestClass + Private Sub TestMethod(a As Boolean) + If a Then F() Else Dim z = F() + End Sub + + Private Function F() As Integer + Return 1 + End Function +End Class", @" +internal partial class TestClass +{ + private void TestMethod(bool a) + { + if (a) + F(); + else + { + int z = F(); + } + } + + private int F() + { + return 1; + } +}"); + } + + [Fact] + public async Task SyncLockWithLoneLocalDeclarationKeepsBracesAsync() + { + await TestConversionVisualBasicToCSharpAsync(@"Class TestClass + Private Sub TestMethod(o As Object) + SyncLock o + Dim z = F() + End SyncLock + End Sub + + Private Function F() As Integer + Return 1 + End Function +End Class", @" +internal partial class TestClass +{ + private void TestMethod(object o) + { + lock (o) + { + int z = F(); + } + } + + private int F() + { + return 1; + } +}"); + } + + [Fact] + public async Task UsingExpressionWithLoneLocalDeclarationKeepsBracesAsync() + { + await TestConversionVisualBasicToCSharpAsync(@"Class TestClass + Private Sub TestMethod(d As System.IDisposable) + Using d + Dim z = F() + End Using + End Sub + + Private Function F() As Integer + Return 1 + End Function +End Class", @"using System; + +internal partial class TestClass +{ + private void TestMethod(IDisposable d) + { + using (d) + { + int z = F(); + } + } + + private int F() + { + return 1; + } }"); } } diff --git a/Tests/TestData/SelfVerifyingTests/VBToCS/VariableScopeTests.vb b/Tests/TestData/SelfVerifyingTests/VBToCS/VariableScopeTests.vb index 52daa5272..2c2ecb314 100644 --- a/Tests/TestData/SelfVerifyingTests/VBToCS/VariableScopeTests.vb +++ b/Tests/TestData/SelfVerifyingTests/VBToCS/VariableScopeTests.vb @@ -75,5 +75,44 @@ Public Class VariableScopeTests Next End Sub + Private _loneDeclarationLog As New List(Of String) + + Private Function LogLoneDeclaration(name As String) As Integer + _loneDeclarationLog.Add(name) + Return 1 + End Function + + + Sub TestLoneDeclarationAsBlockBodyStillRuns() + Dim i = 0 + While i < 1 + Dim z = LogLoneDeclaration("While") + Increment(i) + End While + Do + Dim z = LogLoneDeclaration("DoLoopWhile") + Loop While False + For j = 1 To 2 + Dim z = LogLoneDeclaration("For") + Next + For Each x In {1} + Dim z = LogLoneDeclaration("ForEach") + Next + SyncLock _loneDeclarationLog + Dim z = LogLoneDeclaration("SyncLock") + End SyncLock + Using d As IDisposable = New IO.MemoryStream() + Using d + Dim z = LogLoneDeclaration("Using") + End Using + End Using + If True Then Dim z = LogLoneDeclaration("If") Else Dim y = LogLoneDeclaration("NotElse") + If False Then Dim z = LogLoneDeclaration("NotIf") Else Dim y = LogLoneDeclaration("Else") + Assert.Equal({"While", "DoLoopWhile", "For", "For", "ForEach", "SyncLock", "Using", "If", "Else"}, _loneDeclarationLog) + End Sub + + Private Shared Function Increment(ByRef i As Integer) As Integer + i += 1 + Return i + End Function End Class