Skip to content

Commit 062b902

Browse files
Tidy up string comparison - fixes #500
1 parent acd3108 commit 062b902

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed

CodeConverter/CSharp/VisualBasicEqualityComparison.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,17 @@ public RequiredType GetObjectEqualityType(VBSyntax.BinaryExpressionSyntax node,
6565

6666
public (ExpressionSyntax lhs, ExpressionSyntax rhs) VbCoerceToString(VBSyntax.ExpressionSyntax vbLeft, ExpressionSyntax csLeft, TypeInfo lhsTypeInfo, VBSyntax.ExpressionSyntax vbRight, ExpressionSyntax csRight, TypeInfo rhsTypeInfo)
6767
{
68+
if (IsNonEmptyStringLiteral(vbLeft) || IsNonEmptyStringLiteral(vbRight)) {
69+
return (csLeft, csRight);
70+
}
6871
return (VbCoerceToString(vbLeft, csLeft, lhsTypeInfo), VbCoerceToString(vbRight, csRight, rhsTypeInfo));
6972
}
7073

74+
private static bool IsNonEmptyStringLiteral(VBSyntax.ExpressionSyntax vbExpr)
75+
{
76+
return vbExpr.SkipParens().IsKind(VBSyntaxKind.StringLiteralExpression) && vbExpr is VBSyntax.LiteralExpressionSyntax literal && !IsEmptyString(literal);
77+
}
78+
7179
private ExpressionSyntax VbCoerceToString(VBSyntax.ExpressionSyntax vbNode, ExpressionSyntax csNode, TypeInfo typeInfo)
7280
{
7381
bool isStringType = typeInfo.Type.SpecialType == SpecialType.System_String;
@@ -163,12 +171,16 @@ private bool IsNothingOrEmpty(VBSyntax.ExpressionSyntax expressionSyntax)
163171
{
164172
if (expressionSyntax is VBSyntax.LiteralExpressionSyntax les &&
165173
(les.IsKind(VBSyntaxKind.NothingLiteralExpression) ||
166-
(les.IsKind(VBSyntaxKind.StringLiteralExpression) &&
167-
String.IsNullOrEmpty(les.Token.ValueText)))) return true;
174+
IsEmptyString(les))) return true;
168175
var constantAnalysis = _semanticModel.GetConstantValue(expressionSyntax);
169176
return constantAnalysis.HasValue && (constantAnalysis.Value == null || constantAnalysis.Value as string == "");
170177
}
171178

179+
private static bool IsEmptyString(VBSyntax.LiteralExpressionSyntax les)
180+
{
181+
return les.IsKind(VBSyntaxKind.StringLiteralExpression) && string.IsNullOrEmpty(les.Token.ValueText);
182+
}
183+
172184
private static ExpressionSyntax NegateIfNeeded(VBSyntax.BinaryExpressionSyntax node, InvocationExpressionSyntax positiveExpression)
173185
{
174186
return node.IsKind(VBasic.SyntaxKind.EqualsExpression)

Tests/CSharp/ExpressionTests/ByRefTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,9 +456,9 @@ public void DoSomething(ref string str)
456456
public void Main()
457457
{
458458
DoSomething(ref arr[1]);
459-
Debug.Assert((arr[1] ?? """") == ""test"");
459+
Debug.Assert(arr[1] == ""test"");
460460
DoSomething(ref arr2[2, 2]);
461-
Debug.Assert((arr2[2, 2] ?? """") == ""test"");
461+
Debug.Assert(arr2[2, 2] == ""test"");
462462
}
463463
}");
464464
}

Tests/CSharp/ExpressionTests/LinqExpressionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ Return String.Empty
189189
}
190190

191191
[Fact]
192-
public async Task LinqAsEnumerable()
192+
public async Task LinqAsEnumerableAsync()
193193
{
194194
await TestConversionVisualBasicToCSharpAsync(@"Imports System.Data
195195

Tests/CSharp/ExpressionTests/StringExpressionTests.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,12 @@ public void Foo()
126126
throw new Exception();
127127
}
128128
129-
if ((s1 ?? """") == ""something"")
129+
if (s1 == ""something"")
130130
{
131131
throw new Exception();
132132
}
133133
134-
if (""something"" == (s1 ?? """"))
134+
if (""something"" == s1)
135135
{
136136
throw new Exception();
137137
}
@@ -318,6 +318,29 @@ public void Foo()
318318
{
319319
string x = ""x"" + 4 + ""y"";
320320
}
321+
}");
322+
}
323+
324+
[Fact]
325+
public async Task EmptyStringCoalesceSkippedForLiteralComparisonAsync()
326+
{
327+
await TestConversionVisualBasicToCSharpAsync(
328+
@"Public Class VisualBasicClass
329+
330+
Sub Foo()
331+
Dim x = """"
332+
Dim y = x = ""something""
333+
End Sub
334+
335+
End Class",
336+
@"
337+
public partial class VisualBasicClass
338+
{
339+
public void Foo()
340+
{
341+
string x = """";
342+
bool y = x == ""something"";
343+
}
321344
}");
322345
}
323346
}

0 commit comments

Comments
 (0)