Skip to content

Commit 1a7781d

Browse files
Switch to "is null"
1 parent d66da69 commit 1a7781d

14 files changed

Lines changed: 64 additions & 61 deletions

File tree

CodeConverter/CSharp/BuiltInVisualBasicOperatorSubstitutions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ public async Task<ExpressionSyntax> ConvertNothingComparisonOrNullAsync(VBSyntax
5959
return null;
6060
}
6161
var csOtherArg = (ExpressionSyntax)await ConvertIsOrIsNotExpressionArgAsync(vbOtherArg);
62-
var couldHaveOverloadedOperators = !_semanticModel.GetTypeInfo(vbOtherArg).Type.IsSpecialType();
63-
var isReferenceComparison = node.IsKind(VBasic.SyntaxKind.IsExpression, VBasic.SyntaxKind.IsNotExpression);
62+
var typeSymbol = _semanticModel.GetTypeInfo(vbOtherArg).Type;
63+
var isReferenceComparison = typeSymbol?.IsValueType != true || node.IsKind(VBasic.SyntaxKind.IsExpression, VBasic.SyntaxKind.IsNotExpression);
6464
var notted = node.IsKind(VBasic.SyntaxKind.IsNotExpression, VBasic.SyntaxKind.NotEqualsExpression) || negateExpression;
65-
return notted ? CommonConversions.NotNothingComparison(csOtherArg, isReferenceComparison) : CommonConversions.NothingComparison(csOtherArg, isReferenceComparison, couldHaveOverloadedOperators);
65+
return notted ? CommonConversions.NotNothingComparison(csOtherArg, isReferenceComparison) : CommonConversions.NothingComparison(csOtherArg, isReferenceComparison);
6666
}
6767

6868
private async Task<CSharpSyntaxNode> ConvertIsOrIsNotExpressionArgAsync(VBSyntax.ExpressionSyntax binaryExpressionArg)

CodeConverter/CSharp/CommonConversions.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -658,17 +658,20 @@ public static CSSyntax.ParameterListSyntax CreateParameterList(IEnumerable<Synta
658658
public static CSSyntax.BinaryExpressionSyntax NotNothingComparison(ExpressionSyntax otherArgument, bool isReferenceType)
659659
{
660660
if (isReferenceType) {
661+
// When we upgrade the CodeAnalysis version, we'll be able to use a RecursivePattern of "{}" instead
661662
return SyntaxFactory.BinaryExpression(CSSyntaxKind.IsExpression, otherArgument, ValidSyntaxFactory.ObjectType);
662663
}
663664
return SyntaxFactory.BinaryExpression(CSSyntaxKind.NotEqualsExpression, otherArgument, ValidSyntaxFactory.DefaultExpression);
664665
}
665666

666-
public static ExpressionSyntax NothingComparison(ExpressionSyntax otherArgument, bool isReferenceType, bool couldHaveOverloadedOperators)
667+
public static ExpressionSyntax NothingComparison(ExpressionSyntax otherArgument, bool isReferenceType)
667668
{
668-
// Old project style doesn't support is pattern expressions (or indeed anything beyond c#7.3), so can't use "x is null"
669-
var literal = isReferenceType ? ValidSyntaxFactory.NullExpression : (ExpressionSyntax)ValidSyntaxFactory.DefaultExpression;
670-
if (isReferenceType && couldHaveOverloadedOperators) otherArgument = SyntaxFactory.ParenthesizedExpression(SyntaxFactory.CastExpression(ValidSyntaxFactory.ObjectType, otherArgument));
671-
return SyntaxFactory.BinaryExpression(CSSyntaxKind.EqualsExpression, otherArgument, literal);
669+
if (isReferenceType) {
670+
return SyntaxFactory.IsPatternExpression(otherArgument,
671+
SyntaxFactory.ConstantPattern(ValidSyntaxFactory.NullExpression));
672+
}
673+
674+
return SyntaxFactory.BinaryExpression(CSSyntaxKind.EqualsExpression, otherArgument, ValidSyntaxFactory.DefaultExpression);
672675
}
673676
}
674677
}

Tests/CSharp/ExpressionTests/ExpressionTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ public partial class GenericComparison
399399
{
400400
public void m<T>(T p)
401401
{
402-
if (p == null)
402+
if (p is null)
403403
return;
404404
}
405405
}");
@@ -1347,7 +1347,7 @@ public partial class Foo
13471347
13481348
protected void OnBar(EventArgs e)
13491349
{
1350-
if ((object)Bar == null)
1350+
if (Bar is null)
13511351
{
13521352
Debug.WriteLine(""No subscriber"");
13531353
}

Tests/CSharp/StatementTests/StatementTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,7 +1376,7 @@ internal partial class TestClass
13761376
{
13771377
private void TestMethod(object nullObject)
13781378
{
1379-
if (nullObject == null)
1379+
if (nullObject is null)
13801380
throw new ArgumentNullException(nameof(nullObject));
13811381
lock (nullObject)
13821382
Console.WriteLine(nullObject);
@@ -1397,7 +1397,7 @@ internal partial class TestClass
13971397
{
13981398
private void TestMethod(object nullObject)
13991399
{
1400-
if (nullObject == null)
1400+
if (nullObject is null)
14011401
throw new ArgumentNullException(nameof(nullObject));
14021402
}
14031403
}");

Tests/CSharp/TypeCastTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ public partial class MultipleCasts
402402
{
403403
public static T ToGenericParameter<T>(object Value)
404404
{
405-
if (Value == null)
405+
if (Value is null)
406406
{
407407
return default;
408408
}

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.

Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/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.

0 commit comments

Comments
 (0)