From afaf706e1ee64ec39ab68ea49858cfbcc77f8fbd Mon Sep 17 00:00:00 2001 From: GrahamTheCoder Date: Sun, 19 Jan 2020 15:16:37 +0000 Subject: [PATCH] Avoid repeated break caused by explicit Exit Select - fixes #433 --- CHANGELOG.md | 1 + .../MethodBodyExecutableStatementVisitor.cs | 2 +- Tests/CSharp/StatementTests.cs | 27 +++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a7f37b9ea..ad0477edb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) * First effort converting some Xml Member Access * Avoid adding new keyword when not allowed/required [#504](https://github.com/icsharpcode/CodeConverter/issues/504) * Avoid evaluating Select Case expression multiple times in some cases where it may be non-deterministic or have side effects [#323](https://github.com/icsharpcode/CodeConverter/issues/323) +* Avoid repeated redundant break statement caused by explicit Exit Select [#433](https://github.com/icsharpcode/CodeConverter/issues/433) ### C# -> VB diff --git a/ICSharpCode.CodeConverter/CSharp/MethodBodyExecutableStatementVisitor.cs b/ICSharpCode.CodeConverter/CSharp/MethodBodyExecutableStatementVisitor.cs index 227d10063..414a89ce7 100644 --- a/ICSharpCode.CodeConverter/CSharp/MethodBodyExecutableStatementVisitor.cs +++ b/ICSharpCode.CodeConverter/CSharp/MethodBodyExecutableStatementVisitor.cs @@ -594,7 +594,7 @@ public override async Task> VisitSelectBlock(VBSynta var csBlockStatements = (await ConvertStatements(block.Statements)).ToList(); if (csBlockStatements.LastOrDefault() - ?.IsKind(SyntaxKind.ReturnStatement) != true) { + ?.IsKind(SyntaxKind.ReturnStatement, SyntaxKind.BreakStatement) != true) { csBlockStatements.Add(SyntaxFactory.BreakStatement()); } var list = SingleStatement(SyntaxFactory.Block(csBlockStatements)); diff --git a/Tests/CSharp/StatementTests.cs b/Tests/CSharp/StatementTests.cs index 23293a95e..943603168 100644 --- a/Tests/CSharp/StatementTests.cs +++ b/Tests/CSharp/StatementTests.cs @@ -2008,6 +2008,33 @@ public void DoesNotThrow() }"); } + [Fact] + public async Task SelectCaseWithExplicitExit() + { + await TestConversionVisualBasicToCSharpWithoutComments(@"Class A + Public Function Add(ByVal x As Integer) As Integer + Select Case x + Case 1 + Exit Select + End Select + Return 3 + End Function +End Class", @"internal partial class A +{ + public int Add(int x) + { + switch (x) + { + case 1: + { + break; + } + } + return 3; + } +}"); + } + [Fact] public async Task TryCatch() {