提交 1b836615 编写于 作者: J Jason Malinowski

Update Generate Equals and GetHashCode to handle nullable members

Fixes https://github.com/dotnet/roslyn/issues/37913
上级 58f20239
......@@ -88,6 +88,45 @@ public override bool Equals(object obj)
parameters: CSharp6);
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateEqualsAndGetHashCode)]
public async Task TestNullableReferenceIEquatable()
{
await TestInRegularAndScript1Async(
@"#nullable enable
using System;
using System.Collections.Generic;
class S : IEquatable<S> { }
class Program
{
[|S? a;|]
}",
@"#nullable enable
using System;
using System.Collections.Generic;
class S : IEquatable<S> { }
class Program
{
S? a;
public override bool Equals(object? obj)
{
return obj is Program program &&
EqualityComparer<S?>.Default.Equals(a, program.a);
}
public override int GetHashCode()
{
return -1757793268 + EqualityComparer<S?>.Default.GetHashCode(a);
}
}", index: 1);
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateEqualsAndGetHashCode)]
public async Task TestValueIEquatable()
{
......
......@@ -365,7 +365,7 @@ private static bool IsPrimitiveValueType(ITypeSymbol typeSymbol)
ITypeSymbol type)
{
var equalityComparerType = compilation.EqualityComparerOfTType();
var constructedType = equalityComparerType.Construct(type);
var constructedType = equalityComparerType.ConstructWithNullability(type);
return factory.MemberAccessExpression(
factory.TypeExpression(constructedType),
factory.IdentifierName(DefaultName));
......@@ -375,8 +375,8 @@ private static ITypeSymbol GetType(Compilation compilation, ISymbol symbol)
{
switch (symbol)
{
case IFieldSymbol field: return field.Type;
case IPropertySymbol property: return property.Type;
case IFieldSymbol field: return field.GetTypeWithAnnotatedNullability();
case IPropertySymbol property: return property.GetTypeWithAnnotatedNullability();
default: return compilation.GetSpecialType(SpecialType.System_Object);
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册