Skip to content

Commit dd8558c

Browse files
Wrap all cases for case insensitive strings
1 parent d8b3cd2 commit dd8558c

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

CodeConverter/CSharp/MethodBodyExecutableStatementVisitor.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,8 @@ public override async Task<SyntaxList<StatementSyntax>> VisitGoToStatement(VBSyn
641641
public override async Task<SyntaxList<StatementSyntax>> VisitSelectBlock(VBSyntax.SelectBlockSyntax node)
642642
{
643643
var vbExpr = node.SelectStatement.Expression;
644+
var wrapAllCases = CommonConversions.VisualBasicEqualityComparison.OptionCompareTextCaseInsensitive &&
645+
_semanticModel.GetTypeInfo(vbExpr).ConvertedType?.SpecialType == SpecialType.System_String;
644646
var (csExpr, stmts, csExprWithSourceMapping) = await GetExpressionWithoutSideEffectsAsync(vbExpr, "switchExpr");
645647
var usedConstantValues = new HashSet<object>();
646648
var sections = new List<SwitchSectionSyntax>();
@@ -654,7 +656,7 @@ public override async Task<SyntaxList<StatementSyntax>> VisitSelectBlock(VBSynta
654656
var correctTypeExpressionSyntax = CommonConversions.TypeConversionAnalyzer.AddExplicitConversion(s.Value, originalExpressionSyntax, typeConversionKind, true, true);
655657
var constantValue = _semanticModel.GetConstantValue(s.Value);
656658
var notAlreadyUsed = !constantValue.HasValue || usedConstantValues.Add(constantValue.Value);
657-
var caseSwitchLabelSyntax = correctTypeExpressionSyntax.IsConst && notAlreadyUsed
659+
var caseSwitchLabelSyntax = !wrapAllCases && correctTypeExpressionSyntax.IsConst && notAlreadyUsed
658660
? (SwitchLabelSyntax)SyntaxFactory.CaseSwitchLabel(correctTypeExpressionSyntax.Expr)
659661
: WrapInCasePatternSwitchLabelSyntax(node, correctTypeExpressionSyntax.Expr);
660662
labels.Add(caseSwitchLabelSyntax);

Tests/CSharp/StatementTests/StatementTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,6 +1761,44 @@ public int Add(int x)
17611761
}");
17621762
}
17631763

1764+
[Fact]
1765+
public async Task Issue579SelectCaseWithCaseInsensitiveTextCompareAsync()
1766+
{
1767+
await TestConversionVisualBasicToCSharpAsync(@"
1768+
Option Compare Text
1769+
1770+
Class Issue579SelectCaseWithCaseInsensitiveTextCompare
1771+
Private Function Test(astr_Temp As String) As Boolean
1772+
Select Case astr_Temp
1773+
Case ""Test""
1774+
Return True
1775+
Case Else
1776+
Return False
1777+
End Select
1778+
End Function
1779+
End Class", @"
1780+
internal partial class Issue579SelectCaseWithCaseInsensitiveTextCompare
1781+
{
1782+
private bool Test(string astr_Temp)
1783+
{
1784+
switch (astr_Temp)
1785+
{
1786+
case var @case when @case == ""Test"":
1787+
{
1788+
return true;
1789+
}
1790+
1791+
default:
1792+
{
1793+
return false;
1794+
}
1795+
}
1796+
}
1797+
}
1798+
1 target compilation errors:
1799+
CS0825: The contextual keyword 'var' may only appear within a local variable declaration or in script code");
1800+
}
1801+
17641802
[Fact]
17651803
public async Task TryCatchAsync()
17661804
{

0 commit comments

Comments
 (0)