Skip to content

Commit 844f8f1

Browse files
Use C# 7.3 compatible null check and simplify unary negation
1 parent 72bd4b7 commit 844f8f1

File tree

16 files changed

+76
-58
lines changed

16 files changed

+76
-58
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
1515
* Remove redundant empty string coalesce in string comparison - [#500](https://github.com/icsharpcode/CodeConverter/issues/500)
1616
* Convert VB comparison operators - [#396](https://github.com/icsharpcode/CodeConverter/issues/396)
1717
* Convert Redim Preserve of 1D array to Array.Resize - [#501](https://github.com/icsharpcode/CodeConverter/issues/501)
18+
* Use C#7.3 compatible null check
1819

1920
### C# -> VB
2021

CodeConverter/CSharp/CommonConversions.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -661,10 +661,9 @@ public static CSSyntax.BinaryExpressionSyntax NotNothingComparison(ExpressionSyn
661661

662662
public static ExpressionSyntax NothingComparison(ExpressionSyntax otherArgument, bool isReferenceType)
663663
{
664-
if (isReferenceType) {
665-
return SyntaxFactory.IsPatternExpression(otherArgument, SyntaxFactory.ConstantPattern(SyntaxFactory.LiteralExpression(CSSyntaxKind.NullLiteralExpression)));
666-
}
667-
return SyntaxFactory.BinaryExpression(CSSyntaxKind.EqualsExpression, otherArgument, SyntaxFactory.LiteralExpression(CSSyntaxKind.DefaultLiteralExpression));
664+
// Old project style doesn't support is pattern expressions (or indeed anything beyond c#7.3), so can't use "x is null"
665+
var literalKind = isReferenceType ? CSSyntaxKind.NullLiteralExpression : CSSyntaxKind.DefaultLiteralExpression;
666+
return SyntaxFactory.BinaryExpression(CSSyntaxKind.EqualsExpression, otherArgument, SyntaxFactory.LiteralExpression(literalKind));
668667
}
669668
}
670669
}

CodeConverter/CSharp/ExpressionNodeVisitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ private ExpressionSyntax AsBool(VBSyntax.UnaryExpressionSyntax node, ExpressionS
675675

676676
private async Task<ExpressionSyntax> NegateAndSimplifyOrNullAsync(VBSyntax.UnaryExpressionSyntax node, ExpressionSyntax expr)
677677
{
678-
if (await _operatorConverter.ConvertNothingComparisonOrNullAsync(node.Operand, true) is ExpressionSyntax nothingComparison) {
678+
if (await _operatorConverter.ConvertNothingComparisonOrNullAsync(node.Operand.SkipParens(), true) is ExpressionSyntax nothingComparison) {
679679
return nothingComparison;
680680
} else if (expr is BinaryExpressionSyntax bes && bes.OperatorToken.IsKind(SyntaxKind.EqualsToken)) {
681681
return bes.WithOperatorToken(SyntaxFactory.Token(SyntaxKind.ExclamationEqualsToken));

Tests/CSharp/ExpressionTests/ExpressionTests.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,24 @@ internal partial class TestClass
356356
}");
357357
}
358358

359+
[Fact]
360+
public async Task GenericComparisonAsync()
361+
{
362+
await TestConversionVisualBasicToCSharpAsync(@"Public Class GenericComparison
363+
Public Sub m(Of T)(p As T)
364+
If p Is Nothing Then Return
365+
End Sub
366+
End Class", @"
367+
public partial class GenericComparison
368+
{
369+
public void m<T>(T p)
370+
{
371+
if (p == null)
372+
return;
373+
}
374+
}");
375+
}
376+
359377
[Fact]
360378
public async Task AccessSharedThroughInstanceAsync()
361379
{
@@ -1308,7 +1326,7 @@ public partial class Foo
13081326
13091327
protected void OnBar(EventArgs e)
13101328
{
1311-
if (Bar is null)
1329+
if (Bar == null)
13121330
{
13131331
Debug.WriteLine(""No subscriber"");
13141332
}

Tests/CSharp/StatementTests/StatementTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,7 +1390,7 @@ internal partial class TestClass
13901390
{
13911391
private void TestMethod(object nullObject)
13921392
{
1393-
if (nullObject is null)
1393+
if (nullObject == null)
13941394
throw new ArgumentNullException(nameof(nullObject));
13951395
lock (nullObject)
13961396
Console.WriteLine(nullObject);
@@ -1411,7 +1411,7 @@ internal partial class TestClass
14111411
{
14121412
private void TestMethod(object nullObject)
14131413
{
1414-
if (nullObject is null)
1414+
if (nullObject == null)
14151415
throw new ArgumentNullException(nameof(nullObject));
14161416
}
14171417
}");

Tests/CSharp/TypeCastTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ public partial class MultipleCasts
382382
{
383383
public static T ToGenericParameter<T>(object Value)
384384
{
385-
if (Value is null)
385+
if (Value == null)
386386
{
387387
return default;
388388
}

Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertVbLibraryOnly/VbLibrary/My Project/MyNamespace.Static.1.Designer.cs

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertVbLibraryOnly/VbLibrary/My Project/MyNamespace.Static.2.Designer.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/ConsoleApp4/My Project/MyNamespace.Static.2.Designer.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/VbLibrary/My Project/MyNamespace.Static.1.Designer.cs

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)