From 8027c8a2b05f2135bf498f8c3bf8ec0ac97505e4 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 14 Mar 2026 09:52:09 +0000 Subject: [PATCH] Fix lambda block unpacking with comments When a single statement lambda contained comments, unpacking it to an expression body or an unparenthesized block resulted in the comments being pushed outside the lambda when it's placed inside method invocations like `Parallel.ForEach`. This checks for comments in the lambda node and forces generating a full block syntax to preserve the comments inside. Co-authored-by: GrahamTheCoder <2490482+GrahamTheCoder@users.noreply.github.com> --- CodeConverter/CSharp/LambdaConverter.cs | 3 +- .../CSharp/ExpressionTests/ExpressionTests.cs | 48 ++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/CodeConverter/CSharp/LambdaConverter.cs b/CodeConverter/CSharp/LambdaConverter.cs index f1306f5b7..1be81c651 100644 --- a/CodeConverter/CSharp/LambdaConverter.cs +++ b/CodeConverter/CSharp/LambdaConverter.cs @@ -75,7 +75,8 @@ public async Task ConvertAsync(VBSyntax.LambdaExpressionSyntax BlockSyntax block = null; ExpressionSyntax expressionBody = null; ArrowExpressionClauseSyntax arrow = null; - if (!convertedStatements.TryUnpackSingleStatement(out StatementSyntax singleStatement)) { + bool hasComments = vbNode.DescendantTrivia().Any(t => t.IsKind(VBasic.SyntaxKind.CommentTrivia)); + if (hasComments || !convertedStatements.TryUnpackSingleStatement(out StatementSyntax singleStatement)) { convertedStatements = convertedStatements.Select(l => l.WithTrailingTrivia(SyntaxFactory.ElasticCarriageReturnLineFeed)).ToList(); block = SyntaxFactory.Block(convertedStatements); } else if (singleStatement.TryUnpackSingleExpressionFromStatement(out expressionBody)) { diff --git a/Tests/CSharp/ExpressionTests/ExpressionTests.cs b/Tests/CSharp/ExpressionTests/ExpressionTests.cs index 2f7b78770..64727cbc1 100644 --- a/Tests/CSharp/ExpressionTests/ExpressionTests.cs +++ b/Tests/CSharp/ExpressionTests/ExpressionTests.cs @@ -2931,4 +2931,50 @@ public object Edit(bool flag2 = false, CrashEnum? crashEnum = default) } }"); } -} \ No newline at end of file + [Fact] + public async Task LambdaBodyExpressionWithCommentsAsync() { + await TestConversionVisualBasicToCSharpAsync(@" +Imports System.Threading.Tasks +Imports System.Collections.Generic + +Public Class ConversionTest1 + Public Sub BugRepro() + Dim dt As New List(Of Integer) + + Parallel.ForEach(dt, Sub(row) + If Not String.IsNullOrWhiteSpace("""") Then + 'comment1 + Dim test1 As Boolean = True + 'comment2 + Dim test2 As Boolean = True + 'comment3 + Dim test3 As Boolean = True + End If + End Sub) + End Sub +End Class +", @"using System.Collections.Generic; +using System.Threading.Tasks; + +public partial class ConversionTest1 +{ + public void BugRepro() + { + var dt = new List(); + + Parallel.ForEach(dt, row => + { + if (!string.IsNullOrWhiteSpace("""")) + { + // comment1 + bool test1 = true; + // comment2 + bool test2 = true; + // comment3 + bool test3 = true; + } + }); + } +}", incompatibleWithAutomatedCommentTesting: true); + } +}