Skip to content

Commit dc547be

Browse files
Deal with coalescing literal null - fixes #707
1 parent 3bb328a commit dc547be

2 files changed

Lines changed: 40 additions & 3 deletions

File tree

CodeConverter/CSharp/VisualBasicEqualityComparison.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,6 @@ private static bool IsNonEmptyStringLiteral(VBSyntax.ExpressionSyntax vbExpr)
9191

9292
public ExpressionSyntax VbCoerceToNonNullString(VBSyntax.ExpressionSyntax vbNode, ExpressionSyntax csNode, TypeInfo typeInfo, bool knownNotNull = false)
9393
{
94-
bool isStringType = typeInfo.Type.SpecialType == SpecialType.System_String;
95-
9694
if (IsNothingOrEmpty(vbNode)) {
9795
return EmptyStringExpression();
9896
}
@@ -101,7 +99,7 @@ public ExpressionSyntax VbCoerceToNonNullString(VBSyntax.ExpressionSyntax vbNode
10199
return csNode;
102100
}
103101

104-
csNode = isStringType
102+
csNode = typeInfo.Type.SpecialType == SpecialType.System_String
105103
? SyntaxFactory.ParenthesizedExpression(Coalesce(csNode, EmptyStringExpression()))
106104
: Coalesce(csNode, EmptyCharArrayExpression());
107105

Tests/CSharp/StatementTests/StatementTests.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1809,6 +1809,45 @@ internal partial class Issue579SelectCaseWithCaseInsensitiveTextCompare
18091809
CS0825: The contextual keyword 'var' may only appear within a local variable declaration or in script code");
18101810
}
18111811

1812+
[Fact]
1813+
public async Task Issue707SelectCaseAsync()
1814+
{
1815+
await TestConversionVisualBasicToCSharpAsync(@"
1816+
Class Issue707SelectCaseAsyncClass
1817+
Private Function Exists(sort As Char?) As Boolean?
1818+
Select Case Microsoft.VisualBasic.LCase(sort + """")
1819+
Case """", Nothing
1820+
Return False
1821+
Case Else
1822+
Return True
1823+
End Select
1824+
End Function
1825+
End Class", @"using Microsoft.VisualBasic; // Install-Package Microsoft.VisualBasic
1826+
using Microsoft.VisualBasic.CompilerServices; // Install-Package Microsoft.VisualBasic
1827+
1828+
internal partial class Issue707SelectCaseAsyncClass
1829+
{
1830+
private bool? Exists(char? sort)
1831+
{
1832+
switch (Strings.LCase(Conversions.ToString(sort) + """") ?? """")
1833+
{
1834+
case var @case when @case == """":
1835+
case var case1 when case1 == """":
1836+
{
1837+
return false;
1838+
}
1839+
1840+
default:
1841+
{
1842+
return true;
1843+
}
1844+
}
1845+
}
1846+
}
1847+
1 target compilation errors:
1848+
CS0825: The contextual keyword 'var' may only appear within a local variable declaration or in script code");
1849+
}
1850+
18121851
[Fact]
18131852
public async Task TryCatchAsync()
18141853
{

0 commit comments

Comments
 (0)