Skip to content

Commit

Permalink
Use C# 7.3 compatible null check and simplify unary negation
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamTheCoder committed May 23, 2020
1 parent 72bd4b7 commit 844f8f1
Show file tree
Hide file tree
Showing 16 changed files with 76 additions and 58 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
* Remove redundant empty string coalesce in string comparison - [#500](https://github.com/icsharpcode/CodeConverter/issues/500)
* Convert VB comparison operators - [#396](https://github.com/icsharpcode/CodeConverter/issues/396)
* Convert Redim Preserve of 1D array to Array.Resize - [#501](https://github.com/icsharpcode/CodeConverter/issues/501)
* Use C#7.3 compatible null check

### C# -> VB

Expand Down
7 changes: 3 additions & 4 deletions CodeConverter/CSharp/CommonConversions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -661,10 +661,9 @@ public static CSSyntax.BinaryExpressionSyntax NotNothingComparison(ExpressionSyn

public static ExpressionSyntax NothingComparison(ExpressionSyntax otherArgument, bool isReferenceType)
{
if (isReferenceType) {
return SyntaxFactory.IsPatternExpression(otherArgument, SyntaxFactory.ConstantPattern(SyntaxFactory.LiteralExpression(CSSyntaxKind.NullLiteralExpression)));
}
return SyntaxFactory.BinaryExpression(CSSyntaxKind.EqualsExpression, otherArgument, SyntaxFactory.LiteralExpression(CSSyntaxKind.DefaultLiteralExpression));
// Old project style doesn't support is pattern expressions (or indeed anything beyond c#7.3), so can't use "x is null"
var literalKind = isReferenceType ? CSSyntaxKind.NullLiteralExpression : CSSyntaxKind.DefaultLiteralExpression;
return SyntaxFactory.BinaryExpression(CSSyntaxKind.EqualsExpression, otherArgument, SyntaxFactory.LiteralExpression(literalKind));
}
}
}
2 changes: 1 addition & 1 deletion CodeConverter/CSharp/ExpressionNodeVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ private ExpressionSyntax AsBool(VBSyntax.UnaryExpressionSyntax node, ExpressionS

private async Task<ExpressionSyntax> NegateAndSimplifyOrNullAsync(VBSyntax.UnaryExpressionSyntax node, ExpressionSyntax expr)
{
if (await _operatorConverter.ConvertNothingComparisonOrNullAsync(node.Operand, true) is ExpressionSyntax nothingComparison) {
if (await _operatorConverter.ConvertNothingComparisonOrNullAsync(node.Operand.SkipParens(), true) is ExpressionSyntax nothingComparison) {
return nothingComparison;
} else if (expr is BinaryExpressionSyntax bes && bes.OperatorToken.IsKind(SyntaxKind.EqualsToken)) {
return bes.WithOperatorToken(SyntaxFactory.Token(SyntaxKind.ExclamationEqualsToken));
Expand Down
20 changes: 19 additions & 1 deletion Tests/CSharp/ExpressionTests/ExpressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,24 @@ internal partial class TestClass
}");
}

[Fact]
public async Task GenericComparisonAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Public Class GenericComparison
Public Sub m(Of T)(p As T)
If p Is Nothing Then Return
End Sub
End Class", @"
public partial class GenericComparison
{
public void m<T>(T p)
{
if (p == null)
return;
}
}");
}

[Fact]
public async Task AccessSharedThroughInstanceAsync()
{
Expand Down Expand Up @@ -1308,7 +1326,7 @@ public partial class Foo
protected void OnBar(EventArgs e)
{
if (Bar is null)
if (Bar == null)
{
Debug.WriteLine(""No subscriber"");
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/CSharp/StatementTests/StatementTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1390,7 +1390,7 @@ internal partial class TestClass
{
private void TestMethod(object nullObject)
{
if (nullObject is null)
if (nullObject == null)
throw new ArgumentNullException(nameof(nullObject));
lock (nullObject)
Console.WriteLine(nullObject);
Expand All @@ -1411,7 +1411,7 @@ internal partial class TestClass
{
private void TestMethod(object nullObject)
{
if (nullObject is null)
if (nullObject == null)
throw new ArgumentNullException(nameof(nullObject));
}
}");
Expand Down
2 changes: 1 addition & 1 deletion Tests/CSharp/TypeCastTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ public partial class MultipleCasts
{
public static T ToGenericParameter<T>(object Value)
{
if (Value is null)
if (Value == null)
{
return default;
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 844f8f1

Please sign in to comment.